我可以在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)
有谁能发现我在这里做错了什么?
答案 0 :(得分:0)
有趣的是,在重新同步我的数据库后,我不再有问题了。