简体   繁体   English

Django文件未上传

[英]Django File not uploading

I'm working on an app that will let users build their own databases, and one of the fields they can create is a photo field. 我正在开发一个应用程序,它将允许用户构建自己的数据库,他们可以创建的字段之一是照片字段。 I'm using a form model, and while it captures the rest of the data, it won't try to grab the files. 我使用的是表单模型,尽管它捕获了其余数据,但不会尝试获取文件。

I have file uploads working elsewhere on the site, and if I upload the file manually in the admin panel it shows up just fine, but the form itself won't grab the file. 我可以在网站上的其他地方进行文件上传,如果我在管理面板中手动上传文件,则显示的很好,但是表单本身无法抓取文件。

In my template, I have: 在我的模板中,我有:

<form action="/orgs/{{org.id}}/db/{{db.unique_id}}/rows/add/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    ...
    {% elif column.field_type = 23 %}
        {{form.photofield}}
    {% endif %}
</form>

In my models: 在我的模型中:

from django.db import models

def get_upload_file_name(instance, filename):
    return settings.UPLOAD_FILE_PATTERN % (str(time()).replace('.','_'), filename)

class DbField(models.Model):
    ...
    photofield = models.FileField(upload_to=get_upload_file_name, null=True, blank=True)

in my forms: 以我的形式:

from django import forms
from byodb.models import DbField

class DbFieldForm(forms.ModelForm):
    class Meta:
        model = DbField
        fields = ('photofield',)
        widgets = {'photofield': forms.FileInput()}

and in my views: 在我看来:

def org_database_add_row(request, organization_id=1, db_id=1):
    if request.POST:
        if request.POST.get(str(column.unique_id)):
            if column.field_type == 23:
                form = DbFieldForm(request.POST, request.FILES)
                if form.is_valid():
                    f = form.save(commit=False)
                    ...
                    f.save()

sorry for any confusion about column/row stuff, it's just data to place the field. 对于列/行内容的任何混乱,我们深表歉意,这只是放置字段的数据。 If you need more detail then I can add more, but on the surface it looks like everything SHOULD work... 如果您需要更多细节,那么我可以添加更多,但是从表面上看,一切都应该起作用...

ALTERNATIVELY, I have been trying to get it to work another way, avoiding using the form altogether so that the file field will be named the same as my column. 另外,我一直试图使它以另一种方式工作,避免完全使用该表单,以便将文件字段命名为与我的列相同。

In that instance, in my template it reads: 在这种情况下,在我的模板中显示为:

<input type="field" name="{{column.unique_id}}" id="photofield" />

and in my views it reads: 在我看来,它显示为:

elif column.field_type == 23:
    DbField.objects.create(row=row, column=column, unique_id=column.unique_id, creator=request.user, last_editor=request.user, field_type=column.field_type, photofield=request.FILES[str(column.unique_id)])

However it's the same issue, it will create the field just fine, but it won't try to grab the file. 但是,这是相同的问题,它将很好地创建字段,但不会尝试获取文件。 I'm not sure why it's only failing here, as this works everywhere else on the site. 我不确定为什么它只会在这里失败,因为这在网站上的其他地方都有效。

My bad, I figured out the problem. 不好,我发现了问题所在。

In my views, where I had: 在我看来,我曾在哪里:

if request.POST:
    if request.POST.get(str(column.unique_id)):
        if column.field_type == 23:
            DbField.objects.create(row=row ... photofield=request.FILES[str(column.unique_id)])

It was failing because request.POST.get(str(column.unique_id)) was empty since it was FILES and not POST. 失败是因为request.POST.get(str(column.unique_id))为空,因为它是FILES而不是POST。

I rewrote the view to accomodate: 我重写了视图以适应:

if request.POST.get(str(column.unique_id)):
    ...
else:
    if request.FILES[str(column.unique_id)]:
        if column.field_type == 23:
            DbField.objects.create(row=row ... photofield=request.FILES[str(column.unique_id)])
        else:
            DbField.objects.create(row=row ... all other fields left blank)

This way if the request.POST for the field comes up empty, it checks to see if there's a file attached and the correct field type, if not then it just creates the empty field. 这样,如果该字段的request.POST变为空,它将检查是否附加了文件以及正确的字段类型,如果没有,则仅创建一个空字段。

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

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