django 請(qǐng)求

返回json數(shù)據(jù)格式

from json import dumps
......
@csrf_exempt
def createPosting(request):
    if request.method == 'POST':
        try:
            title=request.POST['title']
            req={'message':'發(fā)帖成功','title':title}
            return HttpResponse(dumps(req),content_type="application/json")
        except:
            req={'message':'發(fā)帖失敗'}
            return HttpResponse(dumps(req),content_type="application/json")

django post 請(qǐng)求 403

在開(kāi)頭添加

from django.views.decorators.csrf import csrf_exempt

在使用POST的函數(shù)前添加@csrf_exempt

示例

from django.shortcuts import render
from django.http import HttpResponse
from .models import Post,Comment
from django.views.decorators.csrf import csrf_exempt


def index(request):
    html = "<html><body><h1>index</h1></body></html>"
    return HttpResponse(html)

@csrf_exempt
def getPostByTitle(request,postTitle):
    if request.method == 'GET':
        post=Post.GetPost(title=postTitle)
        if post=='查詢錯(cuò)誤':
            html='查詢錯(cuò)誤'
            return HttpResponse(html)

        comment = post.comment_set.all() #評(píng)論集
        comment = comment.order_by('-createDate') #按照發(fā)送時(shí)間排序
        data={
            'post':post,
            'comment':comment
        }

        return render(request, 'posting/post.html', data)
    elif request.method == 'POST':
        return HttpResponse('請(qǐng)求成功')

現(xiàn)在使用POST方式調(diào)用該函數(shù)可得到返回 請(qǐng)求成功

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容