首页 > 代码库 > django 学习个人总结 之form提交放到数据库中
django 学习个人总结 之form提交放到数据库中
project:test04
app:blog
models.py
from django.db import models
class User(models.Model):
username = models.CharField(max_length=20)
headImg = models.FileField(upload_to=‘./upload/‘)
views.py
from django.shortcuts import render_to_response from django import forms from django.http import HttpResponse from 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() user = User.objects.create(username = username ,headImg = headImg) print username,headImg return HttpResponse(‘ok‘) else: uf = UserForm() return render_to_response(‘index.html‘,{‘uf‘:uf})
templates/index.html
<html>
<head>
</head>
<body>
<h1>hello</h1>
<form method=‘post‘ enctype=‘multipart/form-data‘>
{{uf.as_p}}
<button type=‘sumbit‘>ok</button>
</form>
</body>
</html>
本文出自 “expect批量同步数据” 博客,请务必保留此出处http://4249964.blog.51cto.com/4239964/1602506
django 学习个人总结 之form提交放到数据库中
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。