cr1
cr1

Reputation: 199

Django pass variable to URL tag

I am using Django 1.11. I am trying to add a link to a DeleteView template from the UpdateView. There is probably a better way to acheive this than the way I am attempting but I am new to Django and so the way i'm trying is to use a URL to direct to myapp/<pk>/delete/

In my template I have {% url 'calendar_delete' pk=event_id %}

In my model I have

class Event(models.Model):
    event_id = models.AutoField(primary_key=True)

In my URLs I have

url(r'^calendar/(?P<pk>\d+)/delete/$',views.CalendarDelete.as_view(),name='calendar_delete'),

With the code as above the template doesnt render due to NoReverseMatch exception. '{'pk': ''}' not found, so its obviously not picking up the event_id from the model. When I hardcode a number in there it will render and the URL will direct me to the DeleteView template.

Can anyone advise how I can get the <pk>/event_id into the URL tag?

The <pk> exists in the URL of the UpdateView template and I have also tried to extract it from the URL as a variable and pass to the URL tag. I can extract ok but not pass in the variable.

Any help on this would be much appreciated, including advice on better methods to use for the desired outcome than I am heading down! Cheers!

Upvotes: 1

Views: 1201

Answers (1)

Alasdair
Alasdair

Reputation: 309099

In an UpdateView, you should be able to access object in the template.

{% url 'calendar_delete' pk=object.pk %}

Upvotes: 1

Related Questions