django表单处理(四)

Posted on Posted in django
Tips: 本文创建于2014年11月23日,已超过 2 年,内容或图片可能已经失效!

f2076672b9142eb301080776cf9ea1d7

基本表单处理

编辑设置

  1. vim?mysite/settings.py
  2. MIDDLEWARE_CLASSES?=?(
  3. ????'django.contrib.sessions.middleware.SessionMiddleware',
  4. ????'django.middleware.common.CommonMiddleware',
  5. ????#'django.middleware.csrf.CsrfViewMiddleware',???#这行注释掉
  6. ????'django.contrib.auth.middleware.AuthenticationMiddleware',
  7. ????'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  8. ????'django.contrib.messages.middleware.MessageMiddleware',
  9. ????'django.middleware.clickjacking.XFrameOptionsMiddleware',
  10. )

编辑视图处理

  1. vim?myapp/views.py
  2. from?django?import?forms
  3. from?django.http?import?HttpResponse
  4. from?django.shortcuts?import?render_to_response
  5. class?UserForm(forms.Form):
  6. ????????name?=?forms.CharField()
  7. def?register(req):
  8. ????????if?req.method?==?'POST':
  9. ????????????????form?=?UserForm(req.POST)???#绑定
  10. ????????????????if?form.is_valid():?#对字段进行基本的验证,是否存在
  11. ????????????????????????print?form.cleaned_data
  12. ????????????????????????return?HttpResponse('ok')
  13. ????????else?:
  14. ????????????????form?=?UserForm()
  15. ????????return?render_to_response('register.html',{'form':form})

编辑模板文件

  1. vim?blog/templates/register.html
  2. <html>
  3. <head>
  4. <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
  5. <title></title>
  6. </head>
  7. <body>
  8. <form?method="post">
  9. {{form}}
  10. <input?type="submit"?value="OK"?/>
  11. </form>
  12. </body>
  13. </html>

×××××××××××××××××××××××××××××××××××
图片上传

  1. vim?blog/views.py
  2. from?django.shortcuts?import?render_to_response
  3. from?django.http?import?HttpResponse
  4. from?django?import?forms
  5. class?UserForm(forms.Form):
  6. ????????username?=?forms.CharField()
  7. ????????headImg?=?forms.FileField()
  8. def?register(req):
  9. ????????if?req.method?==?"POST":
  10. ????????????????uf?=?UserForm(req.POST,?req.FILES)??#接受传过来的文件
  11. ????????????????if?uf.is_valid():
  12. ????????????????????????print?uf.cleaned_data['username']
  13. ????????????????????????print?uf.cleaned_data['headImg'].name
  14. ????????????????????????print?uf.cleaned_data['headImg'].size
  15. ????????????????????????fp?=?file('/home/lhh/mysite/upload/'+uf.cleaned_data['headImg'].name,?'wb')?#以二进制操作
  16. ????????????????????????s?=?uf.cleaned_data['headImg'].read()
  17. ????????????????????????fp.write(s)?#保存
  18. ????????????????????????fp.close()
  19. ????????????????????????return?HttpResponse('ok')
  20. ????????else?:
  21. ????????????????uf?=?UserForm()
  22. ????????return?render_to_response('register.html',{'uf':uf})
  1. vim?blog/templates/register.html
  2. <html>
  3. <head>
  4. <meta?http-equiv="Content-Typy"?content="text/html;?charset=utf-8"?/>
  5. <title>form</title>
  6. </head>
  7. <body>
  8. <div/>yonghu?register</div>
  9. <div>
  10. <form?enctype="multipart/form-data"?method="post">
  11. {{uf.as_p}}
  12. <input?type="submit"?value="ok"?/>
  13. </form>
  14. </div>
  15. </body>
  16. </html>

×××××××××××××××××××××××××××××××××××
图片上传2,通过admin对表的操作直接在后台管理
views 与上面一样

  1. vim?blog/models.py
  2. from?django.db?import?models
  3. #?Create?your?models?here.
  4. class?User(models.Model):
  5. ????????username?=?models.CharField(max_length=32)
  6. ????????headImg?=?models.FileField(upload_to="./upload/")
  7. ????????def?__unicode__(self):
  8. ????????????????return?self.username

同步数据库

  1. python?manage.py?makemigrations
  2. python?manage.py?migrate
  3. python?manage.py?syncdb
  1. vim?blog/admin.py
  2. from?django.contrib?import?admin
  3. from?blog.models?import?User
  4. #?Register?your?models?here.
  5. admin.site.register(User)

上传的路径可以在setting里修改

  1. vim?mysite/settings.py
  2. MEDIA_ROOT?=?""

在admin中添加用户后,上传的图片就在定义的路径里了
×××××××××××××××××××××××××××××××××××
结合上面两种方法

  1. vim??blog/models.py
  2. from?django.db?import?models
  3. #?Create?your?models?here.
  4. class?User(models.Model):
  5. ????????username?=?models.CharField(max_length=32)
  6. ????????headImg?=?models.FileField(upload_to='./upload/')
  7. ????????def?__unicode__(self):
  8. ????????????????return?self.username
  9. vim?blog/admin.py
  10. from?django.contrib?import?admin
  11. from?blog.models?import?User
  12. #?Register?your?models?here.
  13. admin.site.register(User)
  14. python?manage.py?makemigrations
  15. python?manage.py?migrate
  16. python?manage.py?syncdb
  17. vim?blog/views.py
  18. from?django.shortcuts?import?render_to_response
  19. from?django.http?import?HttpResponse
  20. from?django?import?forms
  21. from?blog.models?import?User
  22. class?UserForm(forms.Form):
  23. ????????username?=?forms.CharField()
  24. ????????headImg?=?forms.FileField()
  25. def?register(req):
  26. ????????if?req.method?==?"POST":
  27. ????????????????uf?=?UserForm(req.POST,?req.FILES)
  28. ????????????????if?uf.is_valid():
  29. ????????????????????????username?=?uf.cleaned_data['username']
  30. ????????????????????????headImg?=?uf.cleaned_data['headImg']
  31. ????????????????????????user?=?User()
  32. ????????????????????????user.username?=?username
  33. ????????????????????????user.headImg?=?headImg
  34. ????????????????????????user.save()
  35. ????????????????????????print?username,?headImg
  36. ????????????????????????return?HttpResponse('ok')
  37. ????????else?:
  38. ????????????????uf?=?UserForm()
  39. ????????return?render_to_response('register.html',{'uf':uf})
  40. vim?blog/templates/register.html
  41. <html>
  42. <head>
  43. <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
  44. </head>
  45. <body>
  46. <form?method="post"?enctype="multipart/form-data">
  47. {{uf.as_p}}
  48. <input?type="submit"?value="OK"?/>
  49. </form>
  50. </body>
  51. </html>
» 转载请注明来源:呢喃 » django表单处理(四)

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 16 =