Skip to main content
 首页 » 编程设计

python之Django之异常类型 : NoReverseMatch

2025年05月04日21kevingrace

大家好,我正在学习 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 * 

这是我的view.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()) 

网址.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> 

请您参考如下方法:

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

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

name 参数在反转 url 时使用。文档中有更多信息 here

请注意,您必须为 {% url %} 标签中使用的所有名称创建条目(例如,categoria、plus、minus、add)。