Django自定义验证导致视图返回None
我正在制作一个拍卖应用程序,需要检查表单上输入的出价金额是否等于或大于起始出价(如果有0个出价),或者是否大于当前出价 我使用clean()进行自定义验证: 在views.py中:Django自定义验证导致视图返回None,django,django-models,django-forms,Django,Django Models,Django Forms,我正在制作一个拍卖应用程序,需要检查表单上输入的出价金额是否等于或大于起始出价(如果有0个出价),或者是否大于当前出价 我使用clean()进行自定义验证: 在views.py中: def listing_page(request, listing_id): listing_selected = Listing.objects.get(pk=listing_id) if request.method == "POST": # Since I d
def listing_page(request, listing_id):
listing_selected = Listing.objects.get(pk=listing_id)
if request.method == "POST":
# Since I don't want the user to have to fill out a field specifying what listing page
# they're on, and since I don't have access to that information in the model class
# definition, I did that here:
request.POST._mutable = True
form = BidForm(request.POST)
form.data['listing'] = listing_selected
if form.is_valid():
# ...process clean data...
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
现在,当我使用此自定义验证正在检查的值提交表单时,我得到了“ValueError:view auctions.views.listing_页面没有返回HttpResponse对象,而是返回了None”。我检查了is_valid()是否使用print语句计算为False。如果我提交了一个默认的无效值,如10.1111或“abcd”,我会收到正确的错误消息。当is_valid()由于我的自定义验证而计算为False时,不应该发生这种情况吗?我错过了什么
编辑:根据要求,完整视图(我想我最初包括了所有相关部分,但谁知道呢):
也可根据要求使用html:
<form class="bidform" action="{% url 'auctions:listing_page' listing.id %}" method="POST">
{% csrf_token %}
{{ form.amount }}
<input class="button bid_button" type="submit" value="Place Bid" name="make_bid">
</form>
如果我在is_valid()调用中执行“单步执行”和“单步执行”,就会发生这种情况。为什么会这样做?这是您的完整视图?向我们展示您的模板too@binpy我为您添加了整个视图。@BiploveLamichhane我为您添加了html。在html中,
,您的完整视图是}
之后的是什么too@binpy我为您添加了整个视图。@BiploveLamichhane我为您添加了html。在html中,
,在POST
def listing_page(request, listing_id):
listing_selected = Listing.objects.get(pk=listing_id)
bids = Bid.objects.filter(listing=listing_selected)
if request.method == "POST":
if request.POST.get('add_to_watchlist') and request.user.is_authenticated:
request.user.person.watchlist.add(listing_selected)
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
elif request.POST.get('remove_from_watchlist') and request.user.is_authenticated:
request.user.person.watchlist.remove(listing_selected)
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
elif (request.POST.get('close_listing') and request.user.is_authenticated
and request.user == listing_selected.user):
listing_selected.active = False
listing_selected.save()
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
elif request.POST.get('make_comment') and request.user.is_authenticated:
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = Comment()
comment.user = request.user
comment.listing = listing_selected
comment.comment = comment_form.cleaned_data['comment']
comment.save()
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
elif request.POST.get('make_bid') and request.user.is_authenticated:
request.POST._mutable = True
form = BidForm(request.POST)
form.data['listing'] = listing_selected
if form.is_valid():
bid = Bid()
bid.listing = listing_selected
bid.user = request.user
bid.amount = form.cleaned_data['amount']
bid.save()
listing_selected.current_bid = bid
listing_selected.save()
request.session['bid_success_message'] = "Bid placed successfully."
return HttpResponseRedirect(reverse('auctions:listing_page', args=(listing_id,)))
else:
form = BidForm()
comment_form = CommentForm()
winning = False
if request.user.is_authenticated:
person = Person.objects.filter(user=request.user).filter(watchlist=listing_selected)
else:
person = False
if listing_selected.current_bid and listing_selected.current_bid.user == request.user:
winning = True
comments = Comment.objects.filter(listing=listing_selected)
category = None
if listing_selected.category:
for value, label in Listing.Category.choices:
if value == listing_selected.category:
category = label
context = {
"listing": listing_selected,
"person": person,
"num_bids": len(bids),
"winning": winning,
"form": form,
"comment_form": comment_form,
"comments": comments,
"category": category
}
if "bid_fail_message" in request.session:
message = request.session['bid_fail_message']
del request.session['bid_fail_message']
messages.error(request, message)
if "bid_success_message" in request.session:
message = request.session['bid_success_message']
del request.session['bid_success_message']
messages.success(request, message)
return render(request, "auctions/listing_page.html", context)
<form class="bidform" action="{% url 'auctions:listing_page' listing.id %}" method="POST">
{% csrf_token %}
{{ form.amount }}
<input class="button bid_button" type="submit" value="Place Bid" name="make_bid">
</form>
class Listing(models.Model):
def __str__(self):
return f"Listing '{self.title}' created by user '{self.user.username}' \
on {self.time_created.date()}"