Django - 在单独的页面上加载完整的图像

时间:2012-08-30 14:48:52

标签: python django

我一直试图在我的图片缩略图上放置一个超链接,这会让用户看到一个完整尺寸的图像。但我一直在犯错误。

这里显示,scribblemedia是一个外来关键的涂鸦

models.py

class ScribbleMedia(models.Model):
     media = models.FileField(upload_to=get_file_path)
     def __unicode__(self):
      return self.media
     def find_typecheck(self):
        filename = self.media.name
        try:
           ext = filename.split('.')[-1]
           imgcheck=['jpg','jpeg','png','gif','tiff','bmp']
           if ext in imgcheck :
              chk='image'
           else:
              chk='other'
        except Exception:
           chk='not supported'
        return chk

class Scribble(models.Model):
     title = models.CharField(max_length=120)
     body = models.TextField()
     user = models.ForeignKey(User)
     media = models.ForeignKey(ScribbleMedia)
     def __unicode__(self):
      return u'%s, %s' % (self.user.username, self.media)

views.py

@login_required
def image_page(request,pk):
    img=get_object_or_404(ScribbleMedia,pk=pk)
    image=img.media
    variables= RequestContext(request,{
    'image': image
    })
    return render_to_response('image_page.html',variables)

urls.py

(r"^image/(\d+)/$", image_page),

image_page.html

{% if  image %}
    <img src= {{ image.url }} />

这是图像缩略图可用的页面

scribble_page.html

{% if scribble.media.media %}
                {% if scribble.media.find_typecheck == 'image' %}
                    {% thumbnail scribble.media.media.url "700x500" crop="center" as im %}
                            <a href="/image/{{ scribble.media.pk }}/" target="_blank"><img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"></a>
                    {% endthumbnail %}
                {% else %}
                    do something else
                {% endif %}
            {% endif %}

它不断给我以下错误:

/ image / 2 /上的TemplateSyntaxError 未封闭的标签'if'。寻找其中一个:elif,else,endif

1 个答案:

答案 0 :(得分:4)

您的scribble_page.html中的if语句没问题。您需要在image_page.html模板中关闭if块...

{% if  image %}
  <img src="{{ image.url }}" /> <!-- Also note the added quotations... -->
{% endif %}  <!-- This is the line you need to add -->