Python 正确获取django模型 我正在尝试获取模型的值,并使用for循环获取值,但它给了我这个 我要打印出来
Python 正确获取django模型 我正在尝试获取模型的值,并使用for循环获取值,但它给了我这个 我要打印出来,python,python-3.x,django,django-models,django-views,Python,Python 3.x,Django,Django Models,Django Views,[,,,] 我的观点。py 我的模特 我正在使用django 1.11.29和python 3.8.5非常感谢! for item in all_titles: a.append(item.name) #Append only name. print(a) def watchlist(request): if request.method=='POST': name = User.objects.get(username=request.user.username)
[,,,]
我的观点。py我的模特
我正在使用django 1.11.29和python 3.8.5非常感谢!
for item in all_titles:
a.append(item.name) #Append only name.
print(a)
def watchlist(request):
if request.method=='POST':
name = User.objects.get(username=request.user.username)
title = request.POST['title']
print(name)
listing = Listing.objects.get(title=title)
all_titles = Watchlist.objects.filter(name=name)
print(all_titles)
print(title)
msg = "You already have this on your watchlist"
a = []
for item in all_titles:
a.append(item)
print(a)
if title in a:
return HttpResponseRedirect(reverse("index"))
else:
watchlist = Watchlist.objects.create(taitle=title, name=name, title=listing)
return HttpResponseRedirect(reverse("index"))
else:
user = request.user
watchlist = Watchlist.objects.filter(name=user)
return render(request, "auctions/watchlist.html", {
"watchlist": watchlist
})
class Watchlist(models.Model):
name = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
taitle = models.CharField(max_length=100, null=True)
title = models.ForeignKey(Listing, on_delete=models.CASCADE, null=True)
def __str__(self):
return f"{self.taitle}"
for item in all_titles:
a.append(item.name) #Append only name.
print(a)