Home > Blockchain >  Django - User not passing through form
Django - User not passing through form

Time:08-24

I am making an auction site and I try passing the user who posted a bid on a listing through a form. I already asked this about passing the creator of a listing and I tried the same method but I cannot manage to do it.

My view looks something like this (I shortened it because it's very long):

def show_listing(request, listing_id):
     listing = AuctionListing.objects.get(id=listing_id)
     bidding = listing.bidlisting.last()
     if bidding is None:
        field_name = "starting_bid"
        starting_bid = getattr(listing, field_name)
        createbidform = CreateBid(initial={"bids": starting_bid, "user":request.user})
     else:
        field_name2 = "bids"
        highest_bid = getattr(bidding, field_name2)
        createbidform = CreateBid(initial={"bids": highest_bid, "user":request.user})
     if request.method == "POST":
        form = CreateBid(request.POST)
            if bidding is None and float(form['bids'].value()) >= float(starting_bid):
                if form.is_valid():
                        message = "Your bid is placed"
                        form.instance.listing = listing
                        form.save()
                        createbidform = CreateBid(initial={"bids": form['bids'].value(), "user":request.user})
                        amount_bids = len(Bid.objects.filter(listing=listing_id))
                        return render(request, "auctions/listing.html", {
                         "createbidform" : createbidform
                        })
                else: 
                    print(form.errors)
     return render(request, "auctions/listing.html", {
                        "bidform" : createbidform
                        })

listing.html looks something like this:

<form method="POST">
        {% csrf_token %}
            &#36;{{ bidform.bids }}
            <button type="submit" name="bidbid" >Place your bid</button>
        
    </form>

RIGHT NOW form.errors prints:

<ul ><li>user<ul ><li>This field is required.</li></ul></li></ul>

Here is the model Bid:

class Bid(models.Model):
    listing = models.ForeignKey(AuctionListing, on_delete=models.CASCADE, related_name="bidlisting")
    bids = models.DecimalField(max_digits=6, decimal_places=2)
    user = models.ForeignKey(User, on_delete=models.CASCADE, db_constraint=False, related_name="userrr")
    def __str__(self):
        return str(self.bids)

And here is the form CreateBid:

class CreateBid(forms.ModelForm):
    class Meta:
        model = Bid
        fields = ('bids', 'user')
        widgets = {
            'user': forms.HiddenInput(),
        }

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user')
        super().__init__(*args, **kwargs)
       
        self.fields['user'].initial = user.id

For some reason it doesn't provide the user who posted the bidding to the form, causing the form to not be valid. How to fix this?

CodePudding user response:

You already do it with bids so you don't need extra kwarg:

 createbidform = CreateBid(initial={'bids': starting_bid, 'user': request.user})

you can remove __init__ method form CreateBid form class

Template:

<form method="POST">
        {% csrf_token %}
            &#36;{{ bidform }}
            <button type="submit" name="bidbid" >Place your bid</button>
        
    </form>
  • Related