Home > database >  cant get hello world in django
cant get hello world in django

Time:12-22

I have python version 3.10.1 and django version 4.0

url in project( name = home)`

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

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

url in app (name = hello)

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

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

views in app

from django.http import HttpResponse


   # Create your views here.
   def index(request):
      return HttpResponse("Hello World")

I tried running server with and without adding 'hello' in setting.py still i get only default page. stuck from 3 days

CodePudding user response:

In the urls.py file in app write the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

CodePudding user response:

You have the same code in urls.py home project as well as in hello app. For Django to use your new view, you need to tell Django the index view is the view you want to display when someone navigates to the site root (home page). So you need to change the urls.py of hello app as:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index),
]

In this case, a request to http://localhost:8000/ would route to the index function in the application’s (hello) views.py file.

  • Related