Django: runtime URL configuration

If there is a painless way to modify urlconf at runtime? for example based on database records?

Disposal of Bruteforce:

r('^(.*)/', handmade_router_function) 

too cruel for me :)

Thanks in advance!

UPD: I understand that I can directly modify urlpatterns from my code, but this requires a lot of manual coding (user actions for saving, deleting, etc.). and I want to find out if there is a ready-to-use application / library :)

+4
source share
3 answers

Django has a Contrib application that already does this, it's called FlatPages. It works by registering middleware. When a page is requested, if it is not found, it throws 404, which is captured by middleware. Middleware scans a page in the database if it finds that it serves the page, and if it does not throw 404.

+2
source

Have you tried changing the urlpatterns variable at run time?

0
source

Something like this works. You will repeat something else, for example, model instances, as you mentioned, but the premise is the same:

 for path in ["foo", "bar"]: urlpatterns += patterns("myapp.views", url(r"^%s/$" % path, "index", {}, name=path)) 

I posted this code in my urls.py This leads to a display of the following form:

 http://127.0.0.1:8000/foo/ http://127.0.0.1:8000/bar/ 

Some notes:

  • I'm not sure what your views are, so this just calls up a view called index .
  • I name the URLs after their corresponding path argument; You can choose them in different ways.
0
source

All Articles