Home > Blockchain >  How can I replace part of url in redirecting?
How can I replace part of url in redirecting?

Time:07-24

I have some problems in redirecting urls in my Django app.

This is my urls.py:

urlpatterns = [
path('Stu_SignUp/step1/', views.new_student, name='new_student'),
path('Stu_SignUp/step2/', views.comPnew_student, name='comPnew_student')]

Now I want to change part of url from /step1/ to /step2/.

These are my views.py app:

def new_student(request):
if request.method == "POST":
    Person.objects.create(p_fName=request.POST['fName'], p_lName=request.POST['lName'], p_SSN=request.POST['SSN'],
                          p_birthDate=request.POST['birthdate'], p_phoneNum=request.POST['phoneNum'])
    return redirect('Stu_SignUp/step2/')
else:
    return render(request, 'signUp_student.html', context={'person': Person})


def comPnew_student(request):
    if request.method == "POST":
        if request.method['password'] == request.method['conf_Pass']:
            update_person = Person.objects.get(Person_ID=Person.objects.last().id)
            update_person.p_Email = request.POST['email']
            update_person.p_Password = request.POST['password']
            return redirect('Stu_SignUp/step2/')
    else:
        return render(request, 'signUp_student2.html', context={'person': Person})

Html Template is signUp_student.html and signUp_student2.html:

Page Number 1:

    <form action="{%  url 'new_student' %}" method="post">
    {% csrf_token %}
    <label for="firstName">First Name<br>
        <input type="text" name="fName">
    </label><br>
    <label for="lastName">Last Name<br>
        <input type="text" name="lName">
    </label><br>
    <label for="firstName">SSN Code<br>
        <input type="text" name="SSN">
    </label><br>
    <label for="birthdate">Birthdate <br>
        <input type="date" name='birthdate'>
    </label><br>
    <label for="phoneNumber">Phone Number<br>
        <input type="text" name='phoneNum'>
    </label>
    <br><br>
    <button type="submit">Sign up</button>
</form>

Page Number 2:

    <form action="{%  url 'comPnew_student' %}" method="post">
    {% csrf_token %}
    <label for="Email">Email<br>
        <input type="text" name="email">
    </label><br>
    <label for="password">Password<br>
        <input type="text" name="password">
    </label><br>
    <label for="confPassword">Confirm Password<br>
        <input type="text" name="conf_Pass">
    </label><br>
    <br><br>
    <button type="submit">Sign up</button>
</form>

I'll appreciate your help.

CodePudding user response:

you can add a slash before the path return redirect('/Stu_SignUp/step2/')

OR

use the HttpResponseRedirect

from django.http import HttpResponseRedirect

return HttpResponseRedirect("http://Stu_SignUp/step2/")

CodePudding user response:

add a slash before the redirect path, redirect('/Stu_SignUp/step2/')

Thanks to Marat

  • Related