简体   繁体   English

使用 Django 显示图像

[英]Using Django to display images

I am attempting to use Django to display 3 random images from a large list of user submitted images.我正在尝试使用 Django 从大量用户提交的图像中显示 3 个随机图像。 Each image has other values associated with it like the author, so I've been making each image a model with the image field holding the image itself.每个图像都有与作者相关的其他值,因此我一直将每个图像制作为模型,图像字段包含图像本身。 Unfortunately, I can't figure out how to randomly pick from that list and pass it in to the .html page to display it correctly.不幸的是,我无法弄清楚如何从该列表中随机选择并将其传递到 .html 页面以正确显示它。


So far, I've been able to get user submitted pictures to be saved as models and submitted to a folder /media/images/ .到目前为止,我已经能够将用户提交的图片保存为模型并提交到文件夹/media/images/
My views.py will grab 3 random and unique pictures from that folder like so:我的views.py将从该文件夹中抓取 3 张随机且独特的图片,如下所示:

def home_view(request):
    form = MForm(request.POST or None)
    if form.is_valid():
        form.save()

    message_list = MModel.objects.all()

    #Get first random file
    file1choice = random.choice(os.listdir(DIR))

    #Get second random file
    file2choice = random.choice(os.listdir(DIR))
    while(file2choice == file1choice):
        file2choice = random.choice(os.listdir(DIR))

    #Get third random file
    file3choice = random.choice(os.listdir(DIR))
    while(file3choice == file1choice or file3choice == file2choice):
        file3choice = random.choice(os.listdir(DIR))


    context = {
        'file1': file1choice,
        'file2': file2choice,
        'file3': file3choice,
    }

    return render(request, "home.html", context)



In my .html file that the user sees, I access each image passed like so:在用户看到的 .html 文件中,我访问每个传递的图像,如下所示:
<img src="/media/images/{{ file1 }}" width="300" height="180" alt="">


Unfortunately, this doesn't allow me to actually access the model of the image itself, which means I can't also display the associated values of the image.不幸的是,这不允许我实际访问图像本身的模型,这意味着我也无法显示图像的关联值。 I'm just grabbing random stuff using python.我只是使用 python 抓取随机的东西。


So, my question is how would I pass along to the .html file 3 random image models and how would I display that in the .html itself?所以,我的问题是我将如何传递给 .html 文件 3 个随机图像模型,以及如何在 .html 本身中显示它?



models.py模型.py

from django.db import models

class MModel(models.Model):
    Message = models.TextField(max_length=200)
    Img = models.ImageField(upload_to='images/')
    Author = models.CharField(max_length=50)

forms.py表格.py

from .models import MModel
from django import forms

class MForm(forms.ModelForm):
    class Meta:
        model = MModel
        fields = ['Message', 'Img', 'Author']

I found out I could just pass in the randomly selected object itself,我发现我可以直接传入随机选择的对象本身,

#Some uniquely random file 1
choice1 = random.choice(message_list)

#Some uniquely random file 2
choice2 = random.choice(message_list)
while(choice2 == choice1):
    choice2 = random.choice(message_list)

#Some uniquely random file 3
choice3 = random.choice(message_list)
while(choice3 == choice1 or choice3 == choice2):
    choice3 = random.choice(message_list)


This means I can access the object's image (and other attributes) in the .html like so:这意味着我可以在 .html 中访问对象的图像(和其他属性),如下所示:
<img src="/media/{{ aPassedFile.Img }} " width="290" height="180" alt="">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM