我按照 Django 示例的步骤进行了操作,但它不起作用。

huangapple go评论85阅读模式
英文:

I followed the django example exactly. But it doesn't work

问题

我按照django的示例完全操作了。但它不起作用。网页只是给我显示火箭。

我的项目名称:web

应用程序名称:main

web.settings.py =>

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'main',
  9. ]

web.urls.py =>

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. urlpatterns = [
  4. path('main/', include('main.urls')),
  5. path('admin/', admin.site.urls),
  6. ]

main.urls.py =>

  1. from django.urls import path
  2. from . import views
  3. urlpatterns = [
  4. path('', views.index, name='index'),
  5. ]

main.views.py =>

  1. from django.http import HttpResponse
  2. def index(request):
  3. return HttpResponse("Hello, world. You're at the polls index.")

我希望网页对我显示 "Hello, world. You're at the polls index."。

英文:

**
I followed the django example exactly. But it doesn't work. Web page just show the rocket to me.**

my project name: web

app name: main

web.settings.py
=>

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'main',
  9. ]

web.urls.py

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. urlpatterns = [
  4. path('main/', include('main.urls')),
  5. path('admin/', admin.site.urls),
  6. ]

main.urls.py

  1. from django.urls import path
  2. from . import views
  3. urlpatterns = [
  4. path('', views.index, name='index'),
  5. ]

main.views.py

  1. from django.http import HttpResponse
  2. def index(request):
  3. return HttpResponse("Hello, world. You're at the polls index.")

I hope web page shows "Hello, world. You're at the polls index." to me.

答案1

得分: 1

在你的主要URL文件中,你已经设置了:

  1. urlpatterns = [
  2. path('main/', include('main.urls')),
  3. path('admin/', admin.site.urls),
  4. ]

这实际上意味着,你需要在浏览器中访问 localhost:8000/main 来访问"main"应用中的URL。如果你想在浏览器中不指定main来访问,那么将其更改为:

  1. urlpatterns = [
  2. path('', include('main.urls')),
  3. path('admin/', admin.site.urls),
  4. ]
英文:

In your main urls file you have set:

  1. urlpatterns = [
  2. path('main/', include('main.urls')),
  3. path('admin/', admin.site.urls),
  4. ]

Which actually means, that you need to go for localhost:8000/main for your urls in "main" app. If you want to do it without specifying main in the url in your browser, then change it to:

  1. urlpatterns = [
  2. path('', include('main.urls')),
  3. path('admin/', admin.site.urls),
  4. ]

huangapple
  • 本文由 发表于 2023年2月14日 20:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定