使用django时表单发布获取403禁止错误

时间:2020-08-10 13:54:34

标签: python django django-models django-forms csrf-token

<form  action="{% url 'create'%}" method="POST" >
{% csrf_token %}

这在我的模板文件中。

def create(request):
    return render(request, "auctions/create.html")
    if request.method == "POST":
        title = request.GET["title"]
        des = request.GET["description"]
        bid = request.GET["startingBid"]
        imageurl= request.GET[ "imageUrl"]
        category = request.GET["category"]
        image = request.GET["image"]
        listing= Auctionlisting(request,title=title,description=des,startingBid=bid,imageUrl=imageurl,category=category)
        return render(request, "auctions/index.html",{
        "listing":Auctionlisting.objects.all()
        })

,这是在我的views.py中。 在使用csrf令牌后,我仍然收到403禁止错误。请一些指导我。 这些标题,描述和所有内容都是我的输入...

1 个答案:

答案 0 :(得分:0)

只需重新排列代码,如下所示:

def create(request):
    if request.method == "POST":
        title = request.GET["title"]
        des = request.GET["description"]
        bid = request.GET["startingBid"]
        imageurl= request.GET[ "imageUrl"]
        category = request.GET["category"]
        image = request.GET["image"]
        listing= Auctionlisting(request,title=title,description=des,startingBid=bid,imageUrl=imageurl,category=category)
        listing.save() # save before getting them from database
        return render(request, "auctions/index.html",{
        "listing":Auctionlisting.objects.all()
        })
    else:
        return render(request, "auctions/create.html")