How to map URLs to views in django?

Question:

Goal:

127.0.0.1:8000/about

Should give:

welcome to to about us page

127.0.0.1:8000/contact

Should give:

welcome to contact page

Problem:

Both are giving "page not found at /"

This error:

https://imgur.com/a/P0uWhWl

This is my folder structure:

https://imgur.com/a/84uc5Pw

This is my main project "taskmate"’s urls.py:

from django.contrib import admin

from django.urls import path, include



urlpatterns = [

    path('admin/', admin.site.urls),

    path('task/',include('todolist_app.urls')),

    path('todolist/',include('todolist_app.urls')),        

       
    ]

And for todolist_app, these are my details:

Urls.py:

from django.urls import path

from todolist_app import views

#from . import views    



urlpatterns = [

    path('', views.todolist),

    path('contact', views.contact,name='contact'),

    path('about', views.about, name="about"), #the path can be anything.

]

Views.py:

from django.shortcuts import render

from django.http import HttpResponse



# Create your views here.

def todolist(request):

    context={'welcome_text':"welcome to todo list app"}

    return render(request, 'html/todolist.html',context)



def contact(request):

    context={'welcome_text':"welcome to contact page"}

    return render(request, 'html/contact.html',context)



def about(request):

    context={'welcome_text':"welcome to to about us page"}

    return render(request, 'html/about.html',context)

I’m suspecting that the problem is in my main project urls.py as I’ve not included any details about contact and about URLs there. So, I did this there.

from django.contrib import admin

from django.urls import path, include



urlpatterns = [   


    path('admin/', admin.site.urls),

    path('task/',include('todolist_app.urls')),

    path('todolist/',include('todolist_app.urls')),

    path('about/',include('todolist_app.urls')), 




    

]

The website runs but the goal isn’t achieved. Which is obvious because about/ is getting mapped to same URL like todolist/

It shows what the URLs

http://127.0.0.1:8000/task/

And

http://127.0.0.1:8000/todolist/

were Showing instead of what it should be showing.

Now, I think I need to fix the todolist_app urls.py

from django.urls import path

from todolist_app import views

#from . import views





urlpatterns = [

    path('', views.todolist),

    path('contact', views.contact,name='contact'),

    path('about', views.about, name="about"), #the path can be anything.

]

I’m not sure how to fix that. The first thing that I don’t understand there is

    path('', views.todolist),

should mean any urls 127.0.0.1:8000/ should be giving "welcome to todolist app", but only 127.0.0.1:8000/todolist gives it.

And the views mapping for "contact" and "about" are clearly not working.

How do I fix this issue?

Asked By: user20227569

||

Answers:

So to achieve your goal, change the projec’s url file as:

    path('admin/', admin.site.urls),

    path('task/',include('todolist_app.urls')),

    path('',include('todolist_app.urls')), # change as this

Then you can get the following things:

127.0.0.1:8000/about showing "welcome to about page"

127.0.0.1:8000/contact showing "welcome to contact page"

127.0.0.1:8000/ showing "welcome to todolist app page"

The problem here was in project url file when you add path('todolist/',include('todolist_app.urls')), every urls in the todolist app will be prefixed with the given string in project url file.

You can refer Django – URL Mapping for more.

Answered By: ilyasbbu

Add a slash after your route:

from django.urls import path

from todolist_app import views

urlpatterns = [

    path('', views.todolist),

    path('contact/', views.contact,name='contact'),

    path('about/', views.about, name='about'), #the path can be anything.

]
Answered By: Iqbal Hussain
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.