Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-ImageField blank=True不起作用_Django_Python 2.7 - Fatal编程技术网

Django-ImageField blank=True不起作用

Django-ImageField blank=True不起作用,django,python-2.7,Django,Python 2.7,我在stackoverflow上也看到过类似的问题,但似乎无法让我的工作。当我让用户上传照片时,如果输入为空,Django将抛出一个多值错误。我的imageField中有blank=True,但它似乎不起作用 我的应用程序中有以下课堂文章: def content_file_name(instance, filename): return '/'.join(['content', instance.user.username, filename]) class Content(mod

我在stackoverflow上也看到过类似的问题,但似乎无法让我的工作。当我让用户上传照片时,如果输入为空,Django将抛出一个多值错误。我的imageField中有blank=True,但它似乎不起作用

我的应用程序中有以下课堂文章:

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)
这就是my 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>

{%csrf_令牌%}
{{user.username}

如果表单字段留空,则请求文件['form\u field']将不存在。 相反,您应该使用更类似于:

content.image1 = request.FILES.get('image1_upload', None)
这样,如果表单字段不存在,模型字段将设置为无

您也可以只检查文件中是否存在文件字段,但最终会变得更加详细:

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

让我们看看你的表格,我没有用表格。只需在html模板中使用id=“”,第二部分对我有效。非常感谢。我真的很感谢你的帮助!