0

I am learning Django by developing a blog. My code was working in both Linux and Windows. Recently, I started added images to my models with ImageField and it works in Linux but I can't get it to work in Windows. I am using a standalone server. Here is my settings.py file:

    MEDIA_ROOT = 'C:/django/third_blog/zblog/media_files/'
    MEDIA_URL = 'C:/django/third_blog/zblog/media_files/'
    STATIC_ROOT = 'C:/django/third_blog/zblog/static'
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        'C:/django/third_blog/zblog/media_files/',
    )
    TEMPLATE_DIRS = (
        'C:/django/third_blog/zblog/templates',
    )
    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # Uncomment the next line to enable the admin:
         'django.contrib.admin',
        'blog', 
    )

I fiddled around with the MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, and STATIC URL to get the code working in Linux. I installed pillow-2.8.1 in windows using pip.

This is my html file in template directory:

    {% load staticfiles %}
    <a href = "/">Back to homepage </a>
    {% if list_of_blog_entries %}
        <ol>
        {% for blog_entry in list_of_blog_entries %}
           <li> {% for blog_item in blog_entry %}
                {% if blog_item.image %}
                    <img src = "{{ blog.item.url }}" height=100> </br>
                {% endif %}
            {% endfor %} </li> </br> </br>
        {% endfor %}
        </ol>

    {% else %}
        <p> Blog is empty </p>
    {% endif %}

This is my views.py file:

    # other imports
    import zblog.settings as settings_file
    #from PIL import *
    import PIL
    def list_posts(request):
        """
        Lists the posts with their contents on a single page.
        """

            ###

            # I stands for image or photo.
            if post_item == "I":
                items_in_post.append(post.postimage_set.get(image_id=post_item_id))
                try_image=post.postimage_set.get(image_id=post_item_id)
                print try_image.image.url
                print settings_file.MEDIA_ROOT+try_image.image.url
                print try_image.image
                print
                print
                image_counter += 1


            list_of_blog_entries.append(items_in_post)

        return render_to_response("blog/list_posts.html",  \
                                  {'list_of_blog_entries' : list_of_blog_entries, \
                                   'image_root':settings_file.MEDIA_URL})

I have a lot of other code that I have not posted. I have forms for uploading an image. The image is successfully uploaded and is found in the subdirectory media_file/blog. When I try to list the image with , it shows a broken link to the image. So I printed out the url with "print try_image.image.url" in views.py to find that the url is with respect to the home directory "blog/IMG.jpg". So I added the MEDIA_ROOT and find that I now get the complete directory path "C:/django/third_blog/zblog/media_files/blog/IMG.jpg".

I passed the variable MEDIA_ROOT to the template and changed the template to

    <img src = "{{ image_root }}{{ blog.item.url }}">

Now I get a blank space where the image should have been. Spent the past hour googling and found the links I first used to get it going in Linux. Is there anything specific to Windows? I am using Django 1.7 with Python 2.7.3.

Thanks in advance.

*************EDIT********* My directory structure is:

    C:
        django
            thrid_blog
                zblog
                    blog
                    media_files
                    static
                    zblog
                    manage.py

My settings.py file in the last zblog directory. So, I put:

    BASE_DIR = os.path.dirname(os.path.abspath(__file__))

in my manage.py file and in my settings.py file, I put

    import sys
    sys.path.append("c:\django\third_blog\zblog")
    from manage import BASE_DIR

to get BASE_DIR pointing to the home directory of the project which contains the directories blog, media_files, static, zblog. But it still doesn't work.

Also, even when I hardcode the image into the HTML:

    <img src = "C:\django\third_blog\zblog\media_files\blog\IMG_20141125_104718.jpg" height=100> </br>

It still doesn't show the image. It gives me a blank space.

Shiv Kumar
  • 361
  • 5
  • 16

1 Answers1

0

You want to use the path from

BASE_DIR = os.path.dirname(os.path.dirname(file))

to build your paths for the files. i.e.

# in settings.py
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static', 'static_dirs'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media'),
MEDIA_URL = '/media/'

.join concatenates them properly.

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • Still not working. I put the changes as an edit in my question. – Shiv Kumar Apr 06 '15 at 18:09
  • You don't need to modify manage.py at all. in your template you need {% load staticfiles %} near the top and the format for getting the image would be '{% static "blog\IMG_20141125_104718.jpg" %}' – Pete LoGiudice Apr 07 '15 at 16:41
  • Thanks. Yes, I didn't have to change manage.py. The changes you posted for settings.py are convenient. But MEDIA_ROOT must point to os.path.join(BASE_DIR, 'media_files') and MEDIA_URL must be '/media/'. The main mistake was with MEDIA_URL. Thanks again for your help. – Shiv Kumar Apr 08 '15 at 02:45
  • They were examples showing the use of the os.path BASE_DIR to setup yours. I figured you know to update the names accordingly. – Pete LoGiudice Apr 08 '15 at 14:17