2017-03-06 60 views
0

我相信這是簡單的,我失蹤了。但我正在設置和圖像共享網站,我正在使用cloudinary直接將圖像上傳到雲端。我有CloudinaryField場在我的models.py這樣blank = True不能在CloudinaryField上工作()

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

然而,當我試圖通過選擇要上傳的圖片填寫圖像領域,它不更新的像場,當我提交表單它說沒有檢測到圖像,不會讓我創建新的圖像文章。我有頁面設置,因此只要通過彈出窗口選擇圖像,圖像就會上傳到雲端。

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

通過addind required = False修復它的形式。

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

作爲註釋,您必須添加required = False,因爲您覆蓋了模型上的字段,因此必須重新設置verbose_name/label和required。 – Blackeagle52

相關問題