Django“找不到带有关键字参数'{'pk':``}''的'question_update'的反向。”错误

时间:2019-08-03 08:47:20

标签: django

NoReverseMatch位于/ answer_update / 1/16 找不到带有关键字参数'{'pk':''}'的'question_update'。尝试了1个模式:['问题\ /(?P [0-9] +)\ /更新\ / $']

我正在尝试添加评论更新功能。但这是行不通的。 有人请帮帮我。

urls.py

path('answer_update/<int:qpk>/<int:apk>',views.answer_update, name='answer_update'),

views.py

def answer_update(request, qpk, apk):
answer = get_object_or_404(Answer, pk=apk)
question = get_object_or_404(Question, pk=qpk)

if request.method == "POST":
    form = AnswerForm(request.POST, instance=answer)
    if form.is_vaild():
        form.save()
        return redirect('detail_question', pk=apk)
else:
    form = AnswerForm(instance = answer)
return render(request, 'detail_question.html',{'form':form})

detail_question.html

<div>
<h1>Answer_list</h1>
<div>
    {% for answer in answers %}
        <div>
            {% if answer.image %}
                <img src="{{ answer.image.url}}" height=200>
            {% endif %}
            <p> {{ answer.content }} </p>
            <p> {{ answer.author }} </p>
            {% if user == answer.author %}
                <a href="{% url 'answer_update' question.pk answer.pk %}">update</a>
            {% endif %}
            {% if user == answer.author %}
                <a href="{% url 'answer_remove' question.pk answer.pk %}">delete</a>
            {% endif %}

            {% if user == question.author and question.selected == False %}
                <a href="{% url 'select_question' question.pk answer.pk %}">select</a>
            {% endif %}
            <hr>
        </div>
    {% endfor %}
</div>

1 个答案:

答案 0 :(得分:0)

  

返回渲染(request,'detail_question.html',{'form':form})

您没有将“ answers”变量传递给'detail_question.html'模板

您传递的唯一上下文是{'form':form}

它似乎是表单视图,但是模板用于列表视图...