导入django.contrib.auth.urls不能与现有管理模板配合使用 [英] Importing django.contrib.auth.urls does not play well with existing admin templates

查看:87
本文介绍了导入django.contrib.auth.urls不能与现有管理模板配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试遵循文档,使用内置的Django模板在Django(1.9)网站上登录/注销编外人员.特别是,我通过添加

I've been trying to follow documentation on using builtin Django templates to login/logout nonstaff users on a Django (1.9) site. In particular, I modified the urlconf by adding

url('^',include('django.contrib.auth.urls'))

带有预先登录的默认模板名称的/login和/logout端点和视图.

which brings in /login and /logout endpoints and views with default template names preprogrammed.

用于登录和注销的默认模板名称为 registration/login.html registration/logged_out.html .第一个在任何地方都不存在,因此我假设我应该创建一个 templates/registration/并创建登录模板.我认为同样的事情对于登出应该起作用,除非它不起作用.

The default template names for login and logout are registration/login.html and registration/logged_out.html. The first one doesn't exist anywhere, so I assumed I should create a templates/registration/ and create the login template, which I did. I thought the same thing should work for the logout, except it doesn't.

实际发生的是该模板解析为 django.contrib.admin.templates.registration.logged_out.html .这很漂亮,但是很臭,因为登录链接指向管理员登录,没有编外用户将无法使用该登录.

What actually happens is that the template resolves to django.contrib.admin.templates.registration.logged_out.html. This is pretty but stinks because the login link points to the admin login, which no nonstaff user will be able to use.

我真的希望我可以使用上面的urlconf,使用默认的模板名称,但要编写自己的模板.这不可能吗?替代方法似乎是重复一堆东西,而这并不是Python风格.

I really wish I could use the urlconf above, use the default template names, but write my own templates. Isn't this possible? The alternative seems to be repeating a bunch of stuff and that isn't very Pythonic.

我想这可能涉及修改 TEMPLATES 设置,或更改设置中其他内容的顺序.

I imagine it might involve modification of the TEMPLATES setting, or changing the orders of something else in the settings.

无论采用哪种解决方案,我都希望它不会干扰管理模板的正确解析(即,如果这些管理模板开始​​使用我的新模板,那就不好了.)

Whatever the solution is, I hope it does not interfere with the proper resolution of the admin templates (i.e. it would be bad if those started using my new templates.)

要求的详细信息:

我在(appname)/templates/registration/中创建了一个 login.html ,并且在访问登录URL时效果很好.

I created a login.html in (appname)/templates/registration/, and it works just fine when visiting the login url.

我也在(appname)/templates/registration/中创建了一个 logged_out.html ,但是发现在访问注销URL时,我得到了管理站点logging_out模板(上面写着感谢您今天花一些时间在网站上."

I created a logged_out.html in (appname)/templates/registration/ also, but discovered that when visiting the logout url, I got the admin site logged_out template (the one that says "Thanks for spending some quality time with the Web site today."

我的模板设置:

TEMPLATES = [
    {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
         'DIRS': [],
         'APP_DIRS': True,
         'OPTIONS': {
             'debug': True,
             'context_processors': [
                 'django.template.context_processors.debug',
                 'django.template.context_processors.request',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
             ],
         },
    },
]

INSTALLED_APPS = (
    'django.contrib.admin',
    'app',
    'django.contrib.auth',
    'django.contrib.contenttypes', 
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles'
)

项目结构(忽略我想是不必要的,并使用一些通用名称.)

Project structure (omitting what I guess is nonessential, and using some generic names.)

project/
    app/
        templates/
            app/
            registration/
                login.html
                logged_out.html
        models.py
        views.py
        admin.py
    gettingstarted/
        static/
        settings.py
        urls.py

由于从Heroku"Python入门"应用程序开始对其进行了改编,因此该结构可能看起来有些怪异.

The structure may look a bit weird since it was adapted starting from the Heroku "Getting started with python" app.

我终于在Django错误跟踪器中找到了正确的搜索词,发现它是一个已知问题.令人失望的是,它已经三年了,过去两年中没有任何评论.我想我只需要硬着头皮,定义使用不同路径上的模板的自己的网址即可.

I finally struck upon the right search terms in the Django bug tracker and found that is a known issue. Disappointingly it's already three years old with no comment in the past two years. I guess I will just have to bite the bullet and define my own urls that use templates on a different path.

推荐答案

Django的模板查找是按 INSTALLED_APPS 的顺序自上而下完成的.这意味着,如果要覆盖模板,则应在管理员中被覆盖的模板上方列出覆盖的模板应用.

Django's template lookup is done top down by the order of INSTALLED_APPS. Meaning that if you want to override a template, the overriding template app should be listed above the overriden one in the admin.

在这种情况下,应将 project.app 放在 django.contrib.admin 上方,因此在创建/registration/logout.html 时,将在管理模板之前加载.

In this case, project.app should be placed above django.contrib.admin so when creating /registration/logout.html it'll be loaded before the admin template.

通常,建议的已安装应用程序顺序为:项目->第三方应用程序-> django内置文件.它还会影响静态文件查找器.

In general, the recommended order of installed apps is: project -> 3rd party apps -> django builtins. It also affects static files finders.

由于 Django中的错误,覆盖了 registration/logged_out.html 模板也会覆盖管理"logged_out"模板.

Because of a bug in Django, overriding the registration/logged_out.html template overrides the admin "logged_out" template as well.

您可以专门包括注销视图,并指定其他注销"模板或 next_page (注销后重定向到的视图):

You can include the logout view specifically and specify a different "logged out" template or next_page (the view where it redirects after logout):

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
    url('^', include('django.contrib.auth.urls')),
]

这将在注销后重定向到/.也可以是一个已命名的网址.

This will redirect to / after logout. It can also be a named url.

或更改 logged_out 模板的位置,请使用:

Or to change the logged_out template location use:

url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),

然后在 project/app/templates/中创建 logged_out.html .

如果要在注销后将用户重定向回首页,则使用第一个选项;如果要显示已注销"消息,则使用第二个选项.

I would use the first option if you want to redirect the user back to the home page after logout, and the 2nd if you want to display a "logged out" message.

这篇关于导入django.contrib.auth.urls不能与现有管理模板配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆