blank = True无法在CloudinaryField上工作()

时间:2017-03-06 16:19:23

标签: django cloudinary

我确信这是我想念的简单事。但我正在设置图像共享网站,我正在使用cloudinary将图像直接上传到云端。我的models.py中有CloudinaryField字段,如此

image = CloudinaryField('image', blank=True, null=True)

但是,当我尝试通过选择要上传的图像来填写图像字段时,它不会更新图像字段,当我提交表单时,它表示没有检测到图像,也不会让我创建新的图像文章。我有页面设置,所以一旦通过弹出窗口选择图像就会上传图像,图像将被上传到cloudinary。

views.py

 def post_new_image(request):
   form = PostPhotoDirectForm(request.POST)
   cl_init_js_callbacks(form, request)
   if request.method=='POST':
     form = PostPhotoDirectForm(request.POST)
     if form.is_valid():
         post=form.save(commit=False)
         post.author=request.user
         post.image=form.cleaned_data['image']
         post.save()
         return redirect('post_detail', pk=post.pk)

     else:
       form = PostPhotoDirectForm()
 return render(request, "blog/post_new_image.html", {'form':form})

forms.py

from django import forms
from .models import Post
from cloudinary.forms import CloudinaryJsFileField, CloudinaryUnsignedJsFileField
from cloudinary.compat import to_bytes
import cloudinary, hashlib

class PostForm(forms.ModelForm):

    class Meta:
        model=Post
        fields = ('title', 'text')

class PostPhotoDirectForm(PostForm):
     image = CloudinaryJsFileField()

post_new_image.html

{% extends 'blog/base.html' %}
{% block content %}
    <div id="direct_upload">
        <h1>New Image Post</h1>
        <form action="{% url 'blog.views.post_new_image' %}" method="post" class="post-form" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form }}
            <button type="submit" class="save btn btn-default" value="Upload">Save</button>
        </form>

        <div class="uploader_info_holder">
        </div>
    </div>

    {% endblock %}
{% block javascript %}
    $(function () {
        $('#direct_upload input[type="file"]').cloudinary_fileupload({
            dropZone: '#direct_upload',
            start: function () {
                $('.status_value').text('Starting direct upload...');
            },
        progress: function () {
            $('.status_value').text('Uploading...');
            },
        }).on('cloudinarydone', function (e, data) {
            $('.status_value').text('Updating backend...');
            $.post(this.form.action, $(this.form).serialize()).always(function (result, status, jqxhr) {
            $('.status_value').text(result.errors ?   JSON.stringify(result.errors) : status);
        });


        });
    });
{% endblock %}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

通过表格中的addind required = False修正了它。

class PostPhotoDirectForm(PostForm):
 image = CloudinaryJsFileField(required=False)