Django urls.py 没有呈现正确的模板
Posted
技术标签:
【中文标题】Django urls.py 没有呈现正确的模板【英文标题】:Django urls.py not rendering correct template 【发布时间】:2015-08-22 06:19:36 【问题描述】:这是我的 urls.py
"""s7h URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^articles/', include('article.urls')),
url(r'^',include('django.contrib.auth.urls')),
url(r'^projects/', include('project.urls')),
#Serve Media Files
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
'document_root': settings.MEDIA_ROOT,
),
#subscribe
url(r'^subscribe/$', 'article.views.subscribe'),
url(r'^verify/$', 'article.views.activateSubscriber'),
# user auth urls
url(r'^home/$', 's7h.views.home'),
url(r'^', 's7h.views.home'),
url(r'^accounts/login/$', 's7h.views.login'),
url(r'^accounts/auth/$', 's7h.views.auth_view'),
url(r'^accounts/logout/$', 's7h.views.logout'),
url(r'^accounts/loggedin/$', 's7h.views.loggedin'),
url(r'^accounts/invalid/$', 's7h.views.invalid_login'),
# user registration urls
url(r'^accounts/register/$', 's7h.views.register'),
url(r'^accounts/register_success/$', 's7h.views.register_success'),
url(r'^accounts/register_auth/$', 's7h.views.register_auth'),
url(r'^contact/$', 's7h.views.contact'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
handler400 = 's7h.views.custom_400'
handler403 = 's7h.views.custom_403'
handler404 = 's7h.views.custom_404'
handler500 = 's7h.views.custom_500'
if not settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
这是我的观点.py
from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.contrib import auth,messages
from django.core.context_processors import csrf
from django.template import RequestContext
from django.template.loader import get_template
from forms import MyRegistrationForm
from article.models import Article, ArticleCategory
from project.models import Project, ProjectCategory
def home(request):
args=
article = Article.objects.earliest("-pub_date")
project = Project.objects.earliest("-pub_date")
article_category = ArticleCategory.objects.all()
project_category = ProjectCategory.objects.all()
args.update(csrf(request))
args['article'] = article
args['project'] = project
args['project_categories'] = project_category
args['article_categories'] = article_category
args['loggedin'] = request.session.get('email', None)
return render_to_response('home.html', args)
def login(request):
c =
c.update(csrf(request))
return render_to_response('login.html', c)
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
request.session['email'] = user.email
return render(request, "loggedin.html", locals(),context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/accounts/invalid/')
def loggedin(request):
return render_to_response('loggedin.html', )
def invalid_login(request):
return render_to_response('invalid_login.html')
def logout(request):
auth.logout(request)
return HttpResponseRedirect('/accounts/login/')
这是我的登录模板
% extends "user_forms.html" %
% block title %
S7H - Login
% endblock %
% block main %
<span class="fa fa-user bigicon"></span>
<h2>Enter Details</h2>
<form action = "/accounts/auth/" method = "POST">% csrf_token %
<p><input type="text" name="username" required placeholder="Username" autofocus></p>
<p><input type="password" name="password" required placeholder="Password"></p>
<button class="btn btn-default" type="submit" name="submit" />Sign In</button>
<a href="/accounts/register/" class="btn btn-default">Sign Up</a>
</form>
<small><a href="/password_reset/">Forgot your password?</a></small><br />
% if messages %
% for message in messages %
<small % if message.tags % class="message.tags" % endif %>message</small>
% endfor %
% endif %
% endblock %
现在,当我执行 http://127.0.0.1:8000/accounts/login/ 时,Django 应该呈现 login.html,因为 url accounts/login/ 引导我到 def login(request): 并且在 render_to_response() 中我提到了 login.html 来呈现但不是它,Django 呈现我的 home.html 文件。当我按下主页上的登录按钮时,浏览器的 URL 更改为 http://127.0.0.1:8000/accounts/login/,但它继续呈现相同的 home.html 模板。
这件事也发生在其他网址上。每个 url 都在渲染 home.html 模板。
我仔细检查了我所有的网址、视图和模板,但我无法找出我的错误。 Django 也没有给出任何异常和错误消息。
在终端中,HTTP 响应也是 200 OK :/
[08/Jun/2015 01:38:40]"GET /accounts/login/ HTTP/1.1" 200 10201
帮帮我!
【问题讨论】:
【参考方案1】:您的s7h.views.home
模式没有终止$
,因此它匹配每个网址。
【讨论】:
我想念它,就像我想念其他语言的分号一样。傻我:|以上是关于Django urls.py 没有呈现正确的模板的主要内容,如果未能解决你的问题,请参考以下文章
Django - 直接从 urls.py 渲染 HTML 模板