首页 > 代码库 > django ImageField用法

django ImageField用法

settings里的设置

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(‘__file__‘)))

MEDIA_ROOT = os.path.join(PROJECT_ROOT, ‘media‘)

MEDIA_URL = ‘/media/‘

models里的设置

img = models.ImageField(upload_to=‘photo‘, null=True, blank=True)

cateimg = models.ImageField(upload_to=‘img/%Y/%m/%d‘)

forms里的设置

cateimg = forms.ImageField(label=‘图片‘, help_text=‘不能超过5M!‘)

form = TopicNewForm(request.POST, request.FILES)

cateimg=request.FILES[‘cateimg‘]

或者

model = User

urls里的设置

from django.conf import settings
from django.conf.urls.static import static

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在html里面的调用

<img src="http://www.mamicode.com/media/{{ ret.img }}">