32

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

因此,我在项目根目录的urls.py中添加了以下代码:

from django.views.generic.simple import direct_to_template

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

问题是它在blog/templates/...中搜索模板, 而不是在我的根目录中搜索模板文件夹。其中包含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')),
)

我忽略了什么吗?

4

5 回答 5

63

你设置TEMPLATE_DIRS在你的settings.py? 检查并确保它使用绝对路径正确设置。这就是我确保正确设置的方式:

设置.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
  ...
于 2013-03-14T14:18:51.417 回答
4
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"}),
)
于 2013-03-14T14:37:44.993 回答
2

我会这样重新组织网址:

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

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

于 2013-03-14T14:01:44.840 回答
0

我认为这取决于你希望你的主页是什么。如果它只是一个链接到您网站其他部分的页面,那么凯瑟琳的回答是一个很好的干净方式。

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

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

于 2013-03-14T16:32:01.117 回答
0

你可以参考我的解决方案

Django==3.2.5

https://stackoverflow.com/a/68420097/10852018

于 2021-07-17T11:31:34.443 回答