【问题标题】:NoReverseMatch Django for a forumNoReverseMatch Django 论坛
【发布时间】:2015-09-19 03:10:24
【问题描述】:

在 /forum/ 上的 NoReverseMatch

未找到带有参数 '('',)' 和关键字参数 '{}' 的 '' 的反向操作。尝试了 0 个模式:[]

使用教程:http://www.lightbird.net/dbe/forum1.html

我正在处理 list.html——我不确定这是否是放置此代码的正确位置:

{% extends "base.html" %}

{% block content %}

<!-- Forums -->
<div id="list">
  <table border="0" cellpadding="4" width="100%">
    <tr>
      <td></td>
      <td>Posts</td>
      <td>Last post</td>
      <td></td>
    </tr>
    {% for forum in forums %}
    <tr>
      <td {% if forloop.last %} class="last" {% endif %}>
        {{ forum.title }}
      </td>
      <td {% if forloop.last %} class="last" {% endif %}>
        {{ forum.num_posts }}
      </td>
      <td {% if forloop.last %} class="last" {% endif %}>
        {{ forum.last_post.short|linebreaksbr }}
      </td>

      <td {% if forloop.last %}class="last" {% endif %}>
        <a class="button" href="{% url forum.views.forum forum_pk %}">
          VIEW</a>
      </td> -->

    </tr>
  </table>
  {% endfor %}
</div>
{% endblock %}

我的看法:

from django.shortcuts import render, render_to_response
from django.core.urlresolvers import reverse
from dogslikeme.settings import MEDIA_ROOT, MEDIA_URL
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.context_processors import csrf

from .models import *

# Create your views here.
def main(request):
    """Main listing"""
    forums = Forum.objects.all()
    return render_to_response('forum/list.html', {'forums':forums,
        'user':request.user})

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"))
    except ValueError: page = 1

    try:
        items = paginator.page(page)
    except(InvalidPage, EmptyPage):
        items = paginator.page(paginator.num_pages)
    return items

def forum(request, pk):
    """Listing of threads in a forum"""
    threads = Threads.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
    t = Thread.objects.get(pk=pk)
    return render_to_response('forum/thread.html', add_csrf(request,
        posts=posts, pk=pk, title=title, media_url=MEDIA_URL,
        forum_pk=t.forum.pk))

def post(request, ptype, pk):
    """Display a post form."""
    action = reverse("forum.views.%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, forum_pk=forum_pk))

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("forum.views.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("forum.views.thread",
        arg=[pk]) + "?page=last")

和 urls.py:

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

from . import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'forum.views.main'),
    url(r'^forum(\d+)/$', 'forum.views.forum'),
    url(r'^thread/(\d+)/$', 'forum.views.thread'),

    url(r'^post/(new_thread|reply)/(\d+)/$', 'forum.views.post'),
    url(r'^reply/(\d+)/$', 'forum.views.reply'),
    url(r'^new_thread/(\d+)/$', 'forum.views.new_thread'),
]

但是,在这一行出现了无反向匹配错误: 在 html 中。我不知道 forum_pk 应该来自哪里或为什么会发生错误。我尝试更新视图中的 main 以包括 forum.pk 但我不确定它应该来自哪里。

我也不清楚应该使用哪些 html 页面来学习本教程,以及是否应该将其分开。

【问题讨论】:

    标签: django django-templates django-urls


    【解决方案1】:

    在你的模板上,试试这个:

    <a class="button" href="{% url 'forum.views.forum' forum.pk %}">
    

    forum_pk 替换为forum.pk 并将第一个参数括在引号中:'forum.views.forum'

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2016-04-14
      • 2019-11-02
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      相关资源
      最近更新 更多