imagefield problem .....

13 views
Skip to first unread message

Pulkit Mehrotra

unread,
Jun 13, 2011, 6:52:50 AM6/13/11
to django...@googlegroups.com
problem in using the address stored in imagefield

this is the news.html:
<html>
<head>
<title>News</title>
</head>
<body>
{% if news_list %}
    <ul>
        {% for news in news_list %}
            <li><img src="{{ STATIC_URL }}/{{ news.image }}" width="100" height="100" />{{ news.image }} || {{ news.news }}</li>
        {% endfor %}
    </ul>
{% endif %}
</body>
</html>

this  is the views:
from django.shortcuts import render_to_response
from news.models import MainNews

def display_news(request):
    news_list=MainNews.objects.all().order_by('-date')
    return render_to_response('news/news.html',{'news_list':news_list})

this is the models.py:

from django.db import models

class MainNews(models.Model):
    news=models.CharField(max_length=200)
    date=models.DateTimeField('date published')
    image=models.ImageField(upload_to='news')

Pankaj Singh

unread,
Jun 13, 2011, 10:31:11 AM6/13/11
to django...@googlegroups.com
There can be better solutions but you can some easy hack like this - 

In models.py you can add a function to get image url

######################################

from django.db import models
import os
import settings

class MainNews(models.Model):
    news=models.CharField(max_length=200)
    date=models.DateTimeField('date published')
    image=models.ImageField(upload_to='news')

    def get_image_url(self):
        return os.path.join(settings.STATIC_URL, 'news', self.image.name.split('/')[-1])
######################################

now in the template make changes like this

<html>
<head>
<title>News</title>
</head>
<body>
{% if news_list %}
    <ul>
        {% for news in news_list %}
            <li><img src=" {{ news.image.get_image_url }}" width="100" height="100" />{{ news.image }} || {{ news.news }}</li>

Ivo Brodien

unread,
Jun 13, 2011, 10:38:08 AM6/13/11
to django...@googlegroups.com

On Jun 13, 2011, at 12:52 PM, Pulkit Mehrotra wrote:

> problem in using the address stored in imagefield

you should describe your problem a little better!

But I guess the URL is not working?

You shouldn’t use STATIC_URL but MEDIA_URL for the prefix in your template.

STATIC_URL is for static files, that don’t change like CSS and JS.

MEDIA_URL is for user uploads, like a photo in your news entry.

Pankaj Singh

unread,
Jun 13, 2011, 11:57:05 AM6/13/11
to django...@googlegroups.com
yes even in get_image_url it will be

        return os.path.join(settings.MEDIA_URL, 'news', self.image.name.split('/')[-1])

Ivo Brodien

unread,
Jun 13, 2011, 12:09:14 PM6/13/11
to django...@googlegroups.com
On Jun 13, 2011, at 5:57 PM, Pankaj Singh wrote:

yes even in get_image_url it will be

        return os.path.join(settings.MEDIA_URL, 'news', self.image.name.split('/')[-1])



writing that function is not necessary.


3) All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). You'll most likely want to use the convenience url function provided by Django. For example, if your ImageField is called mug_shot, you can get the absolute path to your image in a template with {{ object.mug_shot.url }}.

so the Pulkit was trying in his template is ok, he just should use the url function and the MEDIA_URL
Reply all
Reply to author
Forward
0 new messages