Django - ImageField Blank=True 不起作用

标签 django python-2.7

我在 stackoverflow 上看到过类似的问题,但似乎无法让我的问题工作。当我让用户上传照片时,如果输入留空,Django 将抛出 MultiValueDictKeyError。我的 imageField 中有空白=True,但它似乎不起作用。

我的应用程序中有以下类(class)文章:

def content_file_name(instance, filename):
    return '/'.join(['content', instance.user.username, filename])


class Content(models.Model):
    user = models.OneToOneField(User, unique=True)
    image1 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image2 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image3 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image4 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image5 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image6 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image7 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image8 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    terms = models.ImageField(upload_to=content_file_name, null=True, blank=True)

这就是我的views.py 的样子:

def register(request):
    if request.POST:
        content = Content()
        content.user = request.user
        content.image1 = request.FILES['image1_upload']
        content.image3 = request.FILES['image3_upload']
        content.image4 = request.FILES['image4_upload']
        content.image5 = request.FILES['image5_upload']
        content.image6 = request.FILES['image6_upload']
        content.image7 = request.FILES['image7_upload']
        content.image8 = request.FILES['image8_upload']
        content.terms = request.POST.get('terms')
        content.save()
        return redirect('/portal/register')

    try:
        gallery = Content.objects.get(user=request.user)
        return render(request, 'portal/register.html', {'gallery': gallery})
    except ObjectDoesNotExist:
        print 'Does Not Exist!'
        return render(request, 'portal/register.html')

这是 Django 抛出的错误:

Request Method: POST
Request URL:    http://127.0.0.1:8000/portal/register/
Django Version: 1.8.2
Exception Type: MultiValueDictKeyError
Exception Value: "'image1_upload'"
Exception Location: /Library/Python/2.7/site-packages/django/utils/datastructures.py in __getitem__, line 322
Python Executable:  /usr/bin/python

上传图片的html:

<div class="container">
    <form role="form" method="post" action="." id="js-upload-form" enctype="multipart/form-data">
                        {% csrf_token %}
    <div class="row">

        <div class="col-lg-12">
            <h1 class="page-header">{{ user.username }}</h1>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image1" src="/media/{{ gallery.image1 }}" alt="">
            </a>
            <input type="file" name="image1_upload" id="image1_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image2" src="/media/{{ gallery.image2 }}" alt="">
            </a>
            <input type="file" name="image2_upload" id="image2_upload" multiple>
        </div>


        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image3" src="/media/{{ gallery.image3 }}" alt="">
            </a>
            <input type="file" name="image3_upload" id="image3_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image4" src="/media/{{ gallery.image4 }}" alt="">
            </a>
            <input type="file" name="image4_upload" id="image4_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image5" src="/media/{{ gallery.image5 }}" alt="">
            </a>
            <input type="file" name="image5_upload" id="image5_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image6" src="/media/{{ gallery.image6 }}" alt="">
            </a>
            <input type="file" name="image6_upload" id="image6_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image7" src="/media/{{ gallery.image7 }}" alt="">
            </a>
            <input type="file" name="image7_upload" id="image7_upload" multiple>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" id="image8" src="/media/{{ gallery.image8 }}" alt="">
            </a>
            <input type="file" name="image8_upload" id="image8_upload" multiple>
        </div>
    </div>

最佳答案

如果表单字段留空,则 request.FILES['form_field'] 将不存在。 你应该使用类似的东西:

content.image1 = request.FILES.get('image1_upload', None)

这样,如果表单字段不存在,模型字段将设置为 None。

您也可以只检查文件中是否存在文件字段,但它最终会稍微冗长一些:

if 'image1_upload' in request.FILES:
    content.image1 = request.FILES.image1_upload

关于Django - ImageField Blank=True 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441475/

相关文章:

django - 限制 django 字段模型中的最大值和最小值

python - Django TypeError at/polls/1/vote/_reverse_with_prefix() * 之后的参数必须是可迭代的,而不是 int

python - 无法使用react js将文件上传到django rest框架

python - 具有工作时间上下文的日期时间列表,计算非工作时间

python - IPython笔记本安装错误

python - 网页抓取:我只得到我想要的文本的 1/10(使用 BeautifulSoup)

python - 从基于类的通用 View 手动获取响应

python - 验证 Django 中的值列表

python - 如何为 python 2.7 卸载 PIL 并改用 Pillow?

python - python中使用for语句的无限循环