2013-02-17 59 views
0

我使用本教程http://lightbird.net/dbe/forum1.htmlDjango的錯誤反向爲{}「未找到

我遇到這個錯誤,我有製作簡單的論壇與參數「論壇」「(」‘)’和關鍵字參數」不知道到什麼do.Can你幫我解決這個錯誤

NoReverseMatch at /forum/post/reply/1/ 

    Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found. 

    Request Method: GET 
    Request URL: http://127.0.0.1:8000/forum/post/reply/1/ 
    Django Version: 1.4.3 
    Exception Type: NoReverseMatch 
    Exception Value: 

    Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found. 

    Exception Location: C:\Python26\Lib\site-packages\django\template\defaulttags.py in render, line 424 
    Python Executable: C:\Python26\python.exe 

    Error during template rendering 

    In template C:\djcode\mysite\forum\templates\forum\post.html, error at line 1 
    Reverse for 'forum' with arguments '('',)' and keyword arguments '{}' not found. 


    1  <a href="{% url ben:forum forum_pk %}">&lt;&lt; back to list of topics</a> 

我Post.html是

<a href="{% url ben:forum forum_pk %}">&lt;&lt; back to list of topics</a> 

我的看法是:

from django.core.urlresolvers import reverse 
from mysite.settings import MEDIA_ROOT, MEDIA_URL 
from forum.models import Forum 
from django.shortcuts import render_to_response 
from forum.models import Thread 
from django.core.paginator import Paginator, InvalidPage, EmptyPage 
from django.core.context_processors import csrf 
from forum.models import Post 

def main(request): 
    """Main listing.""" 
    forums = Forum.objects.all() 
    return render_to_response("forum/list.html", dict(forums=forums, user=request.user)) 
def forum(request, pk): 
      """Listing of threads in a forum.""" 
    threads = Thread.objects.filter(forum=pk).order_by("-created") 
    threads = mk_paginator(request, threads, 20) 
    return render_to_response("forum/forum.html", add_csrf(request, threads=threads, pk=pk)) 
def thread(request, pk): 
    """Listing of posts in a thread.""" 
    posts = Post.objects.filter(thread=pk).order_by("created") 
    posts = mk_paginator(request, posts, 15) 
    title = Thread.objects.get(pk=pk).title 
    return render_to_response("forum/thread.html", add_csrf(request, posts=posts, pk=pk, 
     title=title, media_url=MEDIA_URL)) 
def add_csrf(request, ** kwargs): 
    d = dict(user=request.user, ** kwargs) 
    d.update(csrf(request)) 
    return d 

def mk_paginator(request, items, num_items): 
    """Create and return a paginator.""" 
    paginator = Paginator(items, num_items) 
    try: page = int(request.GET.get("page", '1')) 
    except ValueError: page = 1 

    try: 
     items = paginator.page(page) 
    except (InvalidPage, EmptyPage): 
     items = paginator.page(paginator.num_pages) 
    return items 
def post(request, ptype, pk): 
    """Display a post form.""" 
    action = reverse("ben:%s" % ptype, args=[pk]) 
    if ptype == "new_thread": 
     title = "Start New Topic" 
     subject = '' 
    elif ptype == "reply": 
     title = "Reply" 
     subject = "Re: " + Thread.objects.get(pk=pk).title 

    return render_to_response("forum/post.html", add_csrf(request, subject=subject, 
    action=action, title=title)) 
def new_thread(request, pk): 
    """Start a new thread.""" 
    p = request.POST 
    if p["subject"] and p["body"]: 
     forum = Forum.objects.get(pk=pk) 
     thread = Thread.objects.create(forum=forum, title=p["subject"], creator=request.user) 
     Post.objects.create(thread=thread, title=p["subject"], body=p["body"], creator=request.user) 
    return HttpResponseRedirect(reverse("ben:forum", args=[pk])) 

def reply(request, pk): 
    """Reply to a thread.""" 
    p = request.POST 
    if p["body"]: 
     thread = Thread.objects.get(pk=pk) 
     post = Post.objects.create(thread=thread, title=p["subject"], body=p["body"], 
      creator=request.user) 
    return HttpResponseRedirect(reverse("ben:thread", args=[pk]) + "?page=last") 

我的URL:

from django.conf.urls import patterns,include,url 
from django.contrib import admin 
from django.conf import settings 

urlpatterns = patterns('forum.views', 
         url(r'^$','main',name='main'), 
         url("^forum/(\d+)/$", "forum",name ="forum"), 
         url("^thread/(\d+)/$","thread",name = "thread"), 
         url(r"^post/(new_thread|reply)/(\d+)/$", "post",name = "post"), 
         url(r"^reply/(\d+)/$", "reply" , name ="reply"), 
         url(r"^new_thread/(\d+)/$", "new_thread" , name ="new_thread"), 
) 
+0

我認爲你需要包括你的url conf。 – 2013-02-17 13:33:15

+0

和你的看法。 – 2013-02-17 13:33:38

+0

無論如何,你的'forum_pk'是一個空字符串,解決這個問題。 – 2013-02-17 13:37:05

回答

0

這有點混亂'('',)'意味着url參數列表由一個空字符串,彷彿模板是{% url ben:forum '' %}。沒有你的視圖代碼,就不可能知道它是如何發生的。

 

現在,你發佈的所有你的意見,我們仍然不知道哪一個是造成錯誤:)我們所知道的,不過,是他們沒有傳遞forum_pk背景變量render_to_response

 

是否post?你需要弄清楚論壇PK並把它傳遞到您的模板:

def post(request, ptype, pk): 
    """Display a post form.""" 
    action = reverse("ben:%s" % ptype, args=[pk]) 
    if ptype == "new_thread": 
     title = "Start New Topic" 
     subject = '' 
     forum_pk = pk 
    elif ptype == "reply": 
     title = "Reply" 
     thread = Thread.objects.get(pk=pk) 
     forum_pk = thread.forum.pk 
     subject = "Re: " + thread.title 

    return render_to_response("forum/post.html", add_csrf(request, subject=subject, 
     action=action, title=title, forum_pk=forum_pk)) 

我不同意這個觀點post設計,其中pk意味着不同的事情取決於ptype的。儘管如此,如果ptypenew_thread,我們已經有forum_pk作爲pk。如果是reply,我們需要得到Threadpk,並獲得線程論壇的pkforum_pk

+0

好的我發佈了我的代碼。感謝pavel幫助 – supersheep1 2013-02-17 13:44:16

+0

如何修復forum_pk – supersheep1 2013-02-17 13:50:37

+0

編輯我的文章。 – 2013-02-17 14:01:42

0

除了增加forum_pk = pk

我調整了一個答覆:

forum_pk = Thread.objects.get(PK = PK).forum.pk