[英]passing multiple ids into url Django
嗨,我嘗試將用戶發送回html文件,但是當我在視圖函數中使用以下代碼時:
def delete_topic(request, topic_id):
""" Delete Topic """
topic = Topic.objects.get(id=topic_id)
topic.delete()
return HttpResponseRedirect(reverse('blogging_logs:topics'))
它給了我一個NoReverseMatch錯誤,我似乎無法弄清楚為什么。 我注意到我主題的查看功能需要category_id,但是我不確定如何傳遞多個ID。
這是我的主題視圖
def topics(request, category_id):
"""Show all topics for a single category"""
category = Category.objects.get(id=category_id) # get category that was requested
topics = category.topic_set.all() # get all topics associated with category that was requested
context = {'category': category, 'topics': topics}
return render(request, 'blogging_logs/category.html', context)
這是使用delete_topics def的html頁面。 topic.html
{% block content %}
<h3>{{ topic }}</h3>
{% for entry in entries %}
<p>{{ entry.text|linebreaks }}</p>
{% empty %}
<li>No entry entered yet!</li>
{% endfor %}
<a href="{% url 'blogging_logs:entry' topic.id %}"> Add entry </a>
<a href="{% url 'blogging_logs:delete_topic' topic.id %}">Delete Topic</a>
{% endblock content %}
我試圖在topic.html中的網址( delete_topic )中添加第二個id( category.id ),但這給了我一個錯誤。 我假設我的錯誤是它沒有獲得將用戶送回主題頁面所需的類別ID。
首先,您不需要多個ID,僅需要類別ID。
其次,您需要在Python代碼而不是模板中添加它:
return HttpResponseRedirect(reverse('blogging_logs:topics', args=(topic.category_id)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.