Django-Filer: How to invoke upload widget outside

2019-06-20 09:27发布

问题:

How do I invoke the upload widget outside Admin? Using the standard form below does not work. The widget is corrupted.

forms.py
class PhotoAdd(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ('ImageFilerField',)


views.py
def photoadd(request):
  context={}
  context['form'] = PhotoAdd()
  render(request, 'template.html', context)


template.html
{{ form }}

There is absolutely no reference to this in the documentation. How can I upload photos outside admin?

Edit: (added generated code from django above)

<label for="id_doc_pic">Doc pic:</label>
</th>
 <td>
   <span class="filerFile">
   <img id="id_doc_pic_thumbnail_img" src="/static/filer/icons/nofile_48x48.png" class="quiet" alt="no file selected" />
   &nbsp;<span id="id_doc_pic_description_txt"></span>
   <a href="/admin/filer/folder/last/?_to_field=file_ptr" class="related-lookup" id="lookup_id_doc_pic" title="Lookup">
   <img src="/static/admin/img/icon_searchbox.png" width="16" height="16" alt="Lookup" /></a><img id="id_doc_pic_clear" class="filerClearer" src="/static/admin/img/icon_deletelink.gif" width="10" height="10" alt="Clear" title="Clear" style="display: none;" /><br />
   <input class="vForeignKeyRawIdAdminField" id="id_doc_pic" name="doc_pic" type="text" />

<script type="text/javascript" id="id_doc_pic_javascript">
    django.jQuery(document).ready(function(){
        var plus = django.jQuery("#add_id_doc_pic");
        if (plus.length){
            plus.remove();
        }

        django.jQuery('#id_doc_pic_javascript').remove();
    });
</script>
</span>

Javascript Error: "Uncaught ReferenceError: django is not defined"

Could it be that I need to know what css to include and what javascript files to load?

回答1:

First of all, add {{ form.media }} in your html file, just after the tag <form> and {% csrf_token %}. Then extend the media class in your ModelForm of forms.py as explained by SteinRobert on Github issue: https://github.com/divio/django-cms/issues/6028. It works perfect! Thanks Stein!