Django静态URL为空,图像不';不显示
我知道有很多这样的问题,我读了所有的问题,尝试了所有的方法,但是没有任何方法解决我的问题 我正在用Django写类似博客的东西,当然我想向用户展示图片。 图片存储在Django静态URL为空,图像不';不显示,django,django-templates,Django,Django Templates,我知道有很多这样的问题,我读了所有的问题,尝试了所有的方法,但是没有任何方法解决我的问题 我正在用Django写类似博客的东西,当然我想向用户展示图片。 图片存储在/static/users//中 我的设置.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')]
/static/users//
中
我的设置.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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',
'django.core.context_processors.static'
],
},
},
]
WSGI_APPLICATION = 'Travel.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/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.9/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = '/users/'
我在html中有什么
{% load static from staticfiles %}
<img src="{% static comment.author.picture.url %}"
width="70px" height="70px">
我在浏览器中指向图像的链接如下所示
http://127.0.0.1:8000/users/default/default.png
http://127.0.0.1:8000/static/users/default/default.png
当他们必须看起来像这样的时候
http://127.0.0.1:8000/users/default/default.png
http://127.0.0.1:8000/static/users/default/default.png
我不能在html中将静态添加到url,因为它在其他页面上不起作用
那么,我错过了什么?或者这里出了什么问题?尝试将媒体URL更改为“/static/users/”尝试将媒体URL更改为“/static/users/”在这种情况下,您似乎需要引用媒体而不是静态
在设置中查看您的MEDIA\u ROOT
和MEDIA\u URL
,确保它们指向正确的位置
然后,在模板中,可以通过属性获取对象的URL:
文档中关于介质的更多信息:在本例中,您似乎需要参考介质而不是静态介质
在设置中查看您的MEDIA\u ROOT
和MEDIA\u URL
,确保它们指向正确的位置
然后,在模板中,可以通过属性获取对象的URL:
文档中关于媒体的更多信息:您要显示的图片是否为实际媒体(表示它们在db中管理)?是的,它们是@PatrickBeesonAre您要显示的图片是否为实际媒体(表示它们在db中管理)?是的,它们是@PatrickBeeson