Django编辑个人信息

一 编写视图mysite/account/views.py

from django.shortcuts import render
# HttpResponseRedirect实现URL的转向
from django.http import HttpResponse,HttpResponseRedirect
# Django默认的用户认证和管理中的两个方法
from django.contrib.auth import authenticate, login
from .forms import LoginForm,RegistrationForm,UserProfileForm,UserInfoForm,UserForm
from .models import UserProfile, UserInfo
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required


@login_required(login_url='/account/login/')
def myself_edit(request):
    '''
    先把已有的用户信息读出来,然后判断用户请求是POST还是GET。如果是GET,则显示表单
    并将用户已有信息也显示在其中,如果是POST,则接收用户提交的表单信息,然后更新各个数据模型实例属性的值
    '''
    user = User.objects.get(username=request.user.username)
    userprofile = UserProfile.objects.get(user=request.user)
    userinfo = UserInfo.objects.get(user=request.user)

    if request.method == "POST":
        user_form = UserForm(request.POST)
        userprofile_form = UserProfileForm(request.POST)
        userinfo_form = UserInfoForm(request.POST)
        if user_form.is_valid() * userprofile_form.is_valid() * userinfo_form.is_valid():
            user_cd = user_form.cleaned_data
            userprofile_cd = userprofile_form.cleaned_data
            userinfo_cd = userinfo_form.cleaned_data
            user.email = user_cd['email']
            userprofile.birth = userprofile_cd['birth']
            userprofile.phone = userprofile_cd['phone']
            userinfo.school = userinfo_cd['school']
            userinfo.company = userinfo_cd['company']
            userinfo.profession = userinfo_cd['profession']
            userinfo.address = userinfo_cd['address']
            userinfo.aboutme = userinfo_cd['aboutme']
            user.save()
            userprofile.save()
            userinfo.save()
        # 页面转向 http://locahost:8000/account/my-information/
        return HttpResponseRedirect('/account/my-information/')
    else:
        user_form = UserForm(instance=request.user)
        userprofile_form = UserProfileForm(initial={"birth":userprofile.birth, "phone":userprofile.phone})
        userinfo_form = UserInfoForm(initial={"school":userinfo.school, "company":userinfo.company, "profession":userinfo.profession, "address":userinfo.address, "aboutme":userinfo.aboutme})
        return render(request, "account/myself_edit.html", {"user_form":user_form, "userprofile_form":userprofile_form, "userinfo_form":userinfo_form})

二 编写mysite/templates/account/myself_edit.html

{% extends "base.html" %}

{% block title %}我的信息{% endblock %}

{% block content %}

编辑我的信息

{% csrf_token %}
用户名:
{{user.username}}
邮箱:
{{user_form.email}}
生日:
{{userprofile_form.birth}}
电话:
{{userprofile_form.phone}}
毕业学校:
{{userinfo_form.school}}
工作单位:
{{userinfo_form.company}}
职业:
{{userinfo_form.profession}}
地址:
{{userinfo_form.address}}
个人介绍:
{{userinfo_form.aboutme}}

picture

{% endblock %}

三 配置URL mysite/account/urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views

urlpatterns = [
    # 自定义登录
    # url(r'^login/$', views.user_login, name='user_login'),
    # django内置的登录
    url(r"^login/$", auth_views.login, name="user_login"),
    url(r"^new-login/$", auth_views.login, {"template_name": "account/login.html"}),
    url(r'^logout/$', auth_views.logout, {"template_name": "account/logout.html"}, name='user_logout'),
    url(r'^register/$', views.register, name="user_register"),
    # 给password_change方法传入post_change_redirect参数
    url(r'^password-change/$', auth_views.password_change, {"post_change_redirect": "/account/password-change-done"},name='password_change'),
    url(r'^password-change-done/$', auth_views.password_change_done, name='password_change_done'),
    # 重置密码开始
    url(r'^password-reset/$', auth_views.password_reset, {"template_name":"account/password_reset_form.html","subject_template_name":"account/password_reset_subject.txt", "email_template_name":"account/password_reset_email.html",  "post_reset_redirect":"/account/password-reset-done"}, name="password_reset"),
    url(r'^password-reset-done/$', auth_views.password_reset_done, {"template_name":"account/password_reset_done.html"}, name="password_reset_done"),
    url(r'^password-reset-confirm/(?P[-\w]+)/(?P[-\w]+)/$', auth_views.password_reset_confirm, {"template_name":"account/password_reset_confirm.html", "post_reset_redirect":"/account/password-reset-complete"}, name="password_reset_confirm"),
    url(r'^password-reset-complete/$', auth_views.password_reset_complete, {"template_name":"account/password_reset_complete.html"}, name="password_reset_complete"),
    # 重置密码结束
    # 个人信息
    url(r'^my-information/$', views.myself, name="my_information"),
    # 编辑个人信息
    url(r'^edit-my-information/$', views.myself_edit, name="edit_my_information"),
]

四 提供修改编辑用户信息入口mysite/templates/account/myself.html

{% extends "base.html" %}

{% block title %}我的信息{% endblock %}

{% block content %}

我的信息

用户名:
{{user.username}}
邮箱:
{{user.email}}
生日:
{{userprofile.birth}}
电话:
{{userprofile.phone}}
毕业学校:
{{userinfo.school}}
工作单位:
{{userinfo.company}}
职业:
{{userinfo.profession}}
地址:
{{userinfo.address}}
个人介绍:
{{userinfo.aboutme}}
{% endblock %}

五 测试

1 http://localhost:8000/account/my-information/

Django编辑个人信息_第1张图片

2 点击编辑我的信息

Django编辑个人信息_第2张图片

3 页面展示

Django编辑个人信息_第3张图片

你可能感兴趣的:(Django)