skip to Main Content

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


  1. proyect/urls should look something like this:

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('signin.urls')),
    ]
    

    That’s it, no need to do it for every single URL.

    Login or Signup to reply.
  2. 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/

    urlpatterns = [
        path('admin/', admin.site.urls), 
        path('', include('signin.urls')),   # this one is the first one to match if e.g. you make a request to /signin/ 
        path('home/', include('signin.urls')), 
        path('signin/', include('signin.urls')), 
        path('signup/', include('signin.urls')), 
        path('business_signup/', include('signin.urls')), # this one is the last one and therefore relevant for finally defining names
    ]
    

    if you just leave

    urlpatterns = [
        path('admin/', admin.site.urls), #website.com/admin/
        path('', include('signin.urls')), #website.com/
    ]
    

    everything will be ok

    By the way it would be more logic to integrate /home/ in the base urls and not in singin urls:

    project/urls.py
    
    urlpatterns = [
        path('admin/', admin.site.urls), #website.com/admin/
        path('', include('signin.urls')), 
        path('home/', views.default, name='home')
    
    ]
    
    signin/urls.py
    
    urlpatterns = [
        path('signin/', views.default, name='signin'),
        path('signup/', views.signup, name='signup'),
        path('business_signup/', views.business_signup, name='business_signup'),
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search