简体   繁体   English

Django模板不显示

[英]Django template not displaying

I have a template for an about page which refuses to show. 我有一个关于页面的模板,拒绝显示。 I'm probably doing something silly but I can't work out why and it's driving me insane! 我可能在做一些愚蠢的事情,但是我无法弄清楚为什么,这让我发疯了!

part of views.py: views.py的一部分:

# About view
def about(request):
    return render(request, 'blog/about.html')

urls.py: urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog.views import post as blog_post
from blog.views import profile as blog_profile
admin.autodiscover()
urlpatterns = patterns('',

url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(r'^about/$', 'blog.views.about'),
url(r'^profiles/$', 'blog.views.profile_index'),
url(r'^profiles/(?P<profile_url>[\w\-]+)/$', blog_profile, name = 'blog_profile'),
url(r'^(?P<category>[\w\-]+)/$', 'blog.views.categoryIndex'),
url(r'^(?P<category>[\w\-]+)/(?P<slug>[\w\-]+)/$', blog_post, name = 'blog_post')
)

the about template (not including the base.html): 关于模板(不包括base.html):

{% extends 'base.html' %}
{% block title %} About {% endblock %}
{% block content %}
<h1>About</h1>
{% endblock %}

Using Django 1.6.5 使用Django 1.6.5

Tried to navigate to mysite.com/about/ 试图浏览至mysite.com/about/

Template hierachy: 模板层次结构:

templates
    base.html
    blog
        about.html
        ....

If your templates dir is at the root level of the project, you might want to add the following to the settings.py 如果您的模板目录位于项目的根目录下,则可能需要将以下内容添加到settings.py

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

I've figured out the problem, the following url was being matched with the about page instead: 我已经解决了这个问题,以下网址却与About页面匹配:

url(r'^(?P<category>[\w\-]+)/$', 'blog.views.categoryIndex'),

Removing this solves the issue and displays the about page. 删除它可以解决问题并显示关于页面。 This page explains about url ordering http://www.webforefront.com/django/regexpdjangourls.html 本页说明有关url订购的信息http://www.webforefront.com/django/regexpdjangourls.html

Because my about url was listed before the conflicting one, it was skipped. 由于我的About URL在冲突的URL之前列出,因此被跳过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM