[Fixed]-Django dynamic urls


You can use named groups in the urls to pass data to views and it won’t require any dynamic updating in the urls. The named part containing page.alias will be simply passed as a keyword argument to your view function. You can use it to get the actual Page object.

# urls.py
urlpatterns += patterns('',
   (r'^(?P<page_alias>.+?)/$', 'views.static_page'),

# views.py
def static_page(request, page_alias):    # page_alias holds the part of the url
        active = Page.objects.get(page_alias=page_alias)
    except Page.DoesNotExist:
        raise Http404("Page does not exist")


You don’t need a specific URL for each item in your entire database.

Without seeing your view, I would guess you can get away with one URL, or maybe a few url’s.

As an example:

urlpatterns = patterns('yourapp.views',
url(r'^static_pages/(?P<static_pages_id>\d+)/(?P<True_or_False>\D+)$', your_view_here, name='your_name_here'),

def your_view_here(request, static_pages_id, True_or_False):
    obj = get_object_or_404(Page, pk=static_pages_id)
    if True_or_False:
        #do something when True

Leave a comment