【问题标题】:Django 1.9: NoReverseMatchDjango 1.9:NoReverseMatch
【发布时间】:2016-02-19 06:44:17
【问题描述】:

我有一个基于类的视图,它显示模板中具有 {% extends 'base.html' %} 的数据。

我的基本文件夹位于名为 products

的单独文件夹中

我的应用网址

urlpatterns = [
     url(r'^contact/', views.contact, name='contact'),
     url(r'^product_list/', views.ProductViewList.as_view(), name='ProductViewList'),
]

基于类的视图模板 (product_list.html)

{% extends 'base.html' %}

{% block content %}
{{ object_list }}
{% endblock %}

我的base.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="{% static 'img/favicon.ico' %}">

    <title>Starter Template for Bootstrap</title>

    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
      <link href="{% static 'css/starter-template.css' %}" rel="stylesheet">

  </head>

  <body>

     {% include 'navbar.html' %}

        <div class="container">

          {% block content %}

          {% endblock %}

        </div><!-- /.container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="{% static 'js/jquery.min.js' %}"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="{% static 'js/ie10-viewport-bug-workaround.js' %}></script>
  </body>
</html>

当我访问“http://127.0.0.1:8000/product/product_list/”时弹出以下错误

我的目录结构,我的base.html根目录(模板),product_list.htmlproducts文件夹

认为 {% extends 'base.html' %} 可能有问题,但不知道如何解决问题......非常感谢任何帮助

这是我的第一个网址...进一步重定向到我的应用程序 url.py

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

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^product/', include('products.urls')),
        url(r'^accounts/', include('registration.backends.default.urls')),
    ]

【问题讨论】:

  • 试试http://127.0.0.1:8000/product_list/ ,因为你在urls.py中定义了^product_list/'而不是^product/product_list/'
  • 我已经有基本 url [' url(r'^product/', include('products.urls')),']..........重定向到产品/urls.py
  • 请提供您的 base.html 的完整代码。你是如何得到上述错误的?将直接链接放在地址栏上?你点击什么了吗?您是否在您的代码上指定了 {% url 'name' %} 之类的内容?
  • navbar.html 可能包含一个未映射到应用程序 url 模式的 {% url %} 标记
  • @spidy Aviah Laor 的意思是在 navbar.html 中有一个 {% url 'home' %} 标记导致此错误。因为您的 urls.py 中没有名为 home 的 urlpattern。在您的 url 中添加一个名为 home 的 urlpattern,或者从 navbar.html 中完全删除该标签。

标签: django django-templates django-views


【解决方案1】:

您需要为视图 url 指定一个名称,以标识您要在模板中使用的时间

from django.conf.urls import url
import myapplication.views

app_urlpatterns = [
     url(r'^article/(\d+)/$',views.article_detail,name='article-view-url'),
]

这就是我在 html 模板中使用 url 名称的方式

 {% for article in articles.object_list %}
        <article>
            <h3>
               <a href="{% url 'article-view-url' article.pk %}">{{ article.name }}</a>
            </h3>            
            <p>
                {{ article.desc }}
            </p>
        </article>
    {% endfor %}

【讨论】:

    猜你喜欢
    • 2014-02-10
    • 2017-05-21
    • 2013-09-26
    • 2013-01-19
    • 2016-07-20
    • 2017-04-18
    • 2015-07-18
    相关资源
    最近更新 更多