如何让 Django 中的应用程序知道登录过程已经完成?
How to allow apps in Django to know that the login-process has been done?
在我的 Django 项目中,有几个应用程序(app1、app2、app3)。
App1 在视图中有多个视图模型,包括登录和身份验证。
登录后,网页移回 'home',我的 base.html 有以下命令显示用户名。
此 base.html 文件在整个应用程序中共享项目。
<ul class="nav navbar-nav pull-right">
{% if request.user.is_authenticated %}
<li><a href="/">{{ request.user.username }}</a></li>
<li><a href="/accounts/logout">Logout</a></li>
{% else %}
<li><a href="/accounts/login">Login</a></li>
{% endif %}
</ul>
有趣的是,另一个 app2 和 app3 不显示用户名,在导航栏导航上仍然只显示 "Login"。我想它必须显示 "user_name + Logout"
.
我原以为没有发生登录过程,但从某种意义上说,我屏蔽了 app2 和 app3 的系统似乎这个过程运行良好,除非他们使用@login_required.
正确登录所以当我不登录 app1 时,app2 和 3 不工作。但它没有响应 {{request.user.username}}
。
App2 和 3 有自己的 views.py。我不能将整个视图混合在一起以免使代码变得超级脏。如何解决此问题以在登录后显示正确的用户名和 "Logout" 登录?
下面是 app2(投票)和 app1(注册)的代码
[poll.vies.py]
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.http import Http404
from django.http import HttpResponse
from polls.models import Poll
from django.template import RequestContext, loader
from django.shortcuts import render
from polls.models import Choice, Poll
from django.core.urlresolvers import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
#with this way, I no longer need to import loader, Request context, httpresponse.
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
[注册人数。views.py]
from django.http import HttpResponse
from django.shortcuts import render, render_to_response, RequestContext, HttpResponseRedirect
from django.conf import settings
from django.core.mail import send_mail
from django.contrib import messages
from django.contrib import auth
#from django.core.paginator import Paginator, InvalidPage, EmptyPage
#from django.core.urlresolvers import reverse
from .forms import SignUpForm
def home(request):
#The below line is very critical!! :)
form = SignUpForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
#send_mail(subject,message,from_email,to_list,fail_silently=True)
subject = 'Thank you'
message = 'Welcome home /n I am so happy'
from_email = settings.EMAIL_HOST_USER
to_list = [save_it.email,settings.EMAIL_HOST_USER ]
#send_mail(subject,message,from_email,['christdavid@naver.com'], fail_silently=True)
send_mail('Subject here', 'Wow, this email sending altorithm is working.\n What a marvelous function is.', settings.EMAIL_HOST_USER,
to_list, fail_silently=False)
messages.success(request, 'goodgood')
return HttpResponseRedirect('/thank-you/')
return render_to_response("signup.html", locals(),
context_instance = RequestContext(request))
def login(request):
return render_to_response('login.html', locals(), RequestContext(request))
def authenticate(request):
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
if user == None:
return HttpResponse('username or password error')
auth.login(request, user)
#I requested "request"!!!!
return HttpResponseRedirect(request.POST.get('next', '/') or '/')
def logout(request):
auth.logout(request)
return HttpResponseRedirect('/')
def signup(request):
return render_to_response('signup.html', locals(), RequestContext(request))
def create(request):
user = User.objects.create_user(username=request.POST['username'],
email=request.POST['email'],
password=request.POST['password'])
print 'create', user
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
print 'authenticated', user
auth.login(request, user)
return HttpResponseRedirect(request.POST.get('next', '/') or '/')
在应用程序的每个视图中打印 request.user 并检查您是否正在登录用户名。
而不是 {{ request.user.username }} 放 {{ user.username }}
<ul class="nav navbar-nav pull-right">
{% if user.is_authenticated %}
<li><a href="/">{{ user.username }}</a></li>
<li><a href="/accounts/logout">Logout</a></li>
{% else %}
<li><a href="/accounts/login">Login</a></li>
{% endif %}
</ul>
现在检查它是否工作
在我的 Django 项目中,有几个应用程序(app1、app2、app3)。
App1 在视图中有多个视图模型,包括登录和身份验证。
登录后,网页移回 'home',我的 base.html 有以下命令显示用户名。
此 base.html 文件在整个应用程序中共享项目。
<ul class="nav navbar-nav pull-right">
{% if request.user.is_authenticated %}
<li><a href="/">{{ request.user.username }}</a></li>
<li><a href="/accounts/logout">Logout</a></li>
{% else %}
<li><a href="/accounts/login">Login</a></li>
{% endif %}
</ul>
有趣的是,另一个 app2 和 app3 不显示用户名,在导航栏导航上仍然只显示 "Login"。我想它必须显示 "user_name + Logout"
.
我原以为没有发生登录过程,但从某种意义上说,我屏蔽了 app2 和 app3 的系统似乎这个过程运行良好,除非他们使用@login_required.
正确登录所以当我不登录 app1 时,app2 和 3 不工作。但它没有响应 {{request.user.username}}
。
App2 和 3 有自己的 views.py。我不能将整个视图混合在一起以免使代码变得超级脏。如何解决此问题以在登录后显示正确的用户名和 "Logout" 登录?
下面是 app2(投票)和 app1(注册)的代码
[poll.vies.py]
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.http import Http404
from django.http import HttpResponse
from polls.models import Poll
from django.template import RequestContext, loader
from django.shortcuts import render
from polls.models import Choice, Poll
from django.core.urlresolvers import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
#with this way, I no longer need to import loader, Request context, httpresponse.
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
[注册人数。views.py]
from django.http import HttpResponse
from django.shortcuts import render, render_to_response, RequestContext, HttpResponseRedirect
from django.conf import settings
from django.core.mail import send_mail
from django.contrib import messages
from django.contrib import auth
#from django.core.paginator import Paginator, InvalidPage, EmptyPage
#from django.core.urlresolvers import reverse
from .forms import SignUpForm
def home(request):
#The below line is very critical!! :)
form = SignUpForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
#send_mail(subject,message,from_email,to_list,fail_silently=True)
subject = 'Thank you'
message = 'Welcome home /n I am so happy'
from_email = settings.EMAIL_HOST_USER
to_list = [save_it.email,settings.EMAIL_HOST_USER ]
#send_mail(subject,message,from_email,['christdavid@naver.com'], fail_silently=True)
send_mail('Subject here', 'Wow, this email sending altorithm is working.\n What a marvelous function is.', settings.EMAIL_HOST_USER,
to_list, fail_silently=False)
messages.success(request, 'goodgood')
return HttpResponseRedirect('/thank-you/')
return render_to_response("signup.html", locals(),
context_instance = RequestContext(request))
def login(request):
return render_to_response('login.html', locals(), RequestContext(request))
def authenticate(request):
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
if user == None:
return HttpResponse('username or password error')
auth.login(request, user)
#I requested "request"!!!!
return HttpResponseRedirect(request.POST.get('next', '/') or '/')
def logout(request):
auth.logout(request)
return HttpResponseRedirect('/')
def signup(request):
return render_to_response('signup.html', locals(), RequestContext(request))
def create(request):
user = User.objects.create_user(username=request.POST['username'],
email=request.POST['email'],
password=request.POST['password'])
print 'create', user
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
print 'authenticated', user
auth.login(request, user)
return HttpResponseRedirect(request.POST.get('next', '/') or '/')
在应用程序的每个视图中打印 request.user 并检查您是否正在登录用户名。
而不是 {{ request.user.username }} 放 {{ user.username }}
<ul class="nav navbar-nav pull-right">
{% if user.is_authenticated %}
<li><a href="/">{{ user.username }}</a></li>
<li><a href="/accounts/logout">Logout</a></li>
{% else %}
<li><a href="/accounts/login">Login</a></li>
{% endif %}
</ul>
现在检查它是否工作