我正在尝试创建URL,以便直接从帖子详细信息访问EDIT和DELETE视图,而不是在浏览器中输入。
我无法找到正确的网址格式和模板{%url%}代码,因为有一个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>
答案 0 :(得分:4)
您需要将slug传递给html中的url标记。
尝试这样的事情,
<a href="{% url 'update' slug=instance.slug %}"> Edit </a>
<a href="{% url 'delete' slug=instance.slug %}"> Delete</a>