Thursday, February 25, 2016

URL namespaces


Assuming it can be done, what is the proper way to namespace two models in the same app, where both use slugs in the url but have different views and templates? 

In the docs, https://docs.djangoproject.com/en/1.9/topics/http/urls/#url-namespaces, it talks about how to do this, but it always refers to 'applications' rather than 'models'.


So can I do:

urls.py

url(r'^model1/', include('app.urls', appview4model1, namespace='app.model1')

url(r'^model2/', include('app.urls', appview4model2, namespace='app.model2')


app.urls.py

app_name = "app.model1" 

urlpatterns [ ....]

app_name = "app.model2"

urlpatterns [ ....]

Frankly, I want the shortest urls possible, so if there was a way to do this without putting 'model1' or 'model2' in the url, that would be great, but I'm not seeing a way around it - assuming the app_name = 'app.model1' thing even works. 

Another option I thought of but don't know if possible:

try url(r'^(?P<slug>) )
except url(r'^modelname(?P<slug>) )

I've never seen anything like that, though. If it did work, I doubt it would fit inside urlpatterns[ ], but maybe I could put it after?

Or I could hack the code that handles url name collisions (once I find it)


Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fdb1ce25-0840-49d3-ab79-3011128a7207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment