文件上传:1.配置路径文件上传时,文件数据存储在request.FILES属性中一般文件都存储到服务器的static目录下的upfile文件夹下在setting.py文件中配置路径:#静态文件目录STATICFILES_DIRS=[os.path.join(BASE_DIR,'static')]#文件上传目录MDEIA_ROOT=os.path.join(BASE_DIR,r'static\upfile')2.定义文件跳转路径在前台的urls.py文件中配置请求路径:path('upfile/',views.upfile),path('saveFile/',views.saveFile),3.定义upfile.html文件{% csrf_token %}文件:4.在views.py文件中定义跳转和上传方法def upfile(request):return render(request,"front/upfile.html")import osfrom django.conf import settingsdef saveFile(request):if request.method == 'POST':f = request.FILES['file']filePath = os.path.join(settings.MDEIA_ROOT,f.name)with open(filePath,'wb') as fp:for info in f.chunks():fp.write(info)return HttpResponse("上传成功!")else:return HttpResponse("上传失败!")
#分页功能1.定义urls.pypath('userList//',views.userList),2.定义views.pyfrom .models import UserInfofrom django.core.paginator import Paginatordef userList(request,pageNum):#获取所有用户allList = UserInfo.objects.all()#获取分页对象(总数,每页条数)paginator = Paginator(allList,2)page = paginator.page(pageNum)return render(request,"front/userList.html",{"userinfos":page})3.定义userList.html页面{% for user in userinfos %}{{ user.id }} | {{ user.username }} | {{ user.password }} | {{ user.sex }} | {{ user.age }} | {{ user.isDelete }} |
{% endfor %}
{% for index in userinfos.paginator.page_range %}- {{index}}
{% endfor %}