python - Image not displaying Django 1.8 ImageField -
here's models.py
class item(models.model): author = models.foreignkey('auth.user') name = models.charfield(max_length=200) price = models.decimalfield(max_digits=20,decimal_places=2) description = models.textfield() created_date = models.datetimefield(default=timezone.now) image = models.imagefield(upload_to=settings.media_root) def list(self): self.published_date = timezone.now() self.save() def __str__(self): return self.name
views.py
def item_list(request): items = item.objects.filter(created_date__lte=timezone.now()).order_by('-created_date') return render_to_response('item_list.html', {'items': items}, context_instance=requestcontext(request))
urls.py
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'', include('shop.urls')) ] + static(settings.static_url, document_root=settings.static_root)
and settings.py
base_dir = os.path.dirname(os.path.dirname(__file__)) debug = true allowed_hosts = [] # application definition installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'shop', ) middleware_classes = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 'django.middleware.security.securitymiddleware', ) root_urlconf = 'djangoshop.urls' templates = [ { 'backend': 'django.template.backends.django.djangotemplates', 'dirs': [os.path.join(base_dir, 'shop/templates/shop/')], 'app_dirs': true, 'options': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] wsgi_application = 'djangoshop.wsgi.application' # database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases databases = { 'default': { 'engine': 'django.db.backends.mysql', 'host': '', 'name': 'django_db', 'user': 'root', 'password': '', } } # internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ language_code = 'en-us' time_zone = 'utc' use_i18n = true use_l10n = true use_tz = true # static files (css, javascript, images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ static_url = '/static/' static_root = os.path.join(base_dir, 'static') media_root = os.path.join(base_dir, 'media') media_url = '/users/grasbergerm/projects/djangoshop/djangoshop/media/'
i've looked @ other posts on topic , i've followed solutions can't seem work on end. in page source have
<img scr="/users/grasbergerm/projects/djangoshop/djangoshop/media/5_hat_1_2_b8a44lz.jpg">
and if go url can see image won't show in page. i've been stuck on way long appreciated.
edit: template:
{% item in items %} <img scr="{{item.image.url}}"> <br> {{ item.name }}<br> {{ item.author }}<br> ${{ item.price }}<br> {% endfor %}
first of all:
image = models.imagefield()
you dont need upload_to=settings.media_root
because default imagefields.
and media_url
should media_url = '/media/'
and in template, need:
{% item in items %} <img src="{{ item.image.url }}"> {% endfor %}
update:
you need tell local machine explicitly serve user uploaded files doing this
urlpatterns = [ # ... rest of urlconf goes here ... ] + static(settings.media_url, document_root=settings.media_root)
Comments
Post a Comment