Я запутался во время рендеринга моего views.py "Django".
Мой vews.py:
если вы хотите, чтобы я поделился другой информацией, не стесняйтесь спрашивать!
def viewList(request, id):
# check for the watchlist
listing = Post.objects.get(id=id)
user = User.objects.get(username=request.user)
if listing.watchers.filter(id=request.user.id).exists():
is_watched = True
else:
is_watched = False
if not listing.activate:
if request.POST.get('button') == "Close":
listing.activate = True
listing.save()
else:
price = request.POST.get('bid', 0)
bids = listing.bids.all()
if user.username != listing.creator.username:
if price <= listing.price:
return render(request, 'auctions/item.html', {
"listing": listing,
'form': BidForm(),
"message": "Error! Your bid must be largest than the current bid!",
'comment_form': CommentForm(),
'comments': listing.get_comments.all(),
'is_watched': is_watched,
})
form = BidForm(request.POST)
if form.is_valid():
bid = form.save(commit=False)
bid.user = user
bid.save()
listing.bids.add(bid)
listing.bid = price
listing.save()
else:
return render(request, 'acutions/item.html', {'form'})
context = {
'listing': listing,
'comment_form': CommentForm(),
'comments': listing.get_comments.all(),
'is_watched': is_watched,
'form': BidForm()
}
return render(request, 'auctions/item.html', context)
внутри этого представления я добавил пунш требований моего проекта (комментарии/watchlist(bookmark)/и последняя вещь (то, с чем у меня много проблем) это система Bid), которая позволяет пользователям добавлять ставки на такие посты и позволяет создателю этого поста возможность закрыть его.... пожалуйста помогите я застрял в этой зоне, я много раз пытался понять! Примечание я новичок в Back-end разработке!
Ошибка, с которой вы столкнулись в представлении торгов, а представление, которым вы поделились, это список представлений, было бы лучше, если бы вы поделились представлением торгов и выделили строку ошибки/
Вообще я заметил, что в этой строке есть одна ошибка: user = User.objects.get(username=request.user)
Что должно быть : user = User.objects.get(username=request.user.username)
Надеюсь, это поможет вам немного
В вашем коде есть две проблемы, первая из них выделена Хусамом
user = User.objects.get(username=request.user.username)
а вторая - в операторе return в части else
render(request, 'acutions/item.html', {'form'})
вместо объекта context вы передаете строку, которая в python считается объектом set
и поэтому вы получаете ошибку None type
.
вот отрефакторенный код :-
def viewList(request, id):
# check for the watchlist
listing = Post.objects.get(id=id)
user = User.objects.get(username=request.user.username)
form = BidForm()
is_watched = listing.watchers.filter(id=request.user.id).exists():
context = {}
if not listing.activate:
if request.POST.get('button') == "Close":
listing.activate = True
listing.save()
else:
price = request.POST.get('bid', 0)
bids = listing.bids.all()
if user.username != listing.creator.username:
if price <= listing.price:
context.update({'message':"Error! your bid must be largest than the current bid !"})
else:
form = BidForm(request.POST)
if form.is_valid():
bid = form.save(commit=False)
bid.user = user
bid.save()
listing.bids.add(bid)
listing.bid = price
listing.save()
else:
return render(request, 'acutions/item.html', {'form': form})
context.update({'listing': listing,
'comment_form': CommentForm(),
'comments': listing.get_comments.all(),
'is_watched': is_watched,
'form': form})
return render(request, 'auctions/item.html', context)