0

I am trying to create urlpatterns with sql query, but this will work only for those things that already was in sql table at the moment of server start. If it possible to make django to check for new urls dynamically from database?

I know, this can be done with regular expressions, but they are too greedy, I mean, that i need to make this at root level of my site and regexp will "eat" all matching names and this regexp must be the last of urlpatterns list.

Hikaru
  • 111
  • 8

3 Answers3

2

Going on your comment to pyeleven's answer, it seems you have understood the point of urlpatterns. You don't need or want to specify the choices of your section in the urlconf. What you do is grab the value of each section of the url, and pass that as a parameter to the view. So, for example:

(r'^?P<section>\w+)/$', 'my_view')

This will grab urls like /name1/ and /name2/, and pass name1 and name2 to the view as the section parameter. So there's no need to change the code whenever you add a section.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • No, this is not what I want to achieve. This will just send to "my_view" named group, like section="name1". I want to avoid regexp in urlpatterns (or may be limit the scope of regexp, but i don't want to move it one level deeper, like "domain.com/major/name1") – Hikaru Mar 23 '11 at 13:50
  • This really seems like the correct approach. If you're worried about this regex 'eating' everything, just place it below all your other urls so that they match first. Then, if the section isn't in the DB, just raise 404 from your view. – dgel Mar 23 '11 at 16:03
  • One downside to this approach is that unless you qualify these URLs with some kind of prefix then the rule will always succeed. One consequence to this that I've found is that the APPEND_SLASH variable won't have any effect since it only kicks in once the URL matching has failed. Anyone know a way round this? Or any other disadvantages for that matter? – nedned Jun 29 '11 at 09:10
2

Although this is the nastiest, most un-django-esque thing imaginable, you can get your urls from the db, if you really, really want to:

models.py:

from django.db import models

class Url(models.Model):
    name = models.CharField(max_length=20)

urls.py:

from my_app.models import Url

urls = []
for url_object in Url.objects.all():
    urls.append(url(url_object.name, 'my_view'))

urlpatterns = patterns('my_app.views', *urls)

Voilà. It actually works. Url patterns directly from the db. Please don't do this.

I'm going to go take a shower now.

dgel
  • 16,352
  • 8
  • 58
  • 75
  • One caveat though- I believe you would have to restart your process everytime there is a new url in the db though, as I believe urls.py is only parsed once at startup. This really isn't a good idea. – dgel Mar 23 '11 at 16:44
  • Ok, thank you. Can you tell me, why it is bad idea to get url from db? – Hikaru Mar 24 '11 at 06:31
  • Well, like I said- I think this code would only be parsed once on startup, so if you add a url to the db it wouldn't take effect until you restart the django process. Doing what Daniel suggested accomplishes the _exact_ same thing, but in the way django was designed to be used. – dgel Mar 24 '11 at 07:01
  • Ok, thank you for your reply's! Seems to me Daniel's way is the best one. – Hikaru Mar 24 '11 at 08:20
0

Have you checked django flatpages?

http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/?from=olddocs

Dynamic url might not be such a good idea, for example a bad url line added dynamically might make the server stop functioning.

Can you elaborate on your goals?

endre
  • 1,363
  • 1
  • 11
  • 22
  • I want to divide site into major categories, and if you know from the beginning what categories you will use - there is no problem, but if you don't know you need to find some way to add them, and i think that editing site's source code every time you are adding major category is bad idea. So, I am trying to get major categories from sql database: it will allow easily add them through admin interface (or even delete) I want to get urls, like urlpatterns = patterns('', (^name1/$, major_page), (^name2/$, major_page), ) Yup, they should lead to the same view where i will deal them – Hikaru Mar 23 '11 at 12:55