#Issue with redirection.

21 messages · Page 1 of 1 (latest)

serene girder
#

I'm a beginner to django. I've created a basic user registration and login application.
For some reason it doesn't redirect from the login page.
Here is the views.py file:
`
def login_view(request):
form = LoginForm()
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
# print(user)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect("mysite\polls\templates\polls\profile.html")

context = {"loginform": form}
return render(request, "polls/login.html", context=context)

Here is the urls.py file:
from django.urls import path

from . import views

urlpatterns = [
path("", views.homepage, name=""),
path("register", views.register, name="register"),
path("login", views.login_view, name="login"),
path("profile", views.profile, name="profile"),
path("logout", views.logout, name="logout"),
]
here is the login.html page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome to the Login Page</h1>
<form method="POST" autocomplete="off">
{% csrf_token %}
{{ loginform.as_p }}
<input type="submit" value="Login">
</form>
</body>
</html>
`
I've tried adding event listeners to redirect but that too doesn't work.
This is the repo: https://github.com/shasank01/user-auth

GitHub

User authentication and Authorization Application Using Django - GitHub - shasank01/user-auth: User authentication and Authorization Application Using Django

#

I've tried return redirect("profile") instead of httpresponseredirect but that too didnt work

final rose
#

define "didn't work"

#

also please see #readme-1st on proper code formatting

#

Now you are redirecting to what looks like template file. Django doesn't work like that

#

actually nothing does. You redirect to an url. In django there is no any kind of strong relation between url and template file

#

And another matter - why are making login instead of using built-ins?

serene girder
final rose
#

It stays on same page or does it reload same page?

#

Your view doesn't have any invalid form handling so it would be the case

serene girder
final rose
#

So most likely form is invalid or user is None

#

you don't handle it in any way so that what would happen

serene girder
#

Checked in the Django Admin page. The user exists. Probably invalid form handling is the case. I've learned from this tutorial as reference : https://www.youtube.com/watch?v=Z3qTXmT0yoI . If you skip to the User authentication part, he doesn't prov

Handle user registration and authentication in Django

⚡️ Important links:
https://code.visualstudio.com/download
https://github.com/cloud-with-django/django-auth

🪧 Video timestamps:
00:00 - Intro
00:07 - Setup our Django project

06:56 - Setup our Django app
8:36 - Configure our URL's, Views and Templates

25:19 - User registration

40:25 - ...

▶ Play video
#

provide form handling

#

But the redirection worked for him.

final rose
#

Why, why people make login when it's available out of the box

#

And then hit issues

#

No error handling in login view is beyond idiotic

#

So if you mistype your password - nothing happens, page just reloads - brilliant UX you really need to show in tutorial