mercredi 13 août 2014

python - django d'authentification à l'aide de auth.views - Stack Overflow


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.




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.




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.




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.



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.



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.



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.



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.


0 commentaires:

Enregistrer un commentaire