模板的 Django url 和文件结构
Django urls and file structure for templates
我正在使用 Django 1.9.8 并开始学习 the official tutorial. The official tutorial emphasised reusabilitiy and "plugability". From there I followed this 授权教程。尽管我能够使用授权教程,但我不喜欢(或只是不明白)的一件事是为什么项目的 urls.py
文件包含多个特定于应用程序的 url,而不是将它们放在一起在应用程序的 urls.py
文件中,并将该文件包含在项目的 urls.py
文件中。这似乎与官方教程所强调的相悖。我知道每个项目对于 login/logout/register 可能有不同的 URL,等等...取决于 API 并且仍然需要编辑,但我想在一个地方更改它们更有意义,让事情更整洁。
项目名为authtest,应用名为log
#log/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('log.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm }), #move this to authtest/urls.py
url(r'^logout/$', views.logout, {'next_page': '/login'}), #move this to authtest/urls.py
]
现在为应用的 urls.py
文件
#authtest/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
这 100% 有效,所以现在是第一个问题。 有什么理由我不应该将日志应用程序特定的 url(登录和注销)移出项目的 urls.py 文件(log/urls.py)并将它们放入应用程序的 urls.py file (authtest/urls.py)? 也许有理由不进行身份验证,但是如果我正在制作一个不同的应用程序呢?
现在是我的第二个问题,我想这取决于第一个问题的答案。授权教程将 login.html、logout.html 和 home.html 模板放在项目的根模板文件夹中。 Django 教程建议将它们放在应用程序的模板目录中,在该目录中,另一个目录命名为应用程序的名称(用于命名空间)。 如果我将特定于应用程序的模板文件从项目的模板文件夹移动到日志应用程序的模板文件夹,我需要更改什么?
这是我遵循的授权教程中的当前文件结构
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...settings.py
|...|...urls.py
|...|...views.py
|...manage.py
|...templates
|...|...base.html
|...|...home.html
|...|...login.html
|...static
这就是我假设它应该基于官方教程建议如何使用模板的方式。
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...urls.py
|...|...views.py
|...|...templates
|...|...|...log #namespace of the log app
|...|...|...|...base.html
|...|...|...|...home.html
|...|...|...|...login.html
|...manage.py
|...templates
|...static
当我移动文件时,我在访问 http://localhost:8080/login/
时遇到以下错误 TemplateDoesNotExist at /login/
。我假设它只是 urls.py 文件,但我不确定我必须更改什么。
为 settings.py 模板指令编辑
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
#'DIRS': [os.path.join(BASE_DIR, 'templates')], #I also tried this
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
在您的 settings.py
中,您需要在 OPTIONS
部分添加 loaders
键。这指定了 django 如何找到你的 template files
。如果您没有指定 OPTIONS
键,APP_DIRS
设置应该就足够了。
TEMPLATES = [
{
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
'debug': DEBUG,
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
},
},
]
https://docs.djangoproject.com/en/1.10/ref/templates/api/#loader-types 了解更多信息
我正在使用 Django 1.9.8 并开始学习 the official tutorial. The official tutorial emphasised reusabilitiy and "plugability". From there I followed this 授权教程。尽管我能够使用授权教程,但我不喜欢(或只是不明白)的一件事是为什么项目的 urls.py
文件包含多个特定于应用程序的 url,而不是将它们放在一起在应用程序的 urls.py
文件中,并将该文件包含在项目的 urls.py
文件中。这似乎与官方教程所强调的相悖。我知道每个项目对于 login/logout/register 可能有不同的 URL,等等...取决于 API 并且仍然需要编辑,但我想在一个地方更改它们更有意义,让事情更整洁。
项目名为authtest,应用名为log
#log/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('log.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm }), #move this to authtest/urls.py
url(r'^logout/$', views.logout, {'next_page': '/login'}), #move this to authtest/urls.py
]
现在为应用的 urls.py
文件
#authtest/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
这 100% 有效,所以现在是第一个问题。 有什么理由我不应该将日志应用程序特定的 url(登录和注销)移出项目的 urls.py 文件(log/urls.py)并将它们放入应用程序的 urls.py file (authtest/urls.py)? 也许有理由不进行身份验证,但是如果我正在制作一个不同的应用程序呢?
现在是我的第二个问题,我想这取决于第一个问题的答案。授权教程将 login.html、logout.html 和 home.html 模板放在项目的根模板文件夹中。 Django 教程建议将它们放在应用程序的模板目录中,在该目录中,另一个目录命名为应用程序的名称(用于命名空间)。 如果我将特定于应用程序的模板文件从项目的模板文件夹移动到日志应用程序的模板文件夹,我需要更改什么?
这是我遵循的授权教程中的当前文件结构
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...settings.py
|...|...urls.py
|...|...views.py
|...manage.py
|...templates
|...|...base.html
|...|...home.html
|...|...login.html
|...static
这就是我假设它应该基于官方教程建议如何使用模板的方式。
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...urls.py
|...|...views.py
|...|...templates
|...|...|...log #namespace of the log app
|...|...|...|...base.html
|...|...|...|...home.html
|...|...|...|...login.html
|...manage.py
|...templates
|...static
当我移动文件时,我在访问 http://localhost:8080/login/
时遇到以下错误 TemplateDoesNotExist at /login/
。我假设它只是 urls.py 文件,但我不确定我必须更改什么。
为 settings.py 模板指令编辑
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
#'DIRS': [os.path.join(BASE_DIR, 'templates')], #I also tried this
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
在您的 settings.py
中,您需要在 OPTIONS
部分添加 loaders
键。这指定了 django 如何找到你的 template files
。如果您没有指定 OPTIONS
键,APP_DIRS
设置应该就足够了。
TEMPLATES = [
{
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
'debug': DEBUG,
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
},
},
]
https://docs.djangoproject.com/en/1.10/ref/templates/api/#loader-types 了解更多信息