Django STATIC_URL 为空,图像不显示 [英] Django STATIC_URL is empty, images doesn't show
问题描述
我知道有很多这样的问题,我阅读了所有问题并尝试了所有方法,但没有解决我的问题.
I know that there is plenty of questions like that, I read them all and tried everything, however nothing solved my problem.
我正在用 Django 写博客之类的东西,当然我想向用户展示图片.图片存储在 /static/users/
.
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>
.
我的setting.py
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/'
我在 html 中有什么
What I have in html
{% load static from staticfiles %}
<img src="{% static comment.author.picture.url %}"
width="70px" height="70px">
以及我的观点
@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
我不能只将 static
添加到 html 中的 url,因为它在其他页面上不起作用.
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?
推荐答案
在这种情况下,您似乎需要引用 MEDIA 而不是 STATIC.
It appears that you need to refer to MEDIA instead of STATIC in this case.
查看设置中的 MEDIA_ROOT
和 MEDIA_URL
以确保它们指向正确的位置.
Take a look at your MEDIA_ROOT
and MEDIA_URL
in settings to ensure they're pointed to the right place.
然后,在您的模板中,您可以通过属性获取对象的 URL:
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">
文档中有关 MEDIA 的更多信息:https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-MEDIA_ROOT
More on MEDIA in the docs: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-MEDIA_ROOT
这篇关于Django STATIC_URL 为空,图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!