Trying to get Django to load the server without using the paths I created. The default path is not working

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

Trying to get Django to load the server without using the paths I created. The default path is not working

问题

我的urls.py不允许Django运行服务器。

当我将鼠标悬停在红色波浪线上面的时候,Visual Studio Code显示的错误信息是:“[”没有关闭。

有人可以解释一下这个错误信息是什么意思,以及如何解决它吗?

另外,我正在尝试让Django服务器自行运行。但是它无法打开主页。在这个错误出现之前,当我在“/finance”或“/ingredients”之后放置URL时,它能够运行其他路径。我确信错误与阻止Django加载服务器有关。

urls.py

  1. from django.contrib import admin
  2. from django.urls import include, path
  3. from django.http import HttpResponse
  4. urlpatterns = [ # <----- 错误出现在这里
  5. path('admin/', name='inventory' admin.site.urls),
  6. path('finance/', finance, name='finance'),
  7. path('home/', home, name='home'), # 我试图将home_view函数与views函数连接起来。
  8. path('ingredients/', ingredients, name='ingredients'),
  9. path('menu/', menu, name='menu'),
  10. path('purchases/', purchases, name='purchases'),
  11. ]

views.py

  1. def finance(request):
  2. return HttpResponse('这是我们的业务结果')
  3. def home(request):
  4. return HttpResponse
  5. def ingredients(request):
  6. return HttpResponse('这是食材页面')
  7. def menu(request):
  8. return HttpResponse('这是菜单页面!')
  9. def purchases(request):
  10. return HttpResponse('这是订单历史记录页面!')

我尝试了什么来解决这个问题?
我尝试查看Stack Overflow上的示例。
我尝试在Django Discord上寻求帮助。
我尝试在互联网上研究错误信息。
我尝试参加了一个类似Udemy的Django课程。
Trying to get Django to load the server without using the paths I created. The default path is not working
1: https://i.stack.imgur.com/t6a4Y.png

英文:

my urls.py will not allow Django to run the server.

The error message visual studio code gives when I hover over the red squiggly line that visual studio gives over the URL patterns [ is: "[" was not closed.

Can anyone explain to me what that error message means and how to solve it?

Also, I am trying to get my Django server to run the server on its own. But it will not open the main page. It was able to run the other paths when I put the URL after the /'finance' or /'ingredients' respectively but that was before the bug popped up. I am certain the error is tied to preventing Django to load the server.

urls.py

  1. from django.contrib import admin
  2. from django.urls import include, path
  3. from django.http import HttpResponse
  4. urlpatterns = [ # <----- error is on this thing
  5. path('admin/',name ='inventory' admin.site.urls),
  6. path('finance/', finance, name='finance'),
  7. path('home/', home, name='home'), #I am attempting to connect the home_view function with the views function.
  8. path('ingredients/', ingredients, name='ingredients'),
  9. path('menu/', menu, name='menu'),
  10. path('purchases/', purchases, name='purchases'),]

views.py

  1. def finance(request):
  2. return HttpResponse('Here are our business results')
  3. def home(request):
  4. return HttpResponse
  5. def ingredients(request):
  6. return HttpResponse('Here is the ingredients page')
  7. def menu(request):
  8. return HttpResponse('Here is the menu page!')
  9. def purchases(request):
  10. return HttpResponse('Here is where you can find the order history!')

Trying to get Django to load the server without using the paths I created. The default path is not working

What have I tried to do to solve the problem?
I have tried looking at stack overflow examples.
I have tried reaching out on the Django discord.
I have tried researching the error message on the internet.
I have tried looking at a Django course on an udemy equivalent.

答案1

得分: 0

我看到你已经修复了未关闭的[]问题。

似乎在URL声明中缺少根路径"/"的定义。
你需要明确告诉Django在该路径上运行哪个视图。
看这个例子:

  1. urlpatterns = [
  2. path(
  3. '',
  4. TemplateView.as_view(template_name='template/index.html'),
  5. name='index',
  6. ),
  7. ]
英文:

I see that you already fixed the issue with unclosed [].

It seems that the path for the root "/" is missing in the urls declaration.
You need to explicitly tell django which view you want to run in that path.
Look this example:

  1. urlpatterns = [
  2. path(
  3. '',
  4. TemplateView.as_view(template_name='template/index.html'),
  5. name='index',
  6. ),
  7. ]

huangapple
  • 本文由 发表于 2023年6月1日 11:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378539.html
匿名

发表评论

匿名网友

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

确定