OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django django.contrib.auth.urls auth - redirect after login

  • Thread starter Thread starter Majoris
  • Start date Start date
M

Majoris

Guest
Using python 3.11.9 and Django 5.0.6.

I don't have a login/authentication function in views.py that I can write code of desired redirect in.

I am using django.contrib.auth.urls for authentication. How do make Django redirect user after successful authentication?

I want the user to be redirected to /test after authentication.

urls.py

Code:
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
url('test', views. Test, name='test'),
url('', views.home, name='home'),
]

views.py

Code:
@csrf_protect
def home(request):
    return render(request, 'landing.html')


@csrf_protect
def test(request):
    if request.user.is_authenticated:
        return render(request, 'test.html')
    else:
        return redirect('/')
<p>Using python 3.11.9 and Django 5.0.6.</p>
<p>I don't have a login/authentication function in <code>views.py</code> that I can write code of desired redirect in.</p>
<p>I am using <code>django.contrib.auth.urls</code> for authentication. How do make Django redirect user after successful authentication?</p>
<p>I want the user to be redirected to <code>/test</code> after authentication.</p>
<p>urls.py</p>
<pre><code>urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
url('test', views. Test, name='test'),
url('', views.home, name='home'),
]
</code></pre>
<p>views.py</p>
<pre><code>@csrf_protect
def home(request):
return render(request, 'landing.html')


@csrf_protect
def test(request):
if request.user.is_authenticated:
return render(request, 'test.html')
else:
return redirect('/')
</code></pre>
 
Top