首页 > 解决方案 > 调试在 VS Studio Code Django 中不起作用

问题描述

我一直在使用 Visual Studio Code 中的官方 Django 教程,但遇到了一个问题。目前,当我在该行设置断点时,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"),
]

我的意见.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)

标签: pythondjangovisual-studio-codevisual-studio-debugging

解决方案


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


推荐阅读