setting配置 MEDIA_ROOT = os.path.join(BASE_DIR,"blog","media","upload") MEDIA_URL = "/media/"
urls配置: from blogCMS import settings from django.views.static import serve # media配置 url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), url(r'^uploadFile/$',views.uploadFile),
假如涉及到文件上传问题,需要做如上配置
头像预览问题:
<div class="avator_div cc"> <label for="avator">头像:</label> <img src="/static/img/default.png" class="avator_img" id="preScan"> <input type="file" id="avator" class="avator_file"> </div>
$("#avator").change(function () {
console.log("OK");
var first_file = $(this)[0].files[0];
var reader = new FileReader();
reader.readAsDataURL(first_file);
reader.onload=function () {
$("#preScan").attr("src",this.result)
}
})