你好,我是 Django 的新手,我正在尝试建立我的个人博客。 在这个博客中,我希望我的帖子有一个可以张贴图片的字段。 这是我的代码,但是当我看到我的帖子时,图片没有显示。
设置.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
静态文件(CSS、JavaScript、图片)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = (
os.path.join(BASE_DIR, "media")
)
MEDIA_URL = 'media/'
模型.py
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">
当我访问我的帖子详情网站时,它只显示 alt = "img"
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 不提供静态文件或媒体文件,这是一项通常落在服务器上的任务,以便在您的 urls.py
文件末尾的某处添加以下内容:
# 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 提供了关于如何设置环境以及如何为静态和媒体提供文件的完整文档: https://docs.djangoproject.com/en/1.7/howto/static-files/
关于python - Django 博客文章图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349801/