'Uploads' 对象在 Django 中不可迭代

'Uploads' object is not iterable in Django

我试图让每个模型对象都有自己的页面,所有其他模型对象都附加到它,使用模态 ID,我尝试使用 {{ img.queryset.all.id }} html标签来显示照片,但这没有用。我知道问题出在 views.py 或 single_page.html 中,也可能出在 models.py 中,但我认为这不太可能是问题所在。当我单击照片时,它会将其带到带有照片图标的页面,但由于照片未知,因此不会显示它。每次我使用 {% for x in img %} 时,它都会说 'Uploads' object is not integrable。如果有人能提供帮助那就太好了。

modals.py


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, null = False, blank = True)
    first_name = models.CharField(max_length = 50, null = True, blank = True)
    last_name = models.CharField(max_length = 50, null = True, blank = True)
    phone = models.CharField(max_length = 50, null = True, blank = True)
    email = models.EmailField(max_length = 50, null = True, blank = True)
    bio = models.TextField(max_length = 300, null = True, blank = True)
    profile_picture = models.ImageField(default = 'default.png', upload_to = "img/%y", null = True, blank = True)

    def __str__(self):
        return f'{self.user.username} Profile'



class Uploads(models.Model):
    title = models.CharField(max_length = 500, null = True,)
    artiste = models.CharField(max_length=500, null = True,)
    album = models.ForeignKey('Album', on_delete=models.SET_NULL,null=True,blank=True)
    time_length=models.DecimalField(max_digits=20, decimal_places=2,blank=True, null = True,)
    audio_file=models.FileField(upload_to='musics/',validators=[validate_is_audio], null = True,)

    caption = models.CharField(max_length = 100, blank=True)
    file = models.ImageField(upload_to = "img/%y", null = True)
    profile = models.ForeignKey(Profile, on_delete = models.CASCADE, default = None, null = True)
    id = models.AutoField(primary_key = True, null = False)


    def __str__(self):
        return str(self.file) and f"{self.id}"

class Album(models.Model):
    name=models.CharField(max_length=400)

views.py

def profile(request, user):
    img = Uploads.objects.filter(profile_id = request.user.profile)         #Make sure only your account *images stays on the page
    profile = Profile.objects.filter(user = request.user)
    context = {"profile": profile, "img": img}

    return render(request, "main/profile.html", context)

def single_page(request, id):
    img = Uploads.objects.filter(id = id).first()    
    profile = Uploads.objects.filter(id = request.user.id)

    context = { "img": img, "profile": profile}

    return render(request, "main/single_page.html", context)

single_page.html

{% block content %}
<body>

    {% for x in img %}
    <p>{{title}}</p>

    <img src="{{x.file.url}}" height="100%" width="100%"  class="myImg">
    {% endfor %}


</body>
{% endblock %}

由于您是按 id 过滤配置文件并且还指定 first ,因此不可能循环遍历,因为 first 会呈现第一个对象,因此循环仅适用于您添加 (img.all ) 通过做这样的事情:

# Your view.py 
def single_page(request, id):
    img = Uploads.objects.filter(id=id)    
    profile = Uploads.objects.filter(id = request.user.id)

    context = { "img": img, "profile": profile}

    return render(request, "main/single_page.html", context)

你的Html


{% block content %}
<body>
    {% for x in img.all %}
    <p>{{title}}</p>

    <img src="{{x.file.url}}" height="100%" width="100%"  class="myImg">
    {% endfor %}

</body>
{% endblock %}

如果 img 是一个对象,你不能迭代它,所以 for 循环不是有效的想法。您应该可以通过以下方式访问它的 url:{{ img.file.url }}.

不要使用 id 作为变量,它是内置函数。不要按 request.user.id 过滤 Uploads.id,因为它不会起作用。型号不同。

对于单张照片你应该在 views:

from django.shortcuts import get_object_or_404

def single_page(request, img_id):
    img = get_object_or_404(Uploads, id=img_id)

    context = {"img": img}

    return render(request, "main/single_page.html", context)

single_page.html:

{% block content %}
<body>

    <p>{{ img.title }}</p>

    <img src="{{ img.file.url }}" height="100%" width="100%"  class="myImg">

</body>
{% endblock %}

如果您有一张 User 的更多图片,您可以通过以下方式找到它们:

images = Uploads.objects.filter(profile=self.request.user.profile)
context = {"images": images}

并在模板中:

{% for img in images %}

    <p>{{ img.title }}</p>

    <img src="{{ img.file.url }}" height="100%" width="100%" class="myImg">

{% endfor %}

并且如果你想得到profile,不要用filter搜索它,因为你会得到QuerySet,而不是一个对象实例。要获得单个对象,您应该按照我之前向您展示的方式进行操作:

profile = get_object_or_404(Profile, user=request.user)
# or
profile = Profile.objects.get(user=request.user)

或者如果您真的非常喜欢 filter 方法:

profile = Profile.objects.filter(user=request.user).first()