Sorl缩略图不能在模板中打开图像(Django)

时间:2015-04-06 13:35:17

标签: django upload django-templates thumbnails sorl-thumbnail

我已经按照教程创建了一个缩略图,用于在模板中上传和发布文件内容和图像。我使用的是Django 1.6。有一个版本的get_method()与缩略图字段相关,上传的图片或文本的地方显示在下面的html one_labeling_index.html但未打开,而第二个版本是sorl - thumbnail,它根本没有显示任何内容使用"浏览" - 上传按钮。

在models.py

from sorl.thumbnail import get_thumbnail
from django.core.files.base import ContentFile
# instance, is instance from django and filename is name of uploaded file
#  formats str to filename format str

def get_upload_file_name(instance,filename):
    return "uploaded_files/%s_%s" % (str(time()).replace('.', '_'), filename)

class Labeling(models.Model):

    sentence = models.CharField(max_length=200)
    datetime = models.DateTimeField()
    title = models.CharField(max_length=70)
    current_count = models.IntegerField()
    label = models.CharField(max_length=200)
    # thumbnail field
    thumbnail = models.FileField(upload_to=get_upload_file_name)


 def __unicode__(self):
        return self.sentence

class OneLabeling_Phrase(models.Model):

    sentence = models.CharField(max_length=200)
    label = models.CharField(max_length=200)
    thumbnail = models.FileField(upload_to=get_upload_file_name)
在views.py中

def LabelingIndex(request):
        labels = Labeling.objects.all()

        return render(request,"labeling_index.html", {"labels":labels})


def one_labeling(request,postID):

    one_labeling = Labeling.objects.get(id=postID)
    if request.method == 'POST':
        form = LabelingForm(request.POST, request.FILES)

        if form.is_valid():
            postonelabeling(request, one_labeling)

    else:
        form = LabelingForm()

    m = {"form":form, "one_labeling":one_labeling}
    m.update(csrf(request))

    return render_to_response('one_labeling_index.html', m)

在forms.py

class LabelingForm(forms.ModelForm):


    class Meta:
        model = Labeling
        fields = ('title', 'current_count', 'sentence', 'label', 'thumbnail')
在urls.py中

   url(r'^labeling$', LabelingIndex),
   url(r'^labeling(?P<postID>\d+)$',one_labeling),

在html labels_index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>


{% for one_labeling in labels %}

    <h2><a href="/labeling{{ one_labeling.id}}">{{ one_labeling.title }}</a></h2>

    <p>{{ one_labeling.sentence }}</p>
    <p>{{ one_labeling.label }}</p>

    {% for c in one_labeling.onelabeling_phrase.set_all %}

         <p>{{ one_labeling.sentence }}</p>
         <p>{{ one_labeling.label }}</p>
         <p>{{ one_labeling.thumbnail }}</p>





    {% endfor %}

{% endfor %}


</body>
</html>

在one_labeling_index.html

{% load static %}
{% load thumbnail %}
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>





<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<ul>
    {{ one_labeling.thumbnail}}


     {{form.as_ul}}


    <p><img src="{% get_static_prefix %}{{one_labeling.get_thumbnail}}" width="200"/></p>

      # with soal - thumbnail
    {% thumbnail item.image my_size_string crop="left" as im %}
       <img src="{{thumbnail.labeling}}"width="200">
    {% empty %}
    <p>No image</p>
    {% endthumbnail %}


</ul>



</body>
</html>

1 个答案:

答案 0 :(得分:2)

  

上传图片或文字的位置显示在下面的html one_labeling_index.html中但未打开

我不确定这意味着什么,但我认为你的意思是空间在那里,但没有图像显示。在这种情况下,这种情况正在发生,因为您已经放置了<img>来源。其余部分根本没有显示任何内容,因为代码已被破坏。

这里缺少一些元素。例如,我不确定变量item来自何处?无论如何,你使用的是sorl-thumbnail错误。 Take a look at the examples in the docs。请记住,您有thumbnail item.image AS im,这意味着您的图片对象现在以'im'引用。我有属性url,宽度,高度。所以你的代码应该是:

{% thumbnail one_labeling.thumbnail '200x200' crop="left" as im %}
   <img src="{{ im.url }}" width="{{ im.width }}" heigh="{{ im.height }}">
{% empty %}
   <p>No image</p>
{% endthumbnail %}

get_thumbnail是来自sorl-thumbnail的方法,而不是您的模型,所以除非您在标签模型中实际创建了此方法,否则无法说{{ one_labeling.get_thumbnail }}。如果要从模型中设置sorl-thumbnail:

from sorl.thumbnail import get_thumbnail

class Labeling(models.Model):
   pass

   def get_thumb(self):
        im = get_thumbnail(thumbnail, '200x200', crop='center', quality=99)
        return im.url # remember that sorl objects have url/width/height attributes

因此,在您的模板中,您可以:

<img src="{{ one_labeling.get_thumb }}" />

希望这有帮助。