[英]Django static media not showing picture
after searching for a solution for hours which did not resolve my problem,I am posting this. 在搜索了几个小时没有解决我问题的解决方案之后,我发布了这个。 The image from my media root is not showing up on my html. 来自我的媒体根目录的图片没有显示在我的html上。 In chrome's console i get a 404 file not found
.Even though the image is there. 在chrome的控制台中,我404 file not found
。即使图像在那里。 I am using Python 3 ,Django 1.10 in Pycharm. 我在Pycharm中使用Python 3,Django 1.10。
This is the model which is where i upload images to: 这是我将图像上传到的模型 :
from django.db import models
class Post(models.Model):
username = "anonymous"
post = models.ImageField(upload_to='anon')
creation_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return Post.username
views.py : views.py :
from django.shortcuts import render,get_object_or_404
from .models import Post
def home(request):
return render(request,"base.html",{})
def post_detail(request,id=None):
instance = get_object_or_404(Post,id=id)
context = {
"post": instance.post,
"instance": instance
}
return render(request,"post_detail.html",context)
post_detail.html (here the image isnt showing): post_detail.html (此处图片未显示):
<body>
<img src = "{{ instance.post.url}}" height="520" width="500"><br>
{{ instance.creation_date }}<br>
{{ instance.username }}<br>
</body>
Parts of setting.py : setting.py的部分内容 :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Post',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Post.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'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 = 'Post.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'Post/media/')
What my directories look like: 我的目录是什么样的:
It's a common mistake to mix up the static and media settings. 混淆静态和媒体设置是一个常见的错误。 In your case what you are actually dealing with is user uploaded MEDIA and not STATICs. 在您的情况下,您实际处理的是用户上传的MEDIA而非STATIC。
<img src = "{{ instance.post.url}}" height="520" width="500"><br>
The settings that are most relevent are MEDIA_* settings described here https://docs.djangoproject.com/en/1.10/howto/static-files/ 最相关的设置是此处描述的MEDIA_ *设置https://docs.djangoproject.com/en/1.10/howto/static-files/
But more importantly, in your dev sever you need to enable the delivery of MEDIA by adding 但更重要的是,在您的开发服务器中,您需要通过添加来启用MEDIA的传递
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.