Django入门学习Day23:我的账户视图

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

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 %}我的账号{% endblock %}

{% block breadcrumb %}
  
{% endblock %}

{% block content %}
  
{% csrf_token %} {% include 'includes/form.html' %}
{% endblock %}
Django入门学习Day23:我的账户视图_第1张图片

原文:https://github.com/pythonzhichan/django-beginners-guide/blob/master/ClassBasedViews2.md

你可能感兴趣的:(Django入门学习Day23:我的账户视图)