-1

我可以选择最多添加 5 个文件。

我有一个手动表格,其中我有一个上传文件的字段。

**models.py**
class resmodel(models.Model):
user = models.ForeignKey(User, unique=True)
fname = models.CharField(max_length=20)

file1 = models.FileField(upload_to='documents')
file2 = models.FileField(upload_to='documents')
file3 = models.FileField(upload_to='documents')
file4 = models.FileField(upload_to='documents')

file_header1 = models.CharField(max_length=50)
file_header2 = models.CharField(max_length=50)
file_header3 = models.CharField(max_length=50)
file_header4 = models.CharField(max_length=50)

当我第一次提交表单时,上传的文件及其对应的 file_header 分别保存到 file1 和 file_header 中。

同样,当我第二次提交表单时,我必须检查 file1 是否已填写,如果没有,上传的文件必须保存到 file1,否则应该保存到 file2。

当我第三次提交表单时,我必须检查file1和file2是否填写,如果没有,上传的文件必须保存到file1或file2中,无论哪个为空,否则应该保存到file2。

Ans 等到 file5。

**views.py**
def resview(request):
if request.method == "POST":
    upload_resume = request.FILES['upload_resume']
    file_header = request.POST.get('resume_header')
    fname = request.POST.get('fname')

    if upload_resume:
        details, _ = resmodel.objects.get_or_create(user=request.user)
        if not details.file1:
            details.file1 = upload_resume
        elif not details.file2:
            details.file2 = upload_resume
        elif not details.file3:
            details.file3 = upload_resume
        elif not details.file4:
            details.file4 = upload_resume
        details.save()

    if upload_resume:
        details, _ = resmodel.objects.get_or_create(user=request.user)
        if not details.file_header2:
            details.file_header1 = file_header
        elif not details.file_header2:
            details.file_header2 = file_header
        elif not details.file_header3:
            details.file_header3 = file_header
        elif not details.file_header4:
            details.file_header4 = file_header
        details.save()

    a = resmodel(fname=fname)
    a.save()
    return HttpResponseRedirect('/profile/save/success/')
else:
    return render_to_response('resume.html')

我的模板是

<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<table>
    <tr>
    <td>First Name</td>
    <td><input type="text" name="fname" /></td>
</tr>
<tr>
    <td>File Header<sub>(250 words max)</sub></td>
    <td><input type="text" name="file_header"></td>
</tr>
<tr>
    <td>Upload file<sub>(.doc/.docx, rtf, pdf, txt)</sub></td>
    <td><input type="file" name="upload_resume" ></td>
</tr>
<tr>
    <td></td>
    <td>
        <input type="submit" value="SUBMIT">&nbsp;&nbsp;
        <input type="reset" value="RESET">
    </td>
</tr>
</table>
</form>
4

2 回答 2

1

你需要的是一个额外的模型。

class js_details(models.Model):
    user = models.ForeignKey(User, unique=True)

class js_details_file(models.Model):
    js_details = models.ForeignKey(js_details)
    file = models.FileField(upload_to='documents')

然后在管理界面中使用Inlines来允许上传文件,并在前端使用Formsets

于 2013-04-08T05:59:33.753 回答
0
def resview(request):
    if request.method == "POST":
        upload_resume = request.FILES['upload_resume']
        file_header = request.POST.get('resume_header')
        fname = request.POST.get('fname')

        if upload_resume and file_header:
            details, _ = resmodel.objects.get_or_create(user=request.user, fname=fname)
            if not details.file1:
                details.file1 = upload_resume
                details.file_header1 = file_header
            elif not details.file2:
                details.file2 = upload_resume
                details.file_header2 = file_header
            elif not details.file3:
                details.file3 = upload_resume
                details.file_header3 = file_header
            elif not details.file4:
                details.file4 = upload_resume
                details.file_header4 = file_header
            details.save()

        return HttpResponseRedirect('/profile/save/success/')
    else:
        return render_to_response('resume.html')
于 2013-04-08T06:15:43.163 回答