keisaac keisaac - 1 year ago 163
Python Question

How to use redirect_authenticated_user with extra_context in Django

I am currently using Django v1.11. What I want to happen is, If the authenticated user access the login page they will be redirect. In my case right now they can access the login page even they are authenticated.

my previous url.py that extra_context works is this:

url(r'^login/$', auth_views.login, {'extra_context': { 'all_user': User.objects.all() }}, name='login')


Then base from this documentation: https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.views.LoginView

I changed urls.py of login into LoginView.as_view() but the extra_context is not working anymore on my registration/login.html default template.

{% for user in all_user %}
<option>{{ user.username }}</option>
{% endfor %}


urls.py

url(r'^login/$', auth_views.LoginView.as_view(redirect_authenticated_user=True), {'extra_context': { 'all_user': User.objects.all() }}, name='login')


Am I missing something?

Answer Source

pass the extra_content like this

 url(r'^login/$', auth_views.LoginView.as_view(extra_context= { 'all_user': User.objects.all() },redirect_authenticated_user=True),name='login')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download