|
|
|
from django.shortcuts import render
|
|
|
|
from .forms import *
|
|
|
|
|
|
|
|
|
|
|
|
def userinfo_form(request):
|
|
|
|
if request.method == "GET":
|
|
|
|
myform = UserInfoForm()
|
|
|
|
return render(request, "form_demo/userinfo.html", {'form_obj': myform})
|
|
|
|
else:
|
|
|
|
f = UserInfoForm(request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
print(f.clean())
|
|
|
|
print(f.cleaned_data['username'])
|
|
|
|
print(f.data)
|
|
|
|
else:
|
|
|
|
errors = f.errors
|
|
|
|
print(errors)
|
|
|
|
return render(request, "form_demo/userinfo.html", {'form_obj': f, 'errors': errors})
|
|
|
|
return render(request, "form_demo/userinfo.html", {'form_obj': f})
|
|
|
|
|
|
|
|
|
|
|
|
def img_file_form(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
f = ImgFileForm()
|
|
|
|
return render(request, "form_demo/uploadimg.html", {'form_obj': f})
|
|
|
|
else:
|
|
|
|
f = ImgFileForm(request.POST, request.FILES)
|
|
|
|
print(request.POST)
|
|
|
|
print(request.FILES)
|
|
|
|
if f.is_valid():
|
|
|
|
name = f.cleaned_data['name']
|
|
|
|
heading = f.cleaned_data['heading']
|
|
|
|
userimg = ImgFile()
|
|
|
|
userimg.name = name
|
|
|
|
userimg.heading = heading
|
|
|
|
userimg.save()
|
|
|
|
print('上传成功')
|
|
|
|
print(userimg.heading)
|
|
|
|
return render(request, 'form_demo/uploadimg.html', {'form_obj': f, 'user_img': userimg})
|
|
|
|
|
|
|
|
|
|
|
|
def userinfo_model_form(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
model_form = UserInfoModelForm()
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': model_form})
|
|
|
|
else:
|
|
|
|
f = UserInfoModelForm(request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
print(f.clean())
|
|
|
|
print(f.cleaned_data['username'])
|
|
|
|
print(f.data)
|
|
|
|
f.save()
|
|
|
|
else:
|
|
|
|
errors = f.errors
|
|
|
|
print(errors)
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': f, 'errors': errors})
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': f})
|
|
|
|
|
|
|
|
|
|
|
|
def userinfo_model_from_save(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
a = UserBaseInfo.objects.get(id=1)
|
|
|
|
myform = UserInfoModelForm(instance=a)
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': myform})
|
|
|
|
else:
|
|
|
|
f = UserInfoModelForm(request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
# 直接保存
|
|
|
|
# f.save()
|
|
|
|
# 延时保存
|
|
|
|
user = f.save(commit=False)
|
|
|
|
# user.username = request.username
|
|
|
|
print(user)
|
|
|
|
user.save()
|
|
|
|
else:
|
|
|
|
errors = f.errors
|
|
|
|
print(errors)
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': f, 'errors': errors})
|
|
|
|
return render(request, 'form_demo/userinfomodels.html', {'form_obj': f})
|