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

this in my template file.

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()
        })

and this is in my views.py. still after using csrf token i am getting 403 forbidden error. please some guide me. Also these title, description and all are my inputs...

Haa
  • 25
  • 4
  • this if statment will never be evaluated, everything after `return` is lost. Did you check your consoles for errors (js and terminal)? do you go to the right function/url? – hansTheFranz Aug 10 '20 at 14:20

1 Answers1

0

Just re-arrange your code like this:

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

Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17
  • you probably also need to change all your `request.GET` to `request.POST`. For MultiValueDictKeyError you need to give more explaination about what you are sending in POST request and your model. But hope this can help https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it. (answering about his MultiValuedDictKeyError that he mentioned in an answer) – Mohamed Wagdy Oct 15 '20 at 14:19