Dynamic URLs or creating querysets from your URL paths.

82 views
Skip to first unread message

Timmy O'Mahony

unread,
Oct 31, 2011, 12:51:00 PM10/31/11
to django...@googlegroups.com
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? 

Timmy O'Mahony

unread,
Oct 31, 2011, 1:04:38 PM10/31/11
to django...@googlegroups.com
The more I think about this the more I realize that what I'm talking about IS exactly a REST api.

If I write a phone app, I will code all the presentation layer locally on the phone (client-side) and use json/xml to dynamically fetch data from the server. 

Why isn't this the same for websites? - why isn't the presentation layer completely client-side as opposed to 'a bit of both'?

Ideally when a user requests a webpage from my server, I should deliver the presentation layer to them, along with the information they require to perform an API lookup. From then on they can query my server using the same REST api a phone app would use. This would mean that all the information from EVERY device accessing my website comes from the same api. This is much better then my existing setup, where I might have an API for an app to accompany my site, but I also have a jungle of urls and views to enable a web browser use my site

Am I just repeating/realizing something that is well know and catered for? 

Kurtis Mullins

unread,
Oct 31, 2011, 1:22:20 PM10/31/11
to django...@googlegroups.com
Sure you could do that. It's called using XML Transformations (XSLT), Javascript Templatating Engines, or a number of other approaches. 

Django does this with its Template Engine. The reason this isn't completely ran on the Client-Side is because it would be slower, difficult to cache, and dependent upon some specific Client-Side technologies.

Django lets you easily override this behavior. For example, there's documentation on using alternative Templating Engines. You could apply the same methodology to using Client-Side Templating Engines (written in Javascript), or even just displaying XML w/ XSLT using the Browser's Transformation Engine. I'm not really the most experienced person when it comes to Client-Side templates but I'm sure there's a lot out there in Google Land.

--
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/-/f1xLB5KuZQAJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages