【问题标题】:Django - Exception Type: NoReverseMatchDjango - 异常类型:NoReverseMatch
【发布时间】:2016-01-27 21:02:40
【问题描述】:

大家好,我正在学习 django,但我遇到了这个错误,我不知道如何解决它,你能帮我吗? python 3.x / django 1.9

我收到此错误:

NoReverseMatch at /
Reverse for 'categoria' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.9.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'categoria' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\Yiyeh\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 508
Python Executable:  C:\Users\Yiyeh\AppData\Local\Programs\Python\Python35-32\python.exe
Python Version: 3.5.1
Python Path:    
['C:\\Users\\Yiyeh\\Documents\\Ejercicios\\Python\\puls\\puls',
 'C:\\Users\\Yiyeh\\AppData\\Local\\Programs\\Python\\Python35-32\\python35.zip',
 'C:\\Users\\Yiyeh\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
 'C:\\Users\\Yiyeh\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
 'C:\\Users\\Yiyeh\\AppData\\Local\\Programs\\Python\\Python35-32',
 'C:\\Users\\Yiyeh\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages']
Server time:    Wed, 27 Jan 2016 17:52:56 -0300

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
from datetime import datetime
from app.models import *

这是我的观点.py

def home(request):
    categorias = Categoria.objects.all()
    enlaces = Enlace.objects.all()
    template = "index.html"
    #diccionario = {"categorias": categorias,"enlaces":enlaces}
    return render_to_response(template,locals())

urls.py

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 'app.views.home', name='home'),
]

和 index.html(第 30 行错误)

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8" />

    <meta name="viewport" 
          content="width=device-width, initial-scale=1, maximum-scale=1" />

    <title>Puls3: Comunidad online de gente pro y (no sé que más escribir)</title>
    <link rel="stylesheet" href="normalize.css" />
    <link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="/static/estilos.css" />
    <link rel="stylesheet" href="/static/responsive.css" />
</head>
<body>
    <!-- comentario de lo que sea -->
    <header>
        <figure id="logo">
            <img src="logo.png" />
        </figure>
        <h1 id="titulo_header">Puls3: Comunidad de gente cool, atractiva y millonaria</h1>
        <figure id="avatar">
            <img src="avatar.jpg" />
            <figcaption></figcaption>
        </figure>
    </header>
    <nav>
        <ul>
            <li id="flechita_nav"><a href="/"></a></li>
            {% for cat in categorias %}
                <li><a href="{% url "categoria" cat.pk %}">{{cat}}</a></li>
            {% endfor %}
            <li id="publicar_nav"><a href="{% url "add" %}">Publicar</a></li>
        </ul>
    </nav>
    <aside id="bienvenida">
        {% if not request.user.is_authenticated %}
        <h2>Hola, registrate!</h2>
        <p>Es importante registrarte porque LOL!</p>
        <a id="registro" href="#">Registrate acá</a>
        <p id="mensaje_registro">En serio, registrate por favor</p>
        {% endif %}
    </aside>
    <section id="contenido">
        {% if enlaces %}

        {% for enlace in enlaces %}
        <article class="item">
            <figure class="imagen_item">
                <img src="imagen.jpg" />
            </figure>
            <h2 class="titulo_item">
                <a href="{{enlace.enlace}}">{{enlace.titulo}}</a>
            </h2>
            <p class="autor_item">
                Por <a href="#">{{enlace.usuario}}</a>
            </p>
            <p class="datos_item">
                <a class="tag_item" href="#">{{enlace.categoria}}</a>
                <span class="fecha_item">Hace <strong>{{enlace.timestamp|timesince}}</strong> min</span>
                <a class="guardar_item" href="#"></a>
            </p>
            <p class="votacion">
                <a class="up" href="{% url "plus" enlace.pk %}"></a>
                {{enlace.votos}}
                <a class="down" href="{% url "minus" enlace.pk %}"></a>
            </p>
        </article>
        {% endfor %}
        {% else %}
            NO HAY ENLACES GUARDADOS
        {% endif %}
    </section>
    <footer>
        <p><strong>Powered by Platzi!</strong></p>
        <p>Mejorando.la 2013 ®</p>
    </footer>
</body>
</html>

【问题讨论】:

  • 您没有类别的网址格式
  • 我不知道是什么让你使用locals(),但这是一个坏习惯。您应该使用已注释掉的显式字典。
  • 我尝试使用 diccionario = {"categorias": categorias,"enlaces":enlaces} 但我得到了同样的错误

标签: python django exception types reverse


【解决方案1】:

您的问题是您的模板正在尝试创建超链接,但您在 urls.py 文件中没有相应的条目。每当您创建链接时(例如,href="{% url 'categoria' cat.pk %}" django 将在 urls.py 中查找名称为 urlcategoria,它带有一个参数 - 在这种情况下,这将是主键。所以让它工作,你必须有一个看起来像这样的条目:

urlpatterns = [
    # other url entries...
    url(r'^categoria/([0-9]+)/$', views.categoria_view, name='categoria'),
]

name 参数用于反转 url。更多文档here

请注意,您必须为在 {% url %} 标签中使用的所有名称创建条目(例如,类别、加号、减号、加号)。

【讨论】:

  • 谢谢你的提示,让我免了痛苦。
猜你喜欢
  • 1970-01-01
  • 2019-09-08
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2010-11-15
  • 2017-09-07
相关资源
最近更新 更多