【问题标题】:django allauth different layout if not authenticateddjango allauth 如果未通过身份验证,则布局不同
【发布时间】:2018-11-13 11:26:46
【问题描述】:

我将 django 与 allauth 一起使用,如果用户未通过身份验证,我需要不同的布局。在我的 login.html 中,我不能使用“扩展 base.html”。在不丢失 allauth 的所有功能的情况下,这是否可能?

【问题讨论】:

  • 不同的布局使用相同的模板?还是其他模板?
  • 两种完全不同的布局

标签: django django-allauth


【解决方案1】:

你可以使用类似的东西:

{% if user.is_authenticated %}
    {% include "subtemplate1.html" %}
{% else %}
    {% include "subtemplate2.html" %}
{% endif %}

【讨论】:

    【解决方案2】:

    Allauth 与否,程序是一样的。 在您的视图中,您可以检查用户是否已通过身份验证

    def my_view(request):   
      if request.user.is_authenticated: 
          return render(request, 'myapp/index.html' {})
      else: 
          return render(request, 'myapp/logged_in.html',{})
    

    但是,最好有不同的类:

    from django.contrib.auth.decorators import login_required
    
    def my_view(request): 
       if not request.user.is_authenticated: 
           return render(request, 'myapp/index.html' {})
       else: 
           return my_view_auth(request)
    
    @login_required
    def my_view_auth(request):
       return render(request, 'myapp/logged_in.html',{})
    

    【讨论】:

      【解决方案3】:

      感谢所有答案。我找到了一个解决方案here,我想分享并提出一些意见:

      #  urls.py
      from django.contrib import admin
      from django.urls import path
      from django.conf.urls import url, include
      from django.views.generic.base import RedirectView
      from allauth.account.views import LoginView
      
      class Lvx(LoginView):
          # Login View eXtended
          # beware ordering and collisions on paths
          template_name = "accounts/login.html"
      
      login = Lvx.as_view()
      
      urlpatterns = [
          path('admin/', admin.site.urls),
          url(r'^accounts/login/$', login),
          url(r'^accounts/', include('allauth.urls')),
          url(r'^core/', include('core.urls')),
          url(r'^$', RedirectView.as_view(url='/core'), name='core'),]
      

      在我的模板\帐户文件夹中,我现在有一个完整的 login.html 页面,它具有自己的设计并与 base.html 完全分离

      【讨论】:

      • 不清楚你试图做什么或为什么你让它变得更复杂。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2021-06-15
      • 2017-04-09
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多