【问题标题】:form post getting 403 forbidden error while using django使用 django 时表单帖子出现 403 禁止错误
【发布时间】:2020-08-10 13:54:34
【问题描述】:
<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 禁止错误。请指导我。 这些标题、描述和所有内容都是我的输入......

【问题讨论】:

  • 如果语句永远不会被评估,return 之后的所有内容都会丢失。你检查你的控制台是否有错误(js 和终端)?你去正确的功能/网址吗?

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


【解决方案1】:

只需像这样重新排列您的代码:

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")    

【讨论】:

  • 您可能还需要将所有request.GET 更改为request.POST。对于 MultiValueDictKeyError,您需要详细说明您在 POST 请求中发送的内容和您的模型。但希望这可以帮助stackoverflow.com/questions/5895588/…。 (回答他在回答中提到的 MultiValuedDictKeyError)
猜你喜欢
  • 2021-10-03
  • 2012-05-20
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-07-15
  • 2015-11-13
  • 1970-01-01
  • 2016-01-12
相关资源
最近更新 更多