Usage
from django.conf.urls import patterns, url
from django_view_dispatch import dispatch, dispatch_strict
from . import views
urlpatterns = patterns('',
url(r'^some_url/$', dispatch(get=views.my_view, post=views.my_view_for_post), name='events_json'),
# you can get any keyword argument you want to specify any http verb
url(r'^some_url/$', dispatch(get=views.my_view, put=views.create, foo=views.another_view, bar=views.baz), name='events_json'),
# by default, if a request has an HTTP verb that hasn't been specified in
# the dispatch() function, it will be redirected to the GET view if present
url(r'^some_url/$', dispatch(get=views.will_get_everything_thats_not_put, put=views.stuff), name='events_json'),
# you can change this behavior this way
url(r'^some_url/$', dispatch(get=views.stuff, put=views.will_get_everything_thats_not_get, default="put"), name='events_json'),
# if "default" is set to None, this behavior is disabled and and
# HttpResponseNotAllowed will be returned
url(r'^input/$', dispatch(post=views.my_view_for_post, default=None), name='events_json'), # behave like @require_POST
# a more explicit way to do that is provided with "dispatch_strict" which behave exactly like dispatch with default set to None
url(r'^input/$', dispatch_strict(post=views.my_view_for_post), name='events_json'),
)
Installation
pip install django_view_dispatch
Why
Something that you learn as you progress in programming is that conditional are “bad” (as in “should be avoided”) because they increase the complexity of your code and are a potential source of bugs.
For example, mixing POST and GET handling in the same view can be a good terrain for bug and for a more complex code while spliting it gives a KISS situation where you have two views that do only one thing and do it well.
Django doesn’t provide an easy nor standard way to do this so this library is trying to address that.
Can I use string like in the url function?
No, since django 1.8, using string to target views in patterns is depreciated, therefor there is no point in supporting this feature.
Also, to be honest, I have not idea on how to do that and haven’t really made any research about it. That would also make the code way more complex.
Running tests
pip install pytest py.test test.py
Licence
BSD.
Release files for django_view_dispatch 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_view_dispatch-0.1.tar.gz | 2.6 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_view_dispatch-0.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
| django_view_dispatch-0.1-py2.7.egg | Legacy Egg format | - | - | Details |
Total release size: 10.3 kB
Release files / django_view_dispatch-0.1.tar.gz
| Download URL | django_view_dispatch-0.1.tar.gz |
|---|---|
| Size | 2.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
78be42e34a9912142efb0f82f0eb908d3c42f58da8a633b8a9fe9f177d6c5c09
|
|
BLAKE2b-256 checksum How to use checksums |
210a9c12d50959c0f1be58be64f5420bc68ddad5b61194defd2f8416676826b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_view_dispatch-0.1-py2.py3-none-any.whl
| Download URL | django_view_dispatch-0.1-py2.py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
57b54025af7746a14c3106be3f247f6ab7b41bd6f579a6c99c4c3d737bef749f
|
|
BLAKE2b-256 checksum How to use checksums |
46e34ef095f2f1568d68af52001c1fb453d8b5d60f3cc9b2cf9942c40fceaa5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_view_dispatch-0.1-py2.7.egg
| Download URL | django_view_dispatch-0.1-py2.7.egg |
|---|---|
| Size | 2.8 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
364201152621961a132e08a07ec2e842dea53c94ae99978ce8e745f63a55814f
|
|
BLAKE2b-256 checksum How to use checksums |
9f2744e367fa0440f55955d998c090719cd9c4d3a9bbd2646d26fd024f6a3c32
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |