Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正确获取django模型 我正在尝试获取模型的值,并使用for循环获取值,但它给了我这个 我要打印出来_Python_Python 3.x_Django_Django Models_Django Views - Fatal编程技术网

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)