python 在VS Studio Code Django中无法调试

acruukt9  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(249)

我一直在通过official Django Tutorial in Visual Studio Code工作,但我遇到了一个问题。目前,当我在now = datetime.now行设置断点时,调试器似乎无法到达它。
我的urls.py:

from django.urls import path
from hello import views

urlpatterns = [
path("", views.home, name="home"),
path("hello/<name>", views.hello_there, name="hello_there"),
]

我的views.py:

import re
from django.utils.timezone import datetime
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django!")

def hello_there(request, name):
    now = datetime.now()
    formatted_now = now.strftime("%A, %d %B, %Y at %X")

    # Filter the name argument to letters only using regular expressions. URL arguments
    # can contain arbitrary text, so we restrict to safe characters only.
    match_object = re.match("[a-zA-Z]+", name)

    if match_object:
        clean_name = match_object.group(0)
    else:
        clean_name = "Friend"

    content = "Hello there, " + clean_name + "! It's " + formatted_now
    return HttpResponse(content)
ldxq2e6h

ldxq2e6h1#

你必须在调试器到达断点之前开始运行服务器。一旦你按下运行等待,直到它完成,并转到http://127.0.0.1:8000/hello/VSCode。之后,调试器将转到您的断点。

new9mtju

new9mtju2#

以管理员身份运行VS Code解决了我的问题

相关问题