How do I let applications in Django know that the login process has been completed?

There are several applications in my Django project (app1, app2, app3). App1 has several view models including login and authentication.
After I am logged in, the web page will go back to "home" and my base.html will run the following command to show the username.
This base.html file is shared across all applications in the project.

        <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>

      

It's funny that the other app2 and app3 don't display the username and still only show "Login" on the navbar-nav. I guess it should show "user_name + Logout"

.
I thought the login process did not happen, but it seems that the process looked good on the system in a way, that I masked app2 and app3 if they are not registered correctly using @login_required.
So when I don't enter app 1, apps 2 and 3 don't work. But he doesn't answer {{request.user.username}}

.

App2 and 3 have their own views.py. I cannot combine entire views to avoid making the code super messy. How can I fix this problem to show the correct username and sign out after logging in?

Below are the codes for app2 (poll) and app1 (signups)

[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 '/')    

      

+3


source to share


1 answer


in each view of the print.user request of the application and check if you are logged in username or not.

And instead of {{request.user.username}} enter {{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>

      

Now check if it works or not

+1


source







All Articles