2

I can upload images in Django admin and they appear in the correct directory, but when I have these issues when I try to view them in my templates:

{% if city.main_image %} returns false in my template

{{ city.main_image.url }} returns nothing

{{ city.main_image }} returns None

When I click the admin preview I get this 404:

Request URL:    http://127.0.0.1:8000/admin/search/city/1/test/test.jpg/
city object with primary key u'1/test/test.jpg' does not exist.

The rest of the template outputs the correct information associated with the city object. Here are my settings.py urls:

MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "html/static")
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

My urls.py:

from django.conf.urls.defaults import patterns, include, url
from test import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('test.search.views',
    url('^search/$', 'search', name="home_page"),
    url(r'^(\d+)/$', 'city', name="city_page"),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )

My models.py:

class City(models.Model):
    name = models.TextField("Course Name", blank=False)
    address = models.TextField("Address", blank=False)
    city = models.TextField("City", blank=False)
    state = models.TextField("State", blank=False)
    country = models.TextField("Country", blank=False)
    main_image = models.ImageField(upload_to="test", blank=True)

Can anyone spot what I'm doing wrong here?

HighLife
  • 4,218
  • 7
  • 40
  • 56
  • Are you sure you have the `{{ city }}` variable in your templates? What value does it have in DB? Don't you override this settings somewhere (you can check with `from django.conf import settings; print settings.MEDIA_URL`) – ilvar May 22 '12 at 05:23
  • `print settings.MEDIA_URL` returns `/media/` as expected. I'm sure my template contains the `{{ city }}` variable. If I add `{{ city.name }}` or `{{ city.address }}` to a random place in the template it will output the proper text, yet main_image does not output correctly. – HighLife May 22 '12 at 07:02

1 Answers1

0

Interesting, after re-syncing my database I'm no longer having issues.

HighLife
  • 4,218
  • 7
  • 40
  • 56