2013-03-14 95 views
22

我正在试验Django,并找出如何设置urls.py,以及如何工作。 我已经在项目的根目录中配置了urls.py,以指向我的博客和管理员。 但现在我想添加一个页面到我家,所以在localhost:8000。Django模板文件夹

所以我加入以下代码到urls.py在项目的根目录:

from django.views.generic.simple import direct_to_template 

urlpatterns = patterns('', 
    (r"^$", direct_to_template, {"template": "base.html"}), 
) 

的问题是,它搜索在博客/模板模板/ ... 相反我的根目录下的模板文件夹。其中包含了base.html文件

全urls.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')), 
) 

编辑:添加完整的urls.py :)

我俯瞰的东西吗?

+0

能否请您发表你的完整url.py? – Brandon 2013-03-14 13:52:59

+0

@Brandon添加了:) – Kevinvhengst 2013-03-14 13:57:22

回答

49

您是否在settings.py中设置了TEMPLATE_DIRS?检查并确定它是用绝对路径正确设置的。这是我如何确保它已正确设置:

settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'), 
) 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

这样一来,我在我的项目根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 
    ... 
+0

Thx!我修改了setteings.py中的设置,并且所有内容都按预期工作! thx很多:)我仍然很难找出一些与settings.py有关的问题。我很高兴它的工作。多谢! – Kevinvhengst 2013-03-15 06:52:35

+0

@KevinvanHengst没问题,当我开始时,我遇到了类似的问题,不得不去寻找答案一段时间,很高兴我能提供帮助。 – Ngenator 2013-03-15 11:37:11

+0

感谢您的好评。我现在正在执行此操作,应用程序模板可以工作,但是扩展项目模板不会。模板加载器只会在应用程序文件夹中搜索。有什么建议么? – cbrad 2015-07-19 00:14:26

2

我会重新组织的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"}), 
) 

模式是由它们的特异性匹配,所以我倾向于首先把更具体的模式。否则,您可能会看到一些意外行为。试一试,如果它仍然在请求/的博客中加载模板,我们会深入研究。

+0

它仍在尝试从博客/模板中加载模板。我已经调查了settings.py,如果我搞砸了一些TEMPLATE_DIR,但没有指定一个。所以我仍然好奇它是如何得到这条道路的。 – Kevinvhengst 2013-03-14 14:14:21

+0

我会建议设置一个模板目录。 – Brandon 2013-03-14 14:58:46

+0

是的,查看'settings.py''中的''TEMPLATE_DIR'',你会发现一些东西。在Django文档中了解更多关于这个设置的信息,你可能会修复它。 – 2013-03-14 16:47:03

3
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"}), 
) 
0

我认为这取决于你想要你的主页是什么。如果它只是一个链接到您网站的其他部分的网页,那么catherine's答案是一个不错的干净方式。

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

urlpatterns = patterns('', 
    # Django Admin 
    url(r'^admin/', include(admin.site.urls)), 
    # Tiny MCE Urls 
    url(r'^tinymce/', include('tinymce.urls')), 
    # Other App 
    url(r'^other/', include('projectname.other.urls', namespace='other')), 
    # Blog App 
    url(r'^', include('projectname.blog.urls', namespace='blog')), 
) 

而且不要忘了名字空间的网址包括:https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces