【发布时间】: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.html在products文件夹
我认为 {% 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