Monday, October 31, 2011

Dynamic URLs or creating querysets from your URL paths.

I'm creating a CMS using django-cms and some custom plugins. Everytime I do this I get sick or writing the same views time and time again.

Quick example, I have a Project model that can have one or more Categories, Tags and Clients:

class Project(...)
    categories = models.ManyToManyField(Category)
    tags = models.ManyToManyField(Tag)
    clients = models.ManyToManyField(Client)

I want the user to be able to browse my projects by a certain category, or tag or client. So my urls would be something like:

url(r'^projects/category/(?P<slug>[-w]+)/$', ...
url(r'^projects/partner/(?P<slug>[-w]+)/$', ...
url(r'^projects/client/(?P<slug>[-w]+)/$', ...

allowing me to see all projects for particular relationships, and maybe

url(r'^projects/category/$', ...
url(r'^projects/partner/$', ...
url(r'^projects/client/$', ...

to list all categories, partners and clients. 

This seems totally anti-DRY (WET?) to me. I'd like to be able to dynamically create the query based on what the URL path is, instead of having to manually link the URL paths to the correct queries via views as it stands at the moment. So retrieve the correct model based on what path is supplied - somewhat like a REST API call. Does anyone have any thoughts on this approach, or Is there anything out there that caters for this? 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/PSZyTu0TRk4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment