django在定义模型时需要一些上传的文件,例如图片
class Banner(models.Model):
""“
轮播图models
titles 标题
images 轮播图
url 访问地址
index 轮播地址
add_time 添加时间
"""
title = models.CharField(max_length=100, verbose_name=u'标题')
image = models.ImageField(upload_to='banner/%Y/%m', verbose_name=u'轮播图', max_length=400)
index = models.IntegerField(default=100, verbose_name=u'轮播顺序')
class Meta:
verbose_name = u'轮播图'
verbose_name_plural = verbose_name
def __str__(self):
return self.title
比如上面的我要上传文件,那么就必须设置在settings里设置media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
在url里添加
from django.conf.urls import url
from django.contrib import admin
from 项目名 import settings
from django.views.static import serve
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]