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

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

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

问题

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

我的项目名称:web

应用程序名称:main

web.settings.py =>

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
]

web.urls.py =>

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

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

main.urls.py =>

from django.urls import path
from . import views

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

main.views.py =>

from django.http import HttpResponse

def index(request):
    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
=>

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
]

web.urls.py

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

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

main.urls.py

from django.urls import path
from . import views


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

main.views.py

from django.http import HttpResponse


def index(request):
    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文件中,你已经设置了:

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

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

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

In your main urls file you have set:

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

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:

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

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:

确定