【问题标题】:Django send request to API, where URL is DynamicDjango 向 API 发送请求,其中 URL 是动态的
【发布时间】:2020-01-20 04:16:19
【问题描述】:

网址的样子

 http://<IP_ADDR>:<PORT>/api/post/<POST_NUMBER>

这里 POST_NUMBER 是动态的,如何像这样向页面发送请求 http://127.0.0.1:8080/api/post/14 其中 14 是邮政编号 这是我的网址格式

path('api/post/<int>', views.PostView, name="Post")

这里是 PostView

def PostView(request,POST_NUMBER = 0):
       print(POST_NUMBER)

我的错误是 PostView() 得到了一个意外的关键字 'int'

【问题讨论】:

  • 你可以尝试写 id 而不是 POST_NUMBER = 0。然后当然打印 id

标签: django url dynamic python-3.6 django-urls


【解决方案1】:

试试这个

在网址中

path('api/post/<post_number>', views.PostView, name="Post") 
or 
path('api/post/<int:post_number>', views.PostView, name="Post")

在视图中

def PostView(request,post_number = 0):
       print(post_number)

参考this

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多