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

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

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

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

urlpatterns = [  # <----- 错误出现在这里
    path('admin/', name='inventory' admin.site.urls),
    path('finance/', finance, name='finance'),
    path('home/', home, name='home'),  # 我试图将home_view函数与views函数连接起来。
    path('ingredients/', ingredients, name='ingredients'),
    path('menu/', menu, name='menu'),
    path('purchases/', purchases, name='purchases'),
]

views.py

def finance(request):
    return HttpResponse('这是我们的业务结果')

def home(request):
    return HttpResponse

def ingredients(request):
    return HttpResponse('这是食材页面')

def menu(request):
    return HttpResponse('这是菜单页面!')

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

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

urlpatterns = [ # <----- error is on this thing
    path('admin/',name ='inventory' admin.site.urls),
    path('finance/', finance, name='finance'),
    path('home/', home, name='home'), #I am attempting to connect the home_view function with the views function.
    path('ingredients/', ingredients, name='ingredients'),
    path('menu/', menu, name='menu'),
    path('purchases/', purchases, name='purchases'),]

views.py

def finance(request):
    return HttpResponse('Here are our business results')

def home(request):
    return HttpResponse

def ingredients(request):
    return HttpResponse('Here is the ingredients page')

def menu(request):
    return HttpResponse('Here is the menu page!')

def purchases(request):
    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在该路径上运行哪个视图。
看这个例子:

urlpatterns = [
    path(
        '',
        TemplateView.as_view(template_name='template/index.html'),
        name='index',
    ),
]
英文:

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:

urlpatterns = [
    path(
        '',
        TemplateView.as_view(template_name='template/index.html'),
        name='index',
    ),
]

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:

确定