1.选择好模板,并分析页面
2.修改并自定义基础模板
3.修改各个页面,修改title,面包屑等
4.定义好用户信息的View
5. 数据绑定,数据回填
6. 配置路径利用include
7. 模板显示,在模板中显示默认值:{{request.user.mobile|default_if_none:”}}
{% load staticfiles %}
{% block custom_css %}{% endblock %}
{% block header %}{% endblock %}
{% block bread %}{% endblock %}
{% block content_left %}{% endblock %}
{% block content_right %}{% endblock %}
{% block header %}{% endblock %}
{% block custom_js %}{% endblock %}
{% block header %}
{% endblock %}
{% block content_right %}
个人信息
{% endblock %}
url(r'^user_center/', include('users.url', namespace='user_info')),# organiztion 的url分发,namespace用于重名的处理
urlpatterns = [
url(r’^user_info/$’, UserInfoView.as_view(), name=’user_info’),
]
class UserInfoView(View):
def get(self,request):
next_page = request.path #获取来时路径
#判断是否登陆
if not request.user.is_authenticated():
#return render(request,'usercenter_info.html',{})
target_url = '/login/?next='+next_page #拼凑成与login对应的url
return HttpResponseRedirect(target_url) # 转到来时页面
if request.user.is_authenticated():
return render(request,'usercenter_info.html',{})
通用视图解说
通用视图
1.上传文件和图片 ,需要定制一个url,并配置相应的后台处理视图函数view对其进行接收处理
2.通用视图(登陆、权限等)的继承 10-1
3.定制modelform,进行表单提交
4.视图函数中实例化form
5.文件上传会放在了request.FILES
6.前端利用form进行文件的上传,form表单的提交都需要加上csrf-token,视图函数利用model_form进行数据转换
7.image_form.clean_data[‘image’]获取上传文件信息,注意字段与前端上传的name保持一致
8.通过request.user.image=image进行修改信息
9.利用save()进行保存
10.利用HttpResponse进行信息的反馈,提示用户是否成功
url(r'^user_img_upload/$', UserImgUploadView.as_view(), name='user_img_upload'),
class UserImgUploadForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['img']
class UserImgUploadView(View):
def post(self,request): #上传是用post方法
next_page = request.path #获取来时路径
#判断是否登陆
if not request.user.is_authenticated():
target_url = '/login/?next='+next_page #拼凑成与login对应的url
return HttpResponseRedirect(target_url) # 转到来时页面
if request.user.is_authenticated():
#可以进行图片上传工作
img_upload_form = UserImgUploadForm(request.POST,request.FILES)
if img_upload_form.is_valid():
#验证通过后会将上传图片放在cleaned_data内
#image = img_upload_form.cleaned_data['img']
image = img_upload_form.files['image'] #尝试直接从files里面取值
#对获取的图片进行保存
request.user.img = image
request.user.save()
1.逻辑类似user视图中的修改密码
2.利用ajax请求异步修改密码
3.后台进行逻辑处理,并利用HttpResponse返回修改信息
4.利用HttpResponse返回json数据,返回错误信息 .errors
5.js文件中需要写入绝对路径
6., content_type=’application/json’,这个参数别忘了
#用户密码修改
url(r'^password_update/$', UserPasswordUpdateView.as_view(), name='password_update'),
is_valid()合法后的逻辑处理,验证后的数据保存在实例化后返回的cleaned_data中,cleaned_data是个字典的数据格式,错误信息保存在form.errors中比如说想在views中查看所有报错信息print(f.errors)
class UserPasswordUpdateView(View):
def post(self,request):
password_reset_form = PasswordResetForm(request.POST) #新密码上传至表单中
if password_reset_form.is_valid(): #新设置的密码符合表单的话
password = request.POST.get('password','')
password2 = request.POST.get('password2','')
if password == password2: #两个密码相同,所有逻辑正确,进行密码修改
#在数据库中信息密码修改
request.user.password = make_password(password)
request.user.save()
return HttpResponse(json.dumps({'status': 'success'}),content_type='application/json')
else: #如果两个密码不相同
return HttpResponse(json.dumps({'status': 'fail','msg':'密码有误'}), content_type='application/json') # 将register_form数据传递给Template
else: #表单验证不通过
return HttpResponse(json.dumps({'status': 'fail', 'msg': password_reset_form.errors}),content_type='application/json') # 将register_form数据传递给Template
1.配置验证码发送url,用于发送邮箱验信息及验证码的上传,注意Ajax里面用的是get还是post方法
2.配置视图函数,首先先进行登陆判断,是否是已注册邮箱的判断
3.调用以前邮箱发送功能,配置发送email和send_type
4.登陆邮箱获取验证码,填写完整信息,利用post进行发送
5.查看model是否定义错误
#接收邮箱验证码
url(r'^send_email_code/$', EmailCodeView.as_view(), name='send_email_code'),
#发送更新邮箱验证码
class EmailCodeView(View):
def get(self,request):
#判断是否登陆,如果未登陆则回到login页面
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/') # 转到来时页面
else:
email = request.GET.get('email','')
# 查询该邮箱是否已经存在,如果有人已经用了该邮箱则不可以继续用
whether_exit_email = UserProfile.objects.filter(email=email)
if whether_exit_email:
#告诉用户该邮箱已经存在
return HttpResponse(json.dumps({'email': '邮箱已经存在'}), content_type='application/json') # 将register_form数据传递给Template
else:
#邮箱不存在为可用邮箱,则发送邮箱验证码,并在EmailVerifyCode进行存储数据
send_email(email, send_type='update') #发送验证码
return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json') # 将register_form数据传递给Template
//修改个人中心邮箱验证码
function sendCodeChangeEmail($btn){
var verify = verifyDialogSubmit(
[
{id: '#jsChangeEmail', tips: Dml.Msg.epMail, errorTips: Dml.Msg.erMail, regName: 'email', require: true}
]
);
if(!verify){
return;
}
$.ajax({
cache: false,
type: "get", //注意这里是用的get方法啦
dataType:'json',
url:"/user_center/send_email_code/",
data:$('#jsChangeEmailForm').serialize(),
async: true,
beforeSend:function(XMLHttpRequest){
$btn.val("发送中...");
$btn.attr('disabled',true);
},
success: function(data){
if(data.email){
Dml.fun.showValidateError($('#jsChangeEmail'), data.email);
}else if(data.status == 'success'){
Dml.fun.showErrorTips($('#jsChangeEmailTips'), "邮箱验证码已发送");
}else if(data.status == 'failure'){
Dml.fun.showValidateError($('#jsChangeEmail'), "邮箱验证码发送失败");
}else if(data.status == 'success'){
}
},
complete: function(XMLHttpRequest){
$btn.val("获取验证码");
$btn.removeAttr("disabled");
}
});
}
#修改个人中心邮箱
url(r'^update_email/$', UpdateEmailView.as_view(), name='update_email'),
#邮箱更新view
class UpdateEmailView(View):
def post(self,request):
# 判断是否登陆,如果未登陆则回到login页面
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/') # 转到来时页面
email = request.POST.get('email', '')
code = request.POST.get('code', '')
# 查询该邮箱及验证码信息
exit_email = EmailVerifyCode.objects.filter(email=email,code=code)
if exit_email: #有该条记录则验证正确可以进行修改,完成验证码验证功能,可以进行邮箱修改
request.user.email = email
request.user.save()
return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json')
else:
return HttpResponse(json.dumps({'status': 'fail'}), content_type='application/json')
url(r'^user_info/$', UserInfoView.as_view(), name='user_info'),
class UserInfoView(View):
def get(self,request):
next_page = request.path #获取来时路径
#判断是否登陆
if not request.user.is_authenticated():
#return render(request,'usercenter_info.html',{})
target_url = '/login/?next='+next_page #拼凑成与login对应的url
return HttpResponseRedirect(target_url) # 转到来时页面
if request.user.is_authenticated():
return render(request,'usercenter_info.html',{})
def post(self,request): #注意form表单需要加入csrf-tpken
user_info_form = UserInfodForm(request.POST)
if user_info_form.is_valid():
request.user.nick_name = request.POST.get('nick_name')
request.user.birthday = request.POST.get('birthday')
request.user.gender = request.POST.get('gender')
request.user.adress = request.POST.get('adress')
request.user.mobile = request.POST.get('mobile')
request.user.save()
return HttpResponse(json.dumps({'status': 'success'}),content_type='application/json') # 将register_form数据传递给Template
else:
return HttpResponse(json.dumps({'status': 'failure','msg':user_info_form.errors}),content_type='application/json') # 将register_form数据传递给Template
//保存个人资料
$('#jsEditUserBtn').on('click', function(){
var _self = $(this),
$jsEditUserForm = $('#jsEditUserForm')
verify = verifySubmit(
[
{id: '#nick_name', tips: Dml.Msg.epNickName, require: true}
]
);
if(!verify){
return;
}
$.ajax({
cache: false,
type: 'post',
dataType:'json',
url:"/user_center/user_info/",
data:$jsEditUserForm.serialize(),
async: true,
beforeSend:function(XMLHttpRequest){
_self.val("保存中...");
_self.attr('disabled',true);
},
success: function(data) {
if(data.nick_name){
_showValidateError($('#nick_name'), data.nick_name);
}else if(data.birday){
_showValidateError($('#birth_day'), data.birday);
}else if(data.address){
_showValidateError($('#address'), data.address);
}else if(data.status == "failure"){
Dml.fun.showTipsDialog({
title: '保存失败',
h2: data.msg
});
}else if(data.status == "success"){
Dml.fun.showTipsDialog({
title: '保存成功',
h2: '客官你的信息修改好了哦,爱你~!'
});
setTimeout(function(){window.location.href = window.location.href;},1500);
}
},
complete: function(XMLHttpRequest){
_self.val("保存");
_self.removeAttr("disabled");
}
});
});