开发者

NoReverseMatch at / when redirecting

开发者 https://www.devze.com 2023-04-11 02:40 出处:网络
urls url(r\'^add/$\', \'advice.views.add_new_advice\'), url(r\'^$\', \'advice.views.index\'), url(r\'^(?P<advice_id>\\d+)/$\', \'advice.views.detail\'),

urls

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

views

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)

Hey, I've got this problem. Whenever it comes to redirecting to detail function I get NoReverseMatch at /. Please help.

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


You didn't call anything "detail" so it's not going to find it. Either add the name in your urls.py, use the full path to the function, or pass the function itself instead of a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消