[英]Django blog post image
Hello i'm new in Django and i am trying to build my personal blog. 您好,我是Django的新手,我正在尝试建立我的个人博客。 in this blog i want my post to have a field where i can post an image.
在此博客中,我希望我的帖子有一个可以发布图片的字段。 here is my code but when i see my post the picture doesn't show.
这是我的代码,但是当我看到我的帖子时,图片没有显示。
setting.py Django settings for mysite project. setting.py mysite项目的Django设置。
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'i9s&v1wse3w%gxyc&3qyft-a(cz1y6^f9gvy+rxsypnsozmt7d'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_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',
'blog',
) )
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',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Berlin'
USE_I18N = True
USE_L10N = True
USE_TZ = False
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = (
os.path.join(BASE_DIR, "media")
)
MEDIA_URL = 'media/'
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
image = models.ImageField(upload_to='media/img')
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
<img src="{{ post.image.url }}" alt="img">
when i visit my post details site it only shows the alt = "img" 当我访问我的帖子详细信息网站时,它仅显示alt =“ img”
views.py views.py
from django.shortcuts import render , get_object_or_404
from django.utils import timezone
from .models import Post
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post':post})
Django by default does not serve static files or media files, this is a task that usually falls to a server, in order to get STATIC or MEDIA files served during development (and that should be only during development) in your urls.py
file somewhere at the end add the following: Django默认情况下不提供静态文件或媒体文件,这是一项通常落在服务器上的任务,目的是在开发过程中(应该仅在开发过程中)在您的
urls.py
文件中获取STATIC或MEDIA文件最后添加以下内容:
# Import settings if not imported
from django.conf import settings
# Import static if not imported
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Django provides full documentation on how to setup your environment and how your files should be served for static and media: https://docs.djangoproject.com/en/1.7/howto/static-files/ Django提供了有关如何设置环境以及如何为静态和媒体提供文件的完整文档: https : //docs.djangoproject.com/en/1.7/howto/static-files/
Finally i got it working with your help. 终于我在您的帮助下工作了。 There is no need to use the
{{MEDIA_URL}}
just use the .url
in the html. 无需使用
{{MEDIA_URL}}
只需使用html中的.url
。 Thank you again this was my first answer in stackoverflow 再次谢谢你,这是我在stackoverflow中的第一个答案
You have to fit
MEDIA_URL
in front of img src
您必须在
img src
前面fit
MEDIA_URL
Try something like this 试试这个
<img src="{{ MEDIA_URL }}{{ post.image.url }}" alt="img">
Before this, You have to setup url
for media in dev
environment.in urls.py
在此之前,您必须在
dev
环境中设置媒体的url
。在urls.py
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In settings 在设置中
MEDIA_URL = '/media/`
MEDIA_ROOT = '/path/to/image/'
Update for fix your program 更新以修复您的程序
MEDIA_ROOT = (
BASE_DIR,
)
MEDIA_URL = '/media/'
In html
在
html
<img src="{{ MEDIA_URL }}{{ post.image.url }}" alt="img">
In urls.py
在
urls.py
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will do the trick 这将解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.