I am a beginner in django. Made a project and app by following YouTube Videos and django documentation.
I have made an HTML page for sign in, sign up and business sign up repectively.
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls), #website.com/admin/
path('', include('signin.urls')), #website.com/
path('home/', include('signin.urls')), #website.com/home/
path('signin/', include('signin.urls')), #website.com/signin/
path('signup/', include('signin.urls')), #website.com/signup/
path('business_signup/', include('signin.urls')), #website.com/business_signup/
]
signin/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.default, name='signin'),
path('home/', views.default, name='home'),
path('signin/', views.default, name='signin'),
path('signup/', views.signup, name='signup'),
path('business_signup/', views.business_signup, name='business_signup'),
]
I have made for empty path, home and signin redirect to sign in page. Code is like below:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def default(request):
return render(request, "default.htm")
In the html page:
<p>New here? <a href="/signup">Create an account</a></p>
All the urls are working but the problem is when i use the {% url 'name' %}
in the href of the HTML page like
<p>New here? <a href="{% url 'signup' %}">Create an account</a></p>
All the redirections are changed to
http://127.0.0.1:8000/business_signup/signin/
http://127.0.0.1:8000/business_signup/signup/
http://127.0.0.1:8000/business_signup/business_signup/
I wanted to redirect {% url 'name' %}
like http://127.0.0.1:8000/signin/
and similar.
Any solutions?
2
Answers
proyect/urls should look something like this:
That’s it, no need to do it for every single URL.
Making a request to an url works fine, because in the moment of a request the urlpatterns are matched one-by-one and the resolver will find the second row in your pattern first and use it.
But if you use url tag with a name, then the names(!) of the urlpatterns are matched.
to do that at start of the server, the urlpattern is scanned entry by entry.
If you define names for view, then in case a name appears double, the last one is the one that will stay. This is what happens in your case because you include signin/urls.py several times.
In your case the
path('business_signup/', include('signin.urls'))
is the last one. So{% url 'signup' %}
will translate to../business_signup/signup/
if you just leave
everything will be ok
By the way it would be more logic to integrate /home/ in the base urls and not in singin urls: