I've been working on a Django project that has multiple types of users. Hence, I'm creating more than one signup page, one for each type of user. I created one page for users to chose if they want to sign up as a mentor or a student so they can later be given a right form. However, my urls don't work properly and both 'register' and 'register_student' urls take me to the view I created for register. What am I doing wrong?
My register/urls.py :
urlpatterns = [
path('', views.register, name='register'),
path('', views.student_register, name='student_register'),
]
My register/templates/register/register.html
{% block content %}
<h2>Sign up</h2>
<p class="lead">Select below the type of account you want to create</p>
<a href="{% url 'student_register' %}" class="btn btn-student btn-lg"
role="button">I'm a student</a>
{% endblock %}
My register/views.py
def register(request):
return render(request, "register/register.html")
def student_register(request):
if request.method == "POST":
student_form = StudentRegisterForm(request.POST)
if student_form.is_valid():
user = student_form.save(commit=False)
user.is_student = True
user.save()
else:
student_form = StudentRegisterForm()
return render(request, "register/student_register.html", {"student_form": student_form})
And my app's ursl: mentorapp/mentorapp/urls:
urlpatterns = [
path('', include("django.contrib.auth.urls")), # gives access to django log-in/out pages
path('mainpage/', include('mainpage.urls')),
path('register/', include('register.urls')),
path('student_register/', include('register.urls')),
]
localhost:8000/register shows the 'register.html' page and when I click on 'I'm a student' url changes to localhost:8000/register_student but it stays on the same html page - 'register.html', and it doesn't render the right view containing the student registration form. Any thoughts?
From django docs :
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
The problem is: how you are defining the same urls for both paths with register/urls.py
, the url dispatcher resolves the first one and neves looks for the other.
eg
urlpatterns = [
path('register', views.register, name='register'),
path('register_student', views.student_register, name='student_register'),
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.