我在Django应用程序上有一个复选框,用户可以在其中添加或删除监视列表中的列表。
然而,这个复选框总是返回False,并且从来没有在请求中。POST,我已经尝试了这么多的解决方案,从SO和整个互联网的字面天现在,不能弄清楚它
型号.py
class Watchlists(models.Model):
user = models.CharField(max_length=64, default='user')
title = models.CharField(max_length=64, blank=True)
watchlist = models.BooleanField(default=False, blank=False)
def __str__(self):
return f"{self.title}, {self.user}, {self.watchlist}"
表单.py
class CheckForm(ModelForm):
watchlist = forms.BooleanField(required=False)
# watchlist = forms.DecimalField(widget=forms.CheckboxInput(attrs={"value":"watchlist"}))
class Meta:
model = Watchlists
fields = ['watchlist']
复选框没有值,所以我认为这是问题所在,并试图在评论行中给予它一个值,它没有帮助
查看次数.py
watchlist = CheckForm(request.POST or None)
if request.method == 'POST':
# if request.POST['watchlist']:
# if 'watchlist' in request.POST:
# if request.POST.get('watchlist', False):
if request.POST.get('watchlist', '') == 'on':
if watchlist.is_valid():
check = watchlist.cleaned_data['watchlist']
watchlist_data = Watchlists.objects.all().filter(title=title, user=username).first()
if not watchlist_data:
watchlisted = Watchlists.objects.create(title=title, user=username, watchlist='True')
watchlisted.save()
if watchlist_data:
watchlist_data.delete()
I have tried all the different solutions i could find
**Template**
<form action="listing" method="POST">
{% csrf_token %}
{{ checkbox }}
</form>
It has a name and id attribute, label is fine too
**Entire views.py**
@login_required
def listing(request, title):
if request.user.is_authenticated:
username = request.user.get_username()
form = BidForm()
comment_form = CommentForm()
watchlist = CheckForm(request.POST or None)
listing_object = Listing.objects.all().filter(title=title).first()
author = listing_object.user
bids = Bid.objects.all().filter(title=title).values_list("price", flat=True)
max_bid = max(bids, default=0)
comments = Comment.objects.all().filter(list_title=title)
if request.method == "POST":
bid = Bid(title=title, user=username)
bidform = BidForm(request.POST, request.FILES, instance=bid)
# if request.POST['watchlist']:
# if 'watchlist' in request.POST:
# if request.POST.get('watchlist', False):
if request.POST.get('watchlist', '') == 'on':
if watchlist.is_valid():
check = watchlist.cleaned_data['watchlist']
watchlist_data = Watchlists.objects.all().filter(title=title, user=username).first()
if not watchlist_data:
watchlisted = Watchlists.objects.create(title=title, user=username, watchlist='True')
watchlisted.save()
if watchlist_data:
watchlist_data.delete()
if "price" in request.POST:
if bidform.is_valid():
price = bid.price
if not bids:
bid = bidform.save()
messages.success(request, 'Your bid has been placed succesfully')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
else:
max_bid = max(bids)
if price >= listing_object.price and price > max_bid:
bid = bidform.save()
messages.success(request, 'Your bid has been placed succesfully')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
else:
messages.warning(request, 'Bid price must be greater than highest bid and starting price')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
if "close" in request.POST:
bid = Bid.objects.all().filter(title=title, price=max_bid).first()
max_bid_user = bid.user
listing_object.tag = 'closed'
listing_object.save()
if username == max_bid_user:
messages.warning(request, 'Thank you for your entry into this auction. You have emerged the winner and this listing has been closed')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
comment = Comment(user_commented=username, list_title=title, list_author=author)
comment_form = CommentForm(request.POST, request.FILES, instance=comment)
if "comment" in request.POST:
if comment_form.is_valid():
user_comment = comment_form.save()
comments = Comment.objects.all().filter(list_title=title)
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
return render(request, "auctions/listing.html", {
"form": form,
"listing": listing_object,
"checkbox": watchlist,
"max_bid": max_bid,
"users": author,
"commentform": comment_form,
"comments": comments
})
网址.py
path("auctions/<str:title>/", views.listing, name="listing"),
1条答案
按热度按时间x8diyxa71#
首先,您不必在
watchlist
字段中添加blank=False
,因为您已经为它指定了默认值,因此可以如下重写这样,您还可以从forms.py中删除此内容。
只需按如下方式使用
根据@Sunderam Dubey的评论需要修复的其他问题
在HTML表单中,将
action="listing"
更改为action="{% url 'listing' %}"