2013-03-14 72 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