2

我可以在 Django admin 中上传图像,它们会出现在正确的目录中,但是当我尝试在模板中查看它们时遇到这些问题时:

{% if city.main_image %}在我的模板中返回 false

{{ city.main_image.url }}什么都不返回

{{ city.main_image }}返回无

当我单击管理员预览时,我得到这个 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.

模板的其余部分输出与城市对象关联的正确信息。这是我的settings.py网址:

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

我的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,
        }),
   )

我的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)

谁能发现我在这里做错了什么?

4

1 回答 1

0

有趣的是,重新同步我的数据库后,我不再遇到问题。

于 2012-05-22T07:20:54.943 回答