2๐
You can include myapp.urls
twice, using the same application namespace, but a different instance namespace:
url(r'^myapp_1/', include('myapp.urls', namespace='myapp1', app_name='myapp')),
url(r'^myapp_2/', include('myapp.urls', namespace='myapp2', app_name='myapp')),
Then, in your code, reverse the url using the application namespace, and pass the current instance namespace in the current_app
parameter:
reverse('myapp:rev', current_app=request.resolver_match.namespace)
When using the {% url %}
tag in a template, you cannot pass the current namespace directly, but you can set the current_app
attribute on the request for the same effect:
# in your view
request.current_app = request.resolver_match.namespace
# in your template
{% url 'myapp:rev' %}
In 1.9, this has slightly changed. You specify the app_name
in the included url configuration, and the namespace
in the call to include()
, like this:
mysite/urls.py
:
url(r'^myapp_1/', include('myapp.urls', namespace='myapp1')),
url(r'^myapp_2/', include('myapp.urls', namespace='myapp2')),
myapp/urls.py
:
app_name = 'myapp'
urlpatterns = [
url('^rev/$', views.rev, name='rev'),
]
Another change in 1.9 is that the {% url %}
template tag now uses the namespace of the current request by default. If you need to reverse an url in the same namespace, you no longer have to set request.current_app
explicitly. You still need to pass the current app if you use reverse()
.
0๐
Use namespaces in you urls.py.
Edit: The answers you linked do not look that wrong. What is the specific problem with these? I linked the docs for version 1.8, if there are differences.
- [Answered ]-Self-referencing class variable in Python
- [Answered ]-Django unexpected IntegrityError with PostgreSQL
- [Answered ]-Django | Auto Escape On / Off | Single Quotes | JSON
- [Answered ]-Templates Doesnot exist at /accounts/login