我一直在关注djangogirls' tutorial创建一个django的博客几乎没有遇到任何麻烦,但现在我继续使用他们的extensions tutorial继续创建的Web应用程序在第一个问题上,当我试图了解某个帖子的详细信息时,我会在标题中出现错误。之前这个帖子工作正常。
这里有详细的错误消息
环境:
请求方法:GET请求网址:http://127.0.0.1:8000/post/6/
Django版本:1.10.6 Python版本:3.5.2已安装的应用程序: [' django.contrib.admin',' django.contrib.auth', ' django.contrib.contenttypes',' django.contrib.sessions', ' django.contrib.messages',' django.contrib.staticfiles','博客'] 已安装的中间件: [' django.middleware.security.SecurityMiddleware&#39 ;, ' django.contrib.sessions.middleware.SessionMiddleware&#39 ;, ' django.middleware.common.CommonMiddleware&#39 ;, ' django.middleware.csrf.CsrfViewMiddleware&#39 ;, ' django.contrib.auth.middleware.AuthenticationMiddleware&#39 ;, ' django.contrib.messages.middleware.MessageMiddleware&#39 ;, ' django.middleware.clickjacking.XFrameOptionsMiddleware']
模板错误:在模板中 /home/frank/Desarrollo/python/devdjango/djangogirlstut/blog/templates/blog/post_detail.html, 第14行的错误反转' post_new'参数'()'和 关键字参数' {}'未找到。尝试了0种模式:[] 4:
5:{%if post.published_date%} 6: 7:{{post.published_date}} 8:
9:{%else%} 10:发布11:
{%endif%} 12:{%if user.is_authenticated%} 13:14:15:{% endif%} 16:{{post.title}}
17:
{{post.text | linebreaksbr}}
18:19:{% endblock%} 20:
回溯:
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/exception.py" 在内心 42. response = get_response(request)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/base.py" 在_get_response中 187. response = self.process_exception_by_middleware(e,request)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/base.py" 在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/blog/views.py" 在post_detail 15. return render(request,' blog / post_detail.html',{' post':post})
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/shortcuts.py" 在渲染中 30. content = loader.render_to_string(template_name,context,request,using = using)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/loader.py" 在render_to_string中 68. return template.render(context,request)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/backends/django.py" 在渲染中 66. return self.template.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在渲染中 208. return self._render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在_render 199. return self.nodelist.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在渲染中 994. bit = node.render_annotated(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在render_annotated中 961. return self.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/loader_tags.py" 在渲染中 174. return compiled_parent._render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在_render 199. return self.nodelist.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在渲染中 994. bit = node.render_annotated(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在render_annotated中 961. return self.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/defaulttags.py" 在渲染中 315. return nodelist.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在渲染中 994. bit = node.render_annotated(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" 在render_annotated中 961. return self.render(context)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/defaulttags.py" 在渲染中 439. url = reverse(view_name,args = args,kwargs = kwargs,current_app = current_app)
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/base.py" 相反 91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view,prefix, * args,** kwargs)))
文件 " /home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/resolvers.py" 在_reverse_with_prefix中 392.(lookup_view_s,args,kwargs,len(模式),模式)
异常类型:NoReverseMatch at / post / 6 / Exception Value:Reverse 为了'post_new'参数'()'和关键字参数' {}'不 找到。尝试了0种模式:[]
网站urls.py文件
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
url(r'', include('blog.urls', namespace='blog')),
]
blog / urls.py文件(博客是应用名称)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^post/new/$', views.post_new, name='post_new'),
url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),
]
违规模板blog / templates / blog / post_detail.html
{% extends 'blog/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% else %}
<a class="btn btn-default" href="{% url 'blog:post_publish' pk=post.pk %}">Publish</a>
{% endif %}
{% if user.is_authenticated %}
<a class="btn btn-default" href="{% url 'blog:post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-default" href="{% url 'blog:post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endblock %}
以及它从
扩展的基本模板{% load staticfiles %}
<html>
<head>
<title>Django Girls blog</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<div class="page-header">
{% if user.is_authenticated %}
<a href="{% url 'blog:post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
<a href="{% url 'blog:post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
<p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>
{% else %}
<a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
{% endif %}
<h1><a href="/">Django Girls Blog</a></h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
我尝试过命名空间,我没有更改urls文件中的正则表达式(即它们与之前的工作相同),django版本还可以(1.10)...我读过this other answer about the NoReverseMatch但没有雪茄。
关于我可能在哪里出错的任何想法?
测试int bash:
from django.urls import reverse
reverse('blog:post_new') Traceback (most recent call last): File "<stdin>", line 1, in <module> File
"/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/base.py",
line 33, in reverse
resolver = get_resolver(urlconf) File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/resolvers.py",
line 67, in get_resolver
urlconf = settings.ROOT_URLCONF File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/conf/__init__.py",
line 53, in __getattr__
self._setup(name) File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/conf/__init__.py",
line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting
ROOT_URLCONF, but settings are not configured. You must either define
the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.
答案 0 :(得分:0)
blog
应用位于名称空间blog
下。对此应用的网址的每次引用都应采用namespace:url_name
。
因此,您应该在{% url %}
模板标记内使用命名空间。您在违规模板中正确执行此操作,您也应该在基本模板中执行相同的操作。
所以在base.html
内(以及其他任何blog
网址都涉及):
<!-- base.html -->
{% url 'blog:post_new' %}
{% url 'blog:post_draft_list' %}
保持{% url 'login' %}
和{% url 'logout' %}
不变。它们不是命名空间。