, DetailView.as_view(), name='detail_view...">

Django {% url%} tag without parameters

I have a url defined as follows:

url(r'^details/(?P<id>\d+)$', DetailView.as_view(), name='detail_view'),

In my templates, I want to get the following URL: /details/from the given URL.

I tried {% url detail_view %}, but I get an error, because I do not set the parameter id.

I need a url without an identifier, because I will add it using JS.

How can i do this?

+5
source share
1 answer

Just add this line to your urls.py:

url(r'^details/$', DetailView.as_view(), name='detail_view'),

or

url(r'^details/(?P<id>\d*)$', DetailView.as_view(), name='detail_view'),

(This is a cleaner solution - thanks to Thomas Orozco)

You need to specify what idis optional in your view function:

def view(request, id=None):
+6
source

All Articles