简体   繁体   中英

Django - Loading a full image on separate page

I've been trying to put a hyperlink on my image thumbnail which would take the user to a full size image. but I keep on getting an error.

here as it shows, scribblemedia is a ForeignKey to scribble

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 }} />

This is the page where the image thumbnail is available

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 %}

It keeps on giving me the following error:

TemplateSyntaxError at /image/2/ Unclosed tag 'if'. Looking for one of: elif, else, endif

The if statements in your scribble_page.html are fine. You need to close your if block in your image_page.html template...

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

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