django 学习-15 .Django文件上传(用户注册)

1、vim blog/views.py

from django.shortcuts  import  render_to_response
from django.http   import HttpResponse
from django  import  forms
from  blog.models   import  User


class  UserForm(forms.Form):
        username = forms.CharField()
        headImg = forms.FileField()


def  register(req):
        if req.method == "POST":
                uf = UserForm(req.POST,req.FILES)
                if uf.is_valid():
                        username = uf.cleaned_data['username']
                        headImg = uf.cleaned_data['headImg']
                        user = User()
                        user.username = username
                        user.headImg = headImg
                        user.save()
                        print username,headImg
                        return HttpResponse('ok')
        else:
          uf = UserForm()
        return render_to_response('register.html',{'uf':uf})

2、vim blog/templates/register.html

<!DOCTYPE>
<html>
<head>
        <title></title>
</head>
<body>
<h1>test<h1>
<form method = "post" enctype="multipart/form-data">
{{uf.as_p}}
<input type="submit" value="nice" />
</body>
</html>
 其实没有太大变化,只是在视图文件里有变化                                                                                                                                                                                                 

posted @ 2016-01-15 20:37  Lz阿飞  阅读(189)  评论(0编辑  收藏  举报