【问题标题】:How to pass slug value to a template?如何将 slug 值传递给模板?
【发布时间】:2017-09-20 09:05:31
【问题描述】:

我可以访问 URL 中的username1/edit,但无法通过单击模板中的链接来访问。

urls.py

url(r'^(?P<slug>[-\w\d]+)/edit/$', ProfileEdit.as_view(), name='profile_edit'),

template.html

<a href="{% url 'profile:profile_edit' %}">edit</a>

我收到以下错误

未找到带有参数“()”和关键字参数“{}”的“profile_edit”的反向操作。尝试了 1 种模式:['(?P[-\w\d]+)/edit/$']

我得到的问题是没有将 slug 值发送到 url。但是如何从模板发送 slug 值?

【问题讨论】:

  • 如错误所示,您没有将 slug 传递给 url 标签。
  • 我明白了!我该如何克服这个错误?

标签: python django django-templates


【解决方案1】:

问题的答案:

<a href="{% url 'profile:profile_edit' slug=profile.slug %}">Edit</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 2021-07-26
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多