You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.1 KiB
93 lines
3.1 KiB
from django.shortcuts import render |
|
from django.http import JsonResponse |
|
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}) |
|
|
|
|
|
def ajax_login(request): |
|
return render(request, 'form_demo/ajax_login.html') |
|
|
|
|
|
def ajax_login_data(request): |
|
username = request.POST.get('username') |
|
password = request.POST.get('password') |
|
if username == 'admin' and password == 'admin123': |
|
return JsonResponse({'code': 1, 'msg': '登录成功'}) |
|
else: |
|
return JsonResponse({'code': 0, 'msg': '登录失败'})
|
|
|