Py学习  »  Django

django allauth如果未通过身份验证,则使用不同的布局

cwhisperer • 5 年前 • 1925 次点击  

我将django与allauth一起使用,如果用户没有经过身份验证,我需要一个不同的布局。在login.html中,我不能使用“extends base.html”。这是可能的,而不失去所有的特点阿拉斯?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48847
 
1925 次点击  
文章 [ 3 ]  |  最新文章 5 年前
cwhisperer
Reply   •   1 楼
cwhisperer    6 年前

所有的答案我找到了一个解决办法 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'),]

在我的templates\accounts文件夹中,我现在有了一个完整的login.html页面,它有自己的设计,完全与base.html分离

Alex
Reply   •   2 楼
Alex    6 年前

不管是不是,程序都是一样的。 在您的视图中,您可以检查用户是否经过身份验证

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',{})
Kevin
Reply   •   3 楼
Kevin    6 年前

你可以用这样的东西:

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