重定向时/ NoReverseMatch

时间:2011-10-07 00:07:31

标签: python django

网址

url(r'^add/$', 'advice.views.add_new_advice'),
url(r'^$', 'advice.views.index'),
url(r'^(?P<advice_id>\d+)/$', 'advice.views.detail'),

观点

def add_new_advice(request):
    if request.method == "POST":
        form = AdviceForm(request.POST)
        if form.is_valid():
            new_advice = form.cleaned_data["advice"]
            advice = Advice(advice = new_advice)
            advice.save()
            return redirect('detail', advice_id = advice.id)
    else:
        form = AdviceForm()

    return render_to_response("create.html", {"form" : form},
                              context_instance=RequestContext(request))

def detail(request, advice_id):
    try:
        advice = Advice.objects.get(pk = advice_id)
    except Advice.DoesNotExist:
        return redirect('index')

    return render_to_response("detail.html", {"advice" : advice})

def index(request):
    advices = Advice.objects.all()
    advice = choice(advices)

    return redirect('detail', advice_id = advice.id)
嘿,我有这个问题。每当涉及到重定向到详细功能时,我都会在/获得NoReverseMatch。请帮忙。

NoReverseMatch at /
Reverse for 'detail' with arguments '()' and keyword arguments '{'advice_id': 7}' not found.

1 个答案:

答案 0 :(得分:0)

你没有调用任何"detail",所以它不会找到它。在urls.py中添加名称,使用函数的完整路径,或者传递函数本身而不是字符串。