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>
转载于:https://blog.51cto.com/4249964/1602506