我试图允许用户上传多张图片,但只有一张图片保存到数据库中。这是我的模特
void OnGUI()
{
var mousePosition = Input.mousePosition;
float x = mousePosition.x;
float y = Screen.height - mousePosition.y;
float width = 200;
float height = 200;
var rect = new Rect(x, y, width, height);
GUI.Label(rect, "Test");
}
接下来,上传的表单如下所示:
from django.db import models
from django.contrib.auth.models import User
class File(models.Model):
files = models.FileField(upload_to='images/')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='files')
最后,视图'上传图片'如下:
from django import forms
from .models import File
class FileForm(forms.ModelForm):
class Meta:
model=File
fields=('files',)
widgets={'files':forms.FileInput(attrs={'id':'files','required':True,'multiple':True})}
问题是只保存了1张图片。任何帮助都非常感谢!
答案 0 :(得分:0)
你的代码看起来很好,但看起来你正在寻找的是Django Formsets。它允许您一次上传多个图像。另请查看此问题的答案:how to upload multiple images to a blog post in django