【问题标题】:Django 2.2. Instead of my template django uses the standard姜戈 2.2。而不是我的模板 django 使用标准
【发布时间】:2019-11-25 19:52:40
【问题描述】:

我在 django 中创建了一个 authapp 应用程序。我在 settings 中注册并添加到主 urls.py 中:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('authapp/', include('authapp.urls', namespace='authapp')),
]

authapp/urls.py 我添加了:

from django.contrib.auth import views as auth_views
from django.urls import path, reverse_lazy

import authapp.views as authapp

app_name = 'authapp'

urlpatterns = [
    path('password_change/', auth_views.PasswordChangeView.as_view(success_url=reverse_lazy('authapp:password_change_done')), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
]

我已将模板添加到 authapp/templates/registration
- password_change_form.html
- password_change_done.html
但是,当您单击链接 http://127.0.0.1:8000/authapp/password_change/ 时,我会在 django 管理员中进入标准表单,而不是我的表单...
我做错了什么?

【问题讨论】:

    标签: python django


    【解决方案1】:

    您必须在 DIRS 中指定您的模板目录,如下面的 sn-p:

    TEMPLATES = [
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "DIRS": [BASE_DIR + "/templates/registration/"],
            "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中配置。

    见:https://docs.djangoproject.com/en/2.2/topics/templates/#configuration

    【讨论】:

    • 我已经试过了。它对我不起作用。 ` 'DIRS': [ os.path.join(BASE_DIR, 'templates', 'registration/'), os.path.join(BASE_DIR, 'authapp', 'templates', 'registration/'), os.path. join(os.path.dirname(os.path.abspath(file)), 'templates'), ],`
    【解决方案2】:

    问题解决了。
    只需将 authapp 应用置于 settings.py 中的标准 admin 应用之上。

    INSTALLED_APPS = [
        ...
        'authapp',
        'django.contrib.admin',
        ...
    ]
    

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多