Django STATIC_URL为空,图像不显示 [英] Django STATIC_URL is empty, images doesn't show
问题描述
我在Django中写博客,当然我想向用户展示图片。
图片存储在 / static / users /< username> /< image>
。
我的setting.py
TEMPLATES = [
{
'BACKEND':'django.template。 backend.django.DjangoTemplates',
'DIRS':[os.path.join(BASE_DIR,'templates')]
,
'APP_DIRS':True,
'选项':{
'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'
#数据库
#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'),
}
}
#密码验证
# 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',
},
]
#国际化
#https://docs.djangoproject.com/en/1.9/topics/i18n /
LANGUAGE_COD E ='en-us'
TIME_ZONE ='UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#静态文件(CSS,JavaScript,图像)
#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中有什么
{%static static from staticfiles%}
< img src ={%static comment.author.picture.url%}
width =70pxheight = 70像素 >
我在观看次数中
@login_required
def trip_one(request,pk):
trip = get_object_or_404(Trip,pk = pk)
如果request.method =='POST' :
form = CommentForm(request.POST)
如果form.is_valid():
cd = form.cleaned_data
user = Person.objects.get_by_natural_key(request.user.get_username ())
comment =评论(text = cd ['text'],
author = user,
trip = trip)
comment.trip = trip
comment .save()
comments = trip.comment_set.all()
form = CommentForm()
return render(request,'TravelBuddy / travel.html',{'tripps}
'form':form,
'comments':comments})
我在浏览器中的图片链接看起来像这样
http://127.0.0.1:8000/users/default /default.png
当他们看起来像这样
http://127.0.0.1:8000/static/users/default/default.png
我不能在html中添加 static
到url,因为它在其他页面上不起作用。
那么,我缺少什么?或者这里有什么问题?
在这种情况下,您似乎需要参考MEDIA而不是STATIC。 >
在设置中查看您的 MEDIA_ROOT
和 MEDIA_URL
以确保他们指向正确的地方。
然后,在您的模板中,您可以通过以下属性获取对象的URL:
< img src ={{comment.author.picture.url}}width =70pxheight =70px>
更多关于媒体的文档: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-MEDIA_ROOT
I know that there is plenty of questions like that, I read them all and tried everything, however nothing solved my problem.
I'm writing something like blog in Django and of course I want to show pictures to users.
Pictures are stored at /static/users/<username>/<image>
.
My setting.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/'
What I have in html
{% load static from staticfiles %}
<img src="{% static comment.author.picture.url %}"
width="70px" height="70px">
and what I have in views
@login_required
def trip_one(request, pk):
trip = get_object_or_404(Trip, pk=pk)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = Person.objects.get_by_natural_key(request.user.get_username())
comment = Comment(text=cd['text'],
author=user,
trip=trip)
comment.trip = trip
comment.save()
comments = trip.comment_set.all()
form = CommentForm()
return render(request, 'TravelBuddy/travel.html', {'trips': trip,
'form': form,
'comments': comments})
And my link to images in browser looks like this
http://127.0.0.1:8000/users/default/default.png
When they have to look like this
http://127.0.0.1:8000/static/users/default/default.png
I cant just add static
to url in html because it doesn't work on other pages.
So, what I am missing? Or what is wrong here?
It appears that you need to refer to MEDIA instead of STATIC in this case.
Take a look at your MEDIA_ROOT
and MEDIA_URL
in settings to ensure they're pointed to the right place.
Then, in your template, you can get the URL for the object via the attribute:
<img src="{{ comment.author.picture.url }}" width="70px" height="70px">
More on MEDIA in the docs: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-MEDIA_ROOT
这篇关于Django STATIC_URL为空,图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!