在Django上将multi img上传到1个帖子?怎么办 [英] Upload multi img to 1 post on Django ? How do that?

查看:41
本文介绍了在Django上将multi img上传到1个帖子?怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多img上传到Django上的1个帖子中.我在models.py中使用了ImageField.但是它只能将1张图片上传到1个帖子中.如何使用Django或以某种方式将多张图片上传到1个帖子中,以解决该问题.非常感谢.

I want to upload multi img to 1 post on Django. I used ImageField in models.py. But it's just can upload 1 image to 1 post. How can i upload multi image to 1 post with Django or someways to solved that problem. Thank you so much.

推荐答案

对不起,过去的回答.

我认为这是最好的方法.

I think this is the best way.

models.py

models.py

from django.db import models
from django.template.defaultfilters import slugify

class Post(models.Model):
    title = models.CharField(max_length=128)
    body = models.CharField(max_length=400)

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


class Files(models.Model):
    post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE)
    files = models.FileField(upload_to=get_image_filename, verbose_name='File')

我没有为文件创建表单,因为我们将在模板中手动编写表单.

I did not create a form for the files because we will write it manually in the template.

forms.py

from django import forms
from .models import Post, Images

class PostForm(forms.ModelForm):
    title = forms.CharField(max_length=128)
    body = forms.CharField(max_length=245, label="Item Description.")

    class Meta:
        model = Post
        fields = ('title', 'body', )

views.py

from django.shortcuts import render
from django.contrib import messages
from django.http import HttpResponseRedirect
from .forms import PostForm
from .models import Post, Files

def post(request):

    if request.method == 'POST':
        print(request.FILES.getlist('files'))
        postForm = PostForm(request.POST)

        if postForm.is_valid():
            post_form = postForm.save(commit=False)
            post_form.save()
            if request.FILES.getlist('files'):
                for file in request.FILES.getlist('files'):
                    obj = Files(post=post_form, files=file)
                    obj.save()
            
            messages.success(request, "Yeeew, check it out on the home page!")
            return HttpResponseRedirect("/")
        else:
            print(postForm.errors)
    else:
        postForm = PostForm()
    return render(request, 'index.html', {'postForm' : postForm})

index.html

index.html

<html>
<form id="post_form" method="post" action="" enctype="multipart/form-data">

    {% csrf_token %}
    {% for hidden in postForm.hidden_fields %}
        {{ hidden }}
    {% endfor %}

    {% for field in postForm %}
        {{ field }} <br />
    {% endfor %}
    <input type="file" id="files" name="files" multiple><br>
    <input type="submit" name="submit" value="Submit" />
</form>

</html>

我们为未形成的文件模型创建一个字段.

We make such a field for the model of files that we do not form.

<input type="file" id="files" name="files" multiple><br>

您可以选择多个文件并通过按住CTRL或Shift上载它们.

You can select multiple files and upload them by holding down CTRL or Shift.

这篇关于在Django上将multi img上传到1个帖子?怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆