Home > Back-end >  Routing does not work in Django: page not found (GET)
Routing does not work in Django: page not found (GET)

Time:03-21

I've tried multiple methods of writing views but I don't think it is a problem here. App is installed in settings.py It displays error every time.

project structure: structure

views.py (app folder)

from django.http import HttpResponse
from django.shortcuts import render

def home_view(request):
    return HttpResponse('Hello World')

url.py in apps folder

from django.urls import path
from . import views

urlpatterns = [
    path('home_view/', views.home_view)
]

apps.py in app folder

from django.apps import AppConfig


class AppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'app'

urls.py in store folder

from django.contrib import admin
from django.urls import path, include

from app import views

urlpatterns = [
    path('app/home_view/', include('app.url')),
    path('admin/', admin.site.urls),
]

error message: error

CodePudding user response:

As a django web development expert I saw some small corrections to be done:

In store urls.py file its app/ and app.urls

from django.contrib import admin
from django.urls import path, include

from app import views

urlpatterns = [
    path('app/', include('app.urls')),
    path('admin/', admin.site.urls),
]

Then change the app url.py file name to standard name urls.py.

also don't forget to add your app to installed_apps variable in settings.py file:

INSTALLED_APPS=[
    'app',
    'django.contin.auth',
    #and other already specified apps
]

Rest all files have no bugs!!

According to this the correct path for HttpResponse is:

http://localhost:8000/app/home_view/

OR

http://127.0.0.1:8000/app/home_view/

CodePudding user response:

In your urls.py file

Change

path('app/home_view/', include('app.url')),

To

path('', include('app.url')),

Then

On your browser go to: 127.0.0.1:8000/home_view/

CodePudding user response:

The main thing it must be app.urls not app.url. change your file to url.py to urls.py, its recommended.

If you have defined path('app/home_view/', include('app.urls')) in urls.py of your store folder, then it goes to your urls.py which is in app.

In your app's urls.py you have written path('home_view/',views.home_view).

It means if you type 127.0.0.1:8000/app/home_view/home_view/ then it will render your HttpResponse that is Hello world.

  • Related