views:

907

answers:

3

User should be redirected to the Login page after registration and after logout. In both cases there must be a message displayed indicating relevant messages.

Using the django.contrib.auth.views.login how do I send these {{ info }} messages.

A possible option would be to copy the auth.views to new registration module and include all essential stuff. But that doesn't seem DRY enough.

What is the best approach.

Update: Question elaboration:

For normal cases when you want to indicate to some user the response of an action you can use

request.user.message_set.create()

This creates a message that is displayed in one of the templates and automatically deletes.

However this message system only works for logged in users who continue to have same session id. In the case of registration, the user is not authenticated and in the case of logout since the session changes, this system cannot be used.

Add to that, the built in login and logout functions from django.contrib.auth.views return a 'HttpResponseRedirect' which make it impossible to add another variable to the template.

I tried setting things on the request object itself

request.info='Registered'

and check this in a different view

try:
   info = request.info:
   del request.info
except:
   info = ''

#later
render_to_response('app/file',{'info':info})

Even this didn't work.

Clearly I can define a registered.html and add this static message there, but I was being lazy to write another template and trying to implement it DRY.

I realized that the cases were different for "registered" message and "logged out" message. And the DRY approach I used, I shall write as an answer.

+2  A: 

If the messages are static you can use your own templates for those views:

(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}

From the docs.

Dominic Rodger
But that still requires multiple templates be defined. And each of those templates take form parameter too, which only login function generates. This desnt answer the question.
Lakshman Prasad
I believe this does answer the question, if the messages are static (i.e. they don't change per-user). I don't understand your problem with it, @becomingGuru. If you define a base login template, and then a couple specialized ones for each case that inherit from the base one and fill in the proper message, that would work very well. The "form" parameter is no problem; why would you need to use these templates with any view other than the login view? If this doesn't answer your question, then I don't think you phrased your question very well.
Carl Meyer
A: 

You have Request Context Processors to add this kind of information to the context of every template that gets rendered.

This is the "zero impact" way to do this kind of thing. You don't update any view functions, so it meets some definitions of DRY.

See http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

First, write your own login.html template.

Second, write your own context function to provide any additional information that must be inserted into the template.

Third, update settings to addy your context processor to the TEMPLATE_CONTEXT_PROCESSORS setting.

S.Lott
Just to display a message in the login page, include those variables in all possible requests?
Lakshman Prasad
-1 Context processors are not a good solution to this problem. They run for every single page load on the site, you don't want to use them for information that will be used on only a few pages.
Carl Meyer
Scorpion032 you also need to think of what CarlMeyer has said
Rama Vadakattu
@Rama Vadakattu - who on earth is Scorpion032?
Dominic Rodger
+1  A: 

I think the best solution to this problem is to use a "flash"-type session-based messaging system. There are several floating around: django-flash seems really nice, I use django-session-messages which is very simple. Hopefully by the time we get to Django 1.2 this'll be baked-in.

Carl Meyer
The patch mentioned in django-session-messages and the package itself addresses exactly the concern I had. Thanks!
Lakshman Prasad