这是我的models.py
Date
Views.py
moment.tz("Europe/London").format(); // 2016-01-15T09:21:08-07:00
urls.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
和settings.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))
我已经查看了有关此主题的其他帖子,并且我已经按照他们的解决方案进行了操作,但我似乎无法让它在我的最终工作。在页面源我有
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('shop.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
如果我转到该网址,我可以看到该图片,但它不会显示在我的网页中。我已经被困在这个问题太久了所以任何帮助都会受到赞赏。
编辑:这是我的模板:
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/'
答案 0 :(得分:2)
首先:
image = models.ImageField()
您不需要upload_to=settings.MEDIA_ROOT
,因为它是图像字段的默认值。
和MEDIA_URL
应该只是MEDIA_URL = '/media/'
在您的模板中,您需要:
{% for item in items %}
<img src="{{ item.image.url }}">
{% endfor %}
<强>更新强>:
您需要tell your local machine explicitly通过执行此操作来提供用户上传的文件
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)