AlexBrand
AlexBrand

Reputation: 12409

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 be able to get the following url: /details/ from the defined url.

I tried {% url detail_view %}, but I get an error since I am not specifying the id parameter.

I need the url without the ID because I will be appending it using JS.

How can I accomplish this?

Upvotes: 6

Views: 3356

Answers (1)

ldiqual
ldiqual

Reputation: 15375

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'll need to specify that id is optional in your view function:

def view(request, id=None):

Upvotes: 6

Related Questions