Home > Software engineering >  Getting redirected to wrong url in django
Getting redirected to wrong url in django

Time:04-14

I am trying to send an account verification email and redirect to the login page

def signup(request):
    custom_user_id = ''.join(random.choices(string.ascii_uppercase string.digits,k=10))
    if request.method=="POST":
        form = RegistrationForm(request.POST)
        if form.is_valid():
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            username = form.cleaned_data['username']
            user = Account.objects.create_user(user_id=custom_user_id,first_name=first_name,last_name=last_name,email=email,username=username,password=password)#this method is present in models.py file
            user.save()
            """
            User activation mail
            """
            current_site = get_current_site(request)
            mail_subject = "Please activate your fundaMental account"
            message = render_to_string('account_verification_email.html',{
                'user':user,
                'domain':current_site,
                'uid':urlsafe_base64_encode(force_bytes(user.pk)),
                'token':default_token_generator.make_token(user),
            })
            to_email = email
            send_email = EmailMessage(mail_subject,message,[to_email])
            send_email.send()

It is supposed to redirect to '127.0.0.1:8000/account/login/?command=verification&email= email' but instead it gets redirected to '127.0.0.1:8000/account/signup/account/login/?command=verification&email= email'

    return redirect('account/login/?command=verification&email=' email)
        else:
            form = RegistrationForm()
        context = {
            'form':form,
        }
        return render(

request,"signup.html",context)

Here is my urls.py file

from django.urls import path
from . import views

urlpatterns = [
    path('signup/',views.signup,name='signup'),
    path('login/',views.login,name='login'),
    path('logout/',views.logout,name='logout'),
    path('activate/<uidb64>/<token>',views.activate,name='activate'),
]

Here is my project level urls.py

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.home,name='home'),
    path('account/',include('account.urls')),
]

CodePudding user response:

Try with this


redirect('home', 'account/login/?command=verification&email=' email)

CodePudding user response:

You are redirecting to a relative link, so it is appending the redirect link URL to the existing 'account/signup' URL. To make it an absolute link you need to add a forward slash:

return redirect('/account/login/?command=verification&email=' email)
  • Related