Form based on ModelForm in function

I have the following model

class Bid(models.Model):
    listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name='bids', related_query_name='bid')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bid_users', related_query_name='bid_user')
    price = models.DecimalField(max_digits=8, decimal_places=2)

In I have a form based on a ModelForm

class AddBidForm(ModelForm):
    class Meta:
        model = models.Bid
        fields = (

In I have

def ListingDetail(request, pk):
    bid_form = forms.AddBidForm(initial={'listing': pk})

And I have a function

def AddBid(request):
    if request.method == "POST":
        form = forms.AddBidForm(request.POST)
        form.instance.user_id =

        listing_id = request.POST['listing']

        if form.is_valid():


        return HttpResponseRedirect(reverse("auctions:listing-detail", args=[listing_id]))

I’m not sure I did all this correctly, so any comments are very much appreciated.
The problem is that listing_id = request.POST['listing'] doesn’t assign a value to listing_id
In the template I have

<form action="{% url 'auctions:add-bid' %}" method="post">
    {% csrf_token %}
    <div class="form-floating mb-3">
      <input type="text" id="floatingPrice" class="form-control" name="price" placeholder="Your bid in $">
      <label for="floatingPrice">Your bid in $</label>
    <input class="btn btn-outline-secondary btn-sm" type="submit" value="Place bid">

Is the problem caused by not having listing in the template?



That is correct.

When you’re using a form, you generally want to ensure that you’re rendering all the form fields (e.g. {{ form.listing }} and {{ form.price }}) if you’re not rendering the entire form ({{ form}} - precise naming depending upon how you’re passing the form to the template)

Hi Ken,

Thanks for your reply.

Adding widgets = { 'listing': HiddenInput(), } solved the problem.