Wrong Redirect

13 views
Skip to first unread message

ramadhan ngallen

unread,
Jun 20, 2019, 5:12:05 AM6/20/19
to Django users
Hello Team
I created an app(called users) for user registration
If user enter two mismatched password an app should redirect to the same page. otherwise it should redirect to the homepage
Unfortunately when user enter two mismatched password it redirect to the wrong url http://127.0.0.1:8000/users/register/register 
and also if user enter matched password it also redirect to the wrong url http://127.0.0.1:8000/users/register/register

 its view.py
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth


# Create your views here.


def register(request):

if request.method == 'POST':
first_name = request.POST['first_name']
first_name = first_name.title
last_name = request.POST['last_name']
last_name = last_name.title
username = request.POST['username']
password1 = request.POST['password1']
password2 = request.POST['password2']
email = request.POST['email']
if password1 == password2:
user = User.objects.create_user(
username=username, first_name=first_name, last_name=last_name, email=email, password=password1)

user.save()
print('user created')
return redirect('/travello/')

else:
return render(request, 'users/register.html')

else:
return render(request, 'users/register.html')
I have also created its urls.py as follows
from . import views
from django.urls import path


app_name = 'users'

urlpatterns = [
path('register/', views.register, name='register'),
]

Settings.py
INSTALLED_APPS = [
'users.apps.UsersConfig',
'travello.apps.TravelloConfig',
'calc.apps.CalcConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
user app directory 

image.png






Aldian Fazrihady

unread,
Jun 20, 2019, 5:21:07 AM6/20/19
to django...@googlegroups.com
Hi Ramadhan, 

I think redirection is only needed on valid and successful POST. 

On invalid POST,  I will re-render the current form + error message without redirection. 


Regards,  

Aldian Fazrihady

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAhXuBF07Q72_mi1AfPJ5LHHmaEdd5PhvQ79ToqSvpEt0gCivg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Sipum Mishra

unread,
Jun 20, 2019, 5:24:21 AM6/20/19
to django...@googlegroups.com
Hi Ramadhan,

Instead of rendering to any page please use HttpResponseRedirect('put home page url where u want to redirect' ).

And also import django.http import HttpResponseRedirect

Do the above & let me know. I hope it will work. 

ramadhan ngallen

unread,
Jun 21, 2019, 4:47:02 AM6/21/19
to Django users
Reply all
Reply to author
Forward
0 new messages