简体   繁体   中英

Use django sorl-thumbnail with django form-utils

How do I implement sorl thumbnailing in django form utils?

So the form utils documentation says this about its ImageWidget:

(Thumbnails only available if sorl-thumbnail is installed; otherwise the full-size image is displayed).

However, I can't seem to implement it. I tried using sorl's ImageField in models, it breaks the form-utils' ImageWidget and I can't get my head around using it in a template when all I have in the template is this:

<li class="field_upload">
    {{ form.image.errors }}
    <label for="id_image" class="top">{{  form.image.label }}</label>
    {{ form.image }}
</li>

form utils has an option that says:

ImageWidget accepts a keyword argument, template. This is a string defining how the image thumbnail and the file input widget are rendered relative to each other. The template string should contain variable interpolation markers %(input)s and %(image)s. The default value is %(input)s
%(image)s

which in code looks like this:

pic = forms.ImageField(
    widget=ImageWidget(template='%(image)s<br />%(input)s'))

but I can't still figure it out.

It looks like in your form class definition you are naming the ImageField as pic , but then in your template you are attempting to refer to the field with form.image .

Try just using the standard Django form renderer for now by putting {{form}} in your template--if that works then you know it's just a problem with your templating.


The SORL thumbnail library should only need to be accessible on your Django project's Python Path (along with the Python Image Library).

To check to see whether or not these libraries are available for your Django project, goto your project's root directory and run...

 $ python manage.py shell >> import sorl.thumbnail >> import PIL

If neither of those imports throw ImportError s then they are properly installed.


Also, maybe include some more code, like the entire form as you have it defined in forms.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM