在我的Django项目中,有几个应用程序(app1,app2,app3)。
App1在视图内部有多个视图模型,包括登录和验证
登录后,网页返回“主页”,我的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没有显示用户名,仍然只在navbar-nav上显示“Login”。我想它必须显示"user_name + Logout"
。
我以为登录过程没有发生,但似乎这个过程在某种意义上看起来很好,我掩盖了app2和app3,除非他们使用@login_required正确记录。
因此,当我没有登录app1时,app2和3不起作用。但它没有回应{{request.user.username}}
。
App2和3有自己的views.py。我不能将所有视图混合在一起,以免使代码变得非常脏。如何解决此问题以在登录后显示正确的用户名和“注销”标志?
以下是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)
[signups.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 '/')
答案 0 :(得分:1)
在应用程序打印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>
现在检查其工作情况