Django - Adding {% url %} in template with slug

Viewed 5181

I am trying to create the URL in order to access the EDIT and DELETE views directly from the post detail instead of typing it in the browser.

I am having trouble finding the right url pattern and template {% url %} code since there is a slug.

posts.urls

urlpatterns = [
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', post_delete, name='delete'),

post_detail.html

{% block content %}
<div class='col-sm-6 col-sm-offset-3'>
{% if instance.image %}
<img src='{{ instance.image.url }}' class='img-responsive' />
{% endif %}
<h1>
{{ title }} 
<small>
  {% if instance.draft %}
    <span style='color:red;'>Draft</span> 
       {% endif %}{{ instance.publish }}  
    <div class=''>
    <a href="{% url 'update' %}"> Edit </a> |
    <a href="{% url 'delete' %}"> Delete</a> 
    </div>
  </small>
</h1>
1 Answers
Related