Django模板文件夹

Django模板文件夹,django,django-templates,django-urls,Django,Django Templates,Django Urls,我正在试用Django,并了解如何设置url.py,以及URL是如何工作的。 我在项目的根目录中配置了url.py,以指向我的博客和管理员。 但是现在我想在我的主页上添加一个页面,所以在localhost:8000 因此,我将以下代码添加到项目根目录中的url.py: from django.views.generic.simple import direct_to_template urlpatterns = patterns('', (r"^$", direct_to_templa

我正在试用Django,并了解如何设置url.py,以及URL是如何工作的。 我在项目的根目录中配置了url.py,以指向我的博客和管理员。 但是现在我想在我的主页上添加一个页面,所以在
localhost:8000

因此,我将以下代码添加到项目根目录中的url.py

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r"^$", direct_to_template, {"template": "base.html"}),
)
问题是它在blog/templates/… 而不是我根目录中的templates文件夹。其中包含base.html

完整的URL.py

from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    (r"^$", direct_to_template, {"template": "base.html"}),
    url(r'^blog/', include('hellodjango.blog.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
)

我是否忽略了什么?

我会重新组织URL,如下所示:

urlpatterns = patterns('',
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
    (r'^blog/', include('hellodjango.blog.urls')),
    (r'^$', direct_to_template, {"template": "base.html"}),
)

模式是由它们的特异性来匹配的,所以我倾向于把更具体的模式放在第一位。否则,您可能会看到一些意外的行为。试一试,如果它仍然根据
/
的请求从您的博客加载模板,我们将深入挖掘。

您是否在
设置.py中设置了
模板目录?检查并确保绝对路径设置正确。这是我如何确保正确设置的:

设置.py 这样,我的项目根目录中有一个
templates
文件夹,用于非应用程序模板,每个应用程序在应用程序本身中都有一个
templates/appname
文件夹

如果要使用根模板文件夹中的模板,只需提供模板名称,如
'base.html'
,如果要使用应用程序模板,则使用
'appname/base.html'

文件夹结构:

project/
  appname/
    templates/ 
      appname/  <-- another folder with app name so 'appname/base.html' is from here
        base.html
    views.py
    ...

  templates/    <-- root template folder so 'base.html' is from here
    base.html

  settings.py
  views.py
  ...
项目/
appname/
模板/

appname/我认为这取决于你希望你的主页是什么。如果它只是一个链接到站点其他部分的页面,那么答案是一个干净的好方法

例如,如果您希望站点的根成为您的博客,我会这样做:

urlpatterns=patterns(“”,
#Django管理员
url(r“^admin/”,包括(admin.site.url)),
#微型MCE URL
url(r“^tinymce/”,包括('tinymce.url'),
#其他应用程序
url(r“^other/”,包括('projectname.other.url',namespace='other'),
#博客应用
url(r'^',包括('projectname.blog.url',namespace='blog'),
)

另外,别忘了给你的url命名空间,包括:

你能发布完整的url吗?py?它仍在尝试从blog/templates加载模板。我已经查看了settings.py,如果我用TEMPLATE_DIR搞砸了什么,但是没有指定一个。所以我仍然很好奇它是如何得到这个路径的。我建议设置一个模板目录。是的,在
settings.py
中查看
TEMPLATE\u DIR
,你会发现一些东西。在Django文档中阅读更多关于该设置的信息,您可能会修复它。@Brandon在这种情况下,排序是否重要?他用
$
正确地终止根的正则表达式,这样只会与根url匹配
/
我知道这样排序会更可读,只是想知道这是否会影响django查找模板的位置。还被两个模式lolThx之前缺少的
url
弄糊涂了!我修改了settings.py中的设置,一切正常!thx alot:)我仍然发现很难找出一些与settings.py有关的问题。我很高兴它起作用了。太多了@KevinvanHengst没问题,当我开始的时候,我遇到了类似的问题,不得不去挖掘一段时间来寻找答案,很高兴我能帮上忙。谢谢你的回答。我现在正在这样做,应用程序模板可以工作,但扩展项目模板却不行。模板加载器仅在应用程序文件夹中进行搜索。有什么建议吗?这很好,对于django 1.9,您要更新的变量是位于“TEMPLATES=”中的“DIRS”,并且由于默认项目根是BASE\u DIR,所以在TEMPLATES dict中应该是这样的:'DIRS':[os.path.join(BASE\u DIR,'TEMPLATES')。replace('\\','/'),],django+1.9中不推荐使用此方法。
project/
  appname/
    templates/ 
      appname/  <-- another folder with app name so 'appname/base.html' is from here
        base.html
    views.py
    ...

  templates/    <-- root template folder so 'base.html' is from here
    base.html

  settings.py
  views.py
  ...
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    url(r'^blog/', include('hellodjango.blog.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^tinymce/', include('tinymce.urls')),
)

urlpatterns += patterns(
    'django.views.generic.simple',
    (r'^', 'direct_to_template', {"template": "base.html"}),
)