0

This is more theory question than specific code problem.

I'm kinda confused how information like username, user email, etc. is stored and accessed in Django.
From what I know:
I can access it like {{user.username}} IF previously I got here by using render() or render_to_response() with RequestContext inside. If I got on the page by using HttpResponseRedirect than I don't have access to user.username and it is just blank here.

What I want to know:
Is there way to access user data from every single page regardless of how I got there? I mean even if I don't use any views at all, like manually typing address.
Strangely enough, I always thought that data like username is accessible through sessions, but 9 out of 10 articles about this topic always talk about RequestContext and other stuff that passes values from one page to another.

I also read about custom context processors, but as I think it is only good for specific purposes, not simple username.

So maybe in a nutshell the question is:
If I want to display for example username in the corner of every single page of the site - how should I do that?

What happens here (for dummies)
HttpResponseRedirect doesn't affect anything at all. The problem is that it is redirecting to render_to_response() call that does not include RequestContext. If we do not specify RequestContext it will write it out as blank and force it over the session data given by the context_processor.auth, so you basically have nothing at all as your context data at this page.

How it should be done(in my opinion) is that you always use render() or render_to_response() with RequestContext in any of your views, don't forget it. And don't worry at all about various redirects since they do not affect context data. Static html pages get context data from processor, it works, my fault I didn't check it twice beforehand.

ScienceSamovar
  • 422
  • 7
  • 15

1 Answers1

1

Just output the {{ user.username }}. If user is logged, some of auth middleware and django.contrib.auth.context_processors.auth context processor are enabled then the username will be shown.

The middleware takes the user id from the session, loads the User from the db and sets the request.user attribute. Then context processor takes the request.user attribute and injects it into the RequestContext as the user variable.

To make the user variable available in your template you should use the render() shortcut. Or the render_to_response() with the RequestContext as the context_instance argument.

catavaran
  • 44,703
  • 8
  • 98
  • 85
  • hm... that's strange, I have everything enabled and I'm logged in, but for some reason the pages that are accessed ussing HttpResponseRedirect for example can't output {{user.username}}.... I'll try to debug, but... – ScienceSamovar Apr 01 '15 at 04:55
  • Read the last paragraph of my answer. If you use the `render_to_response()` shortcut then you should pass the valid `context_instance` parameter. – catavaran Apr 01 '15 at 04:59
  • Or, which is much better, use the `render()` call instead of `render_to_response()`. – catavaran Apr 01 '15 at 04:59
  • but what if I need to use HttpResponseRedirect? I can of course walk around it, but it will require more code. – ScienceSamovar Apr 01 '15 at 05:02
  • `HttpRenderToResponse`? Do you mean the `HttpResponseRedirect`? You should do nothing special. This is just a usual redirect and it doesn't alter the session data, – catavaran Apr 01 '15 at 05:06
  • and the question still is - is there solution to make it available even without manipulation with calls in views? Like if I would go to some static html page by typing the address and then see my username there. – ScienceSamovar Apr 01 '15 at 05:06
  • yeah, typo... But if I use it then on the page it goes to I can't access user data, why can it be so? – ScienceSamovar Apr 01 '15 at 05:07
  • Just noticed that this HttpResponseRedirect redirects to view that does render_to_response without RequestContext, does it drops session data or something like that? – ScienceSamovar Apr 01 '15 at 05:14
  • OK, I suppose that was the problem. Render_to_response without RequestContext was changing context data. I tried static html page and it has right user context data(sorry, didn't try it earlier). So, basically everything was working as I wanted in the original post, it was I who forced context data to drop because of absence of RequestContext. – ScienceSamovar Apr 01 '15 at 05:21
  • If you use the `render_to_response()` without `RequestContext` then all variable set in context processors will be unavailable in your template. This is why the `render()` shortcut was made. It has the same functionality as the `render_to_response()` but is easier to use. – catavaran Apr 01 '15 at 05:27
  • Thatnk you very much :) – ScienceSamovar Apr 01 '15 at 05:30