39๐
I generally make two patterns with a named url:
url(r'^so/(?P<required>\d+)/$', 'myapp.so', name='something'),
url(r'^so/(?P<required>\d+)/(?P<optional>.*)/$', 'myapp.so', name='something_else'),
18๐
Django urls are polymorphic:
url(r'^so/(?P<required>\d+)/$', 'myapp.so', name='sample_view'),
url(r'^so/(?P<required>\d+)/(?P<optional>.*)/$', 'myapp.so', name='sample_view'),
its obious that you have to make your views like this:
def sample_view(request, required, optional = None):
so you can call it with the same name and it would work work url resolver fine. However be aware that you cant pass None as the required argument and expect that it will get you to the regexp without argument:
Wrong:
{% url sample_view required optional %}
Correct:
{% if optional %}
{% url sample_view required optional %}
{% else %}
{% url sample_view required %}
{% endif %}
I dont know whether this is documented anywhere โ I have discovered it by accident โ I forgot to rewrite the url names and it was working anyway ๐
- [Django]-How do you use Django URL namespaces?
- [Django]-How can I set a default value for a field in a Django model?
- [Django]-Django makemigrations not detecting new model
5๐
Others have demonstrated the way to handle this with two separate named URL patterns. If the repetition of part of the URL pattern bothers you, itโs possible to get rid of it by using include():
url(r'^so/(?P<required>\d+)/', include('myapp.required_urls'))
And then add a required_urls.py file with:
url(r'^$', 'myapp.so', name='something')
url(r'^(?P<optional>.+)/$', 'myapp.so', name='something_else')
Normally I wouldnโt consider this worth it unless thereโs a common prefix for quite a number of URLs (certainly more than two).
- [Django]-Django template system, calling a function inside a model
- [Django]-How to debug Jinja2 template?
- [Django]-Django Local Settings
1๐
Why not have two patterns:
(r'^so/(?P<required>\d+)/(?P<optional>.*)/$', view='myapp.so', name='optional'),
(r'^so/(?P<required>\d+)/$', view='myapp.so', kwargs={'optional':None}, name='required'),
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-Django: Force select related?
- [Django]-How to create admin user in django tests.py
1๐
For anyone who is still having this problem.
I use Django 1.5 (updated: using 1.8) and itโs still working fine.
I use:
urls.py
url(r'^(?P<app_id>\d+)/start/+(?P<server_id>\d+)?', views.restart, name='restart')
Then when I want to have the two urls
/1/start/2
and
/1/start
I use:
{% url '<namespace>:start' app.id %}
{% url '<namespace>:start' app.id server.id %}
This will create the urls
/1/start/2 and
/1/start/ <- notice the slash.
If you create a url manually you have to keep the / in mind.
I hoop this helps anyone!
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-Create celery tasks then run synchronously
- [Django]-Writing unit tests in Django / Python
-1๐
in views.py you do simple thing.
def so(request, required, optional=None):
And when you dont get optional param in url string it will be None in your code.
Simple and elegant ๐
- [Django]-Django ModelForm has no model class specified
- [Django]-Django-rest-framework + django-polymorphic ModelSerialization
- [Django]-Received "ValueError: Found wrong number (0) of constraints for โฆ" during Django migration
-2๐
Depending on your use case, you may simply want to pass a url parameter like so:
url/?parameter=foo
call this in your view:
request.REQUEST.get('parameter')
this will return โfooโ
- [Django]-In django, how do I call the subcommand 'syncdb' from the initialization script?
- [Django]-How do I see the Django debug toolbar?
- [Django]-Django contrib admin default admin and password