Django url调度程序调用了错误的函数

问题描述 投票:1回答:1

我的问题是这个:我在re_path文件中创建了一个新的urls.py,但是当我在该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代码相同。

python django url routing
1个回答
1
投票

如下更改您的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'),

我有这个问题,这是因为^

© www.soinside.com 2019 - 2024. All rights reserved.