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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。