无法在Django中获取Hello World

yfjy0ee7  于 2023-06-25  发布在  Go
关注(0)|答案(2)|浏览(112)

我有Python版本3.10.1和Django版本4.0

***项目url(name = home)***`

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

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

app中的url(name = hello)

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

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

app浏览量

from django.http import HttpResponse

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

我试着在www.example.com中添加和不添加'hello'的情况下运行服务器setting.py但我仍然只能得到默认页面。从3天卡住

jq6vz3qz

jq6vz3qz1#

在www.example.com home项目和hello app中有相同的代码。urls.py因此,您需要将hello app的urls.py更改为: 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),
]

在这种情况下,对http://localhost:8000/的请求将路由到应用程序的(hello)www.example.com文件中的索引函数。views.py file.

jutyujz0

jutyujz02#

在urls.pyapp的www.example.com文件中编写以下代码:

from django.urls import path

from . import views

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

相关问题