V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SimbaPeng
V2EX  ›  Python

Django 怎么限制 url 的请求方式?类似 flask 的 app.route('/', methods=['POST'])

  •  
  •   SimbaPeng · 2017-12-15 11:28:37 +08:00 · 3516 次点击
    这是一个创建于 2296 天前的主题,其中的信息可能已经有所发展或是发生改变。
    7 条回复    2017-12-15 20:43:46 +08:00
    myyou
        1
    myyou  
       2017-12-15 11:34:50 +08:00
    django rest framework 有类似 flask 写法,也可以通过在 View 类定义请求方法来限制
    manzhiyong
        3
    manzhiyong  
       2017-12-15 11:41:40 +08:00
    你不在 view 里写 post 接收方法不就完了
    vZexc0m
        4
    vZexc0m  
       2017-12-15 13:16:25 +08:00
    ```
    @require_http_methods(['GET'])
    ```
    iiduce
        5
    iiduce  
       2017-12-15 13:22:19 +08:00
    view 里:

    if request.method == 'POST':
    post = request.POST
    ...
    Nick2VIPUser
        6
    Nick2VIPUser  
       2017-12-15 14:36:15 +08:00
    ```
    class TestView(View):
    def get(self, request):
    ......
    def post(self, request):
    ......
    ```
    ZnZt
        7
    ZnZt  
       2017-12-15 20:43:46 +08:00
    from django.views.decorators.http import require_POST, require_GET

    @require_GET
    def query():
    pass

    @require_POST
    def add():
    pass
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2893 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:16 · PVG 19:16 · LAX 04:16 · JFK 07:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.