Django url dispatcher 调用了错误的函数

Django url dispatcher calls the wrong function

我遇到的问题是: 我在我的 urls.py 文件中创建了一个新的 re_path,但是当我在那个 url 发出请求时调用了错误的函数。

# myapp/urls.py
from django.urls import path, re_path
from . import views as multiplayer_lite_views

urlpatterns = [
    # other paths

    re_path(r'vote/(?P<match_id>\w{16})', multiplayer_lite_views.vote, name='multiplayer_lite_vote'),
    re_path(r'nightvote/(?P<match_id>\w{16})', multiplayer_lite_views.night_vote, name='multiplayer_lite_night_vote'),
    path('new-match/', multiplayer_lite_views.new_match, name='multiplayer_lite_new_match'),
    path('', multiplayer_lite_views.home, name='multiplayer_lite_home'),
]

我所做的只是复制行 re_path(r'vote/... 并将其重命名为 re_path(r'nightvote/... 但也更改了所有其他信息,例如 multiplayer_lite_views.votemultiplayer_lite_views.night_vote.

问题是,当我转到此 url nightvote/ 时,函数 vote 被调用。

# myapp/views.py

def vote(request, match_id):
    print('vote function')
    # do other stuff
    return return JsonResponse(...)

def night_vote(request, match_id):
    print('nightvote function')
    # do other stuff
    return return JsonResponse(...)

在服务器端我看到的是:

...
vote function
[18/Mar/2020 10:19:16] "POST /nightvote/gfvkpvhlwlqzosae HTTP/1.1" 200 16
...

PS我已经尝试关闭Django并重新打开,vs代码也是如此。

改变你的 url re_path 如下:

re_path(r'^vote/(?P<match_id>\w{16})$', multiplayer_lite_views.vote, name='multiplayer_lite_vote'),
re_path(r'^nightvote/(?P<match_id>\w{16})$', multiplayer_lite_views.night_vote, name='multiplayer_lite_night_vote'),

我遇到了这个问题,这是因为 ^