如何访问通过Django中的表单集上传的图像?

时间:2018-07-30 16:53:51

标签: python django image file django-forms

尽管似乎已按照代码指示上载了文件,但我似乎无法找到一种方法来遍历表单集上载的文件。

当我加载页面时,它显示1-3个“ img src =(unkown)”标签。上传2张图片时显示3,上传1张图片时显示1。

模板视图后:

{% block body %}
<div class="container">
<div class="col-md-8">
    <p>
        <h1>{{ post.title }}</h1>
        <h4>{{ post.body }}</h4>
        <p>
            Posted by {{ post.user }} on {{ post.created_date }}
            {% if not post.created_date == post.updated_date %}
            and updated on {{ post.updated_date }}
            {% endif %}
        </p>
    </p>
    {% for img in images %}
    <img src="{{ img.url }}" width=360>
    {{ img.url }}
    <br>
    {% endfor %}
</div>
<div class="col-4-md">
    {% if post.cover_image %}
    <img src="{{ post.cover_image.url }}" width=240>
    {% endif %}
</div>
</div>
{% endblock %}

在这里我使用过滤器,而不是获取,因此它应该给我列出通过Foreignkey连接到正确帖子的图像列表。此处的帖子将按原样显示,并带有内部封面图片和帖子中的文字。

查看观点:

class ViewPost(TemplateView):
    template_name = 'posts/view_post.html'
    context_object_name = 'view_post'

def get(self, request, pk):
    post = Post.objects.get(pk=pk)
    images = Images.objects.filter(post=post)
    args = {'post': post, 'images': images}
    return render(request, self.template_name, args)

模板制作:

{% block body %}
<div class="container">
<div class="col-md-8">
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        {{ form.errors }}
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            {{ form }}
        {% endfor %}
    <button type="submit">Submit</button>
    </form>
</div>
</div>
{% endblock %}

查看发布信息:

class MakePost(DetailView):

    template_name = 'posts/make_post.html'
    ImageFormSet = modelformset_factory(Images, form=ImageForm, extra=3)

def get(self, request):
    form = PostForm()
    formset = self.ImageFormSet(queryset=Images.objects.none())
    args = {'form': form, 'formset': formset}
    return render(request, self.template_name, args)

def post(self, request):
    form = PostForm(request.POST, request.FILES)
    formset = self.ImageFormSet(request.POST, request.FILES,
                           queryset=Images.objects.none())
    if form.is_valid() and formset.is_valid():
        post = form.save(commit=False)
        post.user = request.user
        post.cover_image = form.cleaned_data['cover_image']
        post.title = form.cleaned_data['title']
        post.body = form.cleaned_data['body']
        post.save()

        for imgform in formset:
            image = imgform.cleaned_data.get('image')
            photo = Images(post=post, post_image=image)
            photo.save()
        return redirect('home_space:home')
    else:
        return redirect('home_space:home')

用于命名文件和图像模型的功能:

def get_image_filename(instance, filename):
    title = instance.post.title
    slug = slugify(title)
    return "post_images/%s-%s" % (slug, filename)

class Images(models.Model):
    post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE)
    post_image = models.ImageField(upload_to=get_image_filename,
                          verbose_name='Image')

1 个答案:

答案 0 :(得分:0)

在模板中,您正在访问url模型实例的Images。但是,url属性在ImageField类实例中。使用img.post_image.url

示例

{% for img in images %}
    <img src="{{ img.post_image.url }}" width=360>
    {{ img.post_image.url }}
    <br>
{% endfor %}