简体   繁体   中英

cant get hello world in django

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

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'),
]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM