2021年1月19日星期二

Django '>' not supported between instances of 'float' and 'NoneType'

I'm creating a site where users can bid on items, similar to ebay. For the actual view to allow them to bid (put a dollar amount in a form) I am having trouble getting the number to save. Currently though I'm getting a type error: '>' not supported between instances of 'float' and 'NoneType'

I have tried doing int(float(my code)) but I'm not sure how to convert it into an int. I have tried many different ways but always get an error. If anyone can help me with the code I'd really appreciate it. I'm very new to Django.

Views.py

def new_bid(request, listingid):      if request.method == "POST":          listing = Listings.objects.get(pk=listingid)          response = redirect("listingpage", listingid=listingid)          try:              bid = int(request.POST["bid"])          except ValueError:              response.set_cookie(                  "message", "Please input something before submitting the bid", max_age=3              )              return response          if bid > listing.current_price():              response.set_cookie("message", "Your bid was accepted", max_age=3)              Bid.objects.create(bid=bid, listing=listing, user=request.user)          else:              response.set_cookie(                  "message", "Your bid should be higher than the current bid", max_age=3              )          return response      else:          return redirect("index")  

models.py

class Listings(models.Model):        listingid = models.IntegerField(blank=True, null=True)      starting_bid = models.IntegerField()      bids = models.ManyToManyField('Bid', related_name='bids_in_the_auction', blank=True)      last_bid = models.ForeignKey('Bid', on_delete=models.CASCADE,   related_name='last_bid_for_the_auction', blank=True,                               null=True)        def current_price(self):          return self.listing_for_bid.last().bid      class Bid(models.Model):        user = models.CharField(max_length=64)      title = models.CharField(max_length=64)      bid = models.IntegerField(blank=True, null=True, default='')      listingid = models.ForeignKey('Listings', on_delete=models.CASCADE,   related_name='listing_for_bid')  
https://stackoverflow.com/questions/65802946/django-not-supported-between-instances-of-float-and-nonetype January 20, 2021 at 12:07PM

没有评论:

发表评论