Capturing current login using Django classes?

I am trying to capture a registered user and display it at the top of each view. I searched everywhere for this, but I cannot find a direct answer to my problem.

I managed to get it in the form of a form, but for some reason I can’t display it in normal mode. It drives me crazy.

from django.http import HttpResponse, Http404 from django.views.generic import ListView, DetailView, FormView from django.template import RequestContext, loader, Context from django.core.urlresolvers import reverse from boards.models import Links, LinksCreateForm, Category from django.contrib.auth.models import User def get_user(request): current_user = request.get.user return current_user class LinksListView(ListView): model = Links class LinksDetailView(DetailView): model = Links class LinksCreateView(FormView): template_name = 'boards/link_create.html' form_class = LinksCreateForm def form_valid(self, form): name = form.cleaned_data['name'] description = form.cleaned_data['description'] user = self.request.user category = Category.objects.get(id=form.cleaned_data['category'].id) link = Links(name=name, description=description, user=user, category=category) link.save() self.success_url = '/boards/' return super(LinksCreateView, self).form_valid(form) 
+6
source share
4 answers

In your general view implementation, you will need to extend get_context_data

 def get_context_data(self, **kwargs): # Call the base implementation first to get a context c = super(ReqListView, self).get_context_data(**kwargs) user = self.request.user return c 

Then it depends on your requirements, what you want to do with it.

Extending generic view classes to generic get_context_data

+8
source

You can add 'django.core.context_processors.request' to TEMPLATE_CONTEXT_PROCESSORS in settings.py and then refer to the current user as {{ request.user }} . If you do not have this variable, you can add it as:

 TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.request', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.static', 'django.core.context_processors.tz', 'django.contrib.messages.context_processors.messages' ) 

https://docs.djangoproject.com/en/1.4/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS

+6
source

In your view, you have access to the request (and therefore to the user):

 self.request.user 

Since you are talking about displaying it at the top of the "View", I believe that you need access to it in the template.

You should have access to it in your template as:

 {{ request.user }} 
+5
source

adding templates to context processors, as @sneawo point is the best way. however, I prefer not to override the defaults, but to expand . Like this:

 from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP # ... TEMPLATE_CONTEXT_PROCESSORS = TCP + ( 'django.core.context_processors.request', ) 

Why? Because the list of standard context processors in django varies from version to version. Some built-in / shipped (tabbed) libraries depend on the default settings. In general, I believe that it is better not to override the default values ​​if you do not need to.

0
source

All Articles