URLs stored in database for Django site

I've produced a few Django sites but up until now I have been mapping individual views and URLs in urls.py.

Now I've tried to create a small custom CMS but I'm having trouble with the URLs. I have a database table (SQLite3) which contains code for the pages like a column for header, one for right menu, one for content.... so on, so on. I also have a column for the URL. How do I get Django to call the information in the database table from the URL stored in the column rather than having to code a view and the URL for every page (which obviously defeats the purpose of a CMS)?

If someone can just point me at the right part of the docs or a site which explains this it would help a lot.

Thanks all.

13.10.2009 21:43:48
2 ОТВЕТА
РЕШЕНИЕ

You dont have to to it in the flatpage-way

For models, that should be addressable, I do this:

In urls.py I have a url-mapping like

  url(r'(?P<slug>[a-z1-3_]{1,})/$','cms.views.category_view', name="category-view")

in this case the regular expression (?P<slug>[a-z1-3_]{1,}) will return a variable called slug and send it to my view cms.views.category_view. In that view I query like this:

@render_to('category.html')
def category_view(request, slug):
    return {'cat':Category.objects.get(slug=slug)}

(Note: I am using the annoying-decorator render_to – it is the same as render_to_response, just shorter)

Edit This should be covered by the tutorial. Here you find the url-configuration and dispatching in every detail. The djangobook also covers it. And check pythons regex module.

Of course you can use this code.

5
14.10.2009 22:08:29
This was the kind of thing I was looking for. Using a 404 seemed a bit 'dirty'. This gets straight to the point. Can I ask where this type of URL mapping is documented? I've gone back through the official docs and the Django book and can't find it. Secondly, when my site is up and running do you mind if I use the above as an example in a small tutorial? Many thanks anyway.
adam.ec 14.10.2009 02:33:20

Your question is a little bit twisted, but I think what you're asking for is something similar to how django.contrib.flatpages handles this. Basically it uses middleware to catch the 404 error and then looks to see if any of the flatpages have a URL field that matches.

We did this on one site where all of the URLs were made "search engine friendly". We overrode the save() method, munged the title into this_is_the_title.html (or whatever) and then stored that in a separate table that had a URL => object class/id mapping.ng (this means it is listed before flatpages in the middleware list).

1
13.10.2009 22:42:30
How the flatpages works is what I meant. Is this how most CMS's work using Django? I wondered if there was a way to get Django to rewrite urls.py automatically when a page is written or if there are any other ways of mapping urls against pages stored in a database.
adam.ec 13.10.2009 23:04:56
URL mapping in Django falls into 2 categories: explicit (via the regexps in the urls.py files), and implicit (or perhaps 'inferred' is a better word). You only need to use the implicit method (which includes flatpage's 404 mechanism) if you (or your CMS) have some arbitrary set of rules of what URL will map to which page. I think it would be cleaner/cearer if there was a way to specify a series of 404 handlers in the urls.py files rather than through the semi-magical middleware method. I.E. to make the implicit method more explicit. I suppose you could do that with a regexp of '*'. Oh well...
Peter Rowell 14.10.2009 15:49:59