Django入门与实践-第24章:我的账户视图

By 刘志军, 2020-01-07, 分类: django教程

django

好的,那么,这部分将是我们最后的一个视图。之后,我们将专心来改进现有功能。

accounts/views.py 查看完整文件

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic import UpdateView

@method_decorator(login_required, name='dispatch')
class UserUpdateView(UpdateView):
    model = User
    fields = ('first_name', 'last_name', 'email', )
    template_name = 'my_account.html'
    success_url = reverse_lazy('my_account')

    def get_object(self):
        return self.request.user

myproject/urls.py 查看完整文件

from django.conf.urls import url
from accounts import views as accounts_views

urlpatterns = [
    # ...
    url(r'^settings/account/$', accounts_views.UserUpdateView.as_view(), name='my_account'),
]

templates/my_account.html

{% extends 'base.html' %}

{% block title %}My account{% endblock %}

{% block breadcrumb %}
  <li class="breadcrumb-item active">My account</li>
{% endblock %}

{% block content %}
  <div class="row">
    <div class="col-lg-6 col-md-8 col-sm-10">
      <form method="post" novalidate>
        {% csrf_token %}
        {% include 'includes/form.html' %}
        <button type="submit" class="btn btn-success">Save changes</button>
      </form>
    </div>
  </div>
{% endblock %}


有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅

猜你喜欢

2020-01-07
Django入门与实践-第20章:QuerySets(查询结果集)
2020-05-21
django charfield 字段在 admin 中使用 textarea
2020-01-07
Django入门与实践-第23章:分页实现
2020-01-07
Django入门与实践-第27章:项目部署
2020-01-07
Django入门与实践-第14章:用户注册
2014-03-21
Django-Compressor 静态文件压缩
2014-02-08
Django url()函数详解
2014-03-31
Django Signals 从实践到源码分析
2019-05-16
Django Admin 日期字段格式化
2019-05-24
Django 数据模型 ForeignKey 的 on_delete 属性的可选值