Django 编辑授权用户配置文件
Django edit auth user profile
我是 Django
的新手,正在用 Django 1.11 编写应用程序。
我想创建一个 Profile update
页面。
我创建了一个应用程序 accounts
来管理所有与个人资料相关的活动,并创建了一个 class
from django.contrib.auth.models import User
# Create your views here.
from django.views.generic import TemplateView, UpdateView
class ProfileView(TemplateView):
template_name = 'accounts/profile.html'
class ChangePasswordView(TemplateView):
template_name = 'accounts/change_password.html'
class UpdateProfile(UpdateView):
model = User
fields = ['first_name', 'last_name']
template_name = 'accounts/update.html'
并在 myapp/accounts/urls.py
from django.conf.urls import url
from . import views
app_name = 'accounts'
urlpatterns = [
url(r'^$', views.ProfileView.as_view(), name='profile'),
url(r'^profile/', views.ProfileView.as_view(), name='profile'),
url(r'^change_password/', views.ChangePasswordView.as_view(), name='change_password'),
url(r'^update/', views.UpdateProfile.as_view(), name='update'),
url(r'^setting/', views.SettingView.as_view(), name='setting')
]
当我访问 127.0.0.1:8000/accounts/update
时,它给出
AttributeError at /accounts/update/
Generic detail view UpdateProfile must be called with either an object pk or a slug.
因为,我希望登录用户编辑 his/her 个人资料信息。我不想在 url.
中传递 pk
如何在 Django 1.11 中创建配置文件更新页面?
class UpdateProfile(UpdateView):
model = User
fields = ['first_name', 'last_name']
template_name = 'accounts/update.html'
def get_object(self):
return self.request.user
正如错误告诉您的那样,如果您不精确处理对象,则必须 return pk 或 slug。所以通过覆盖 get_object 方法,你可以告诉 django 你想要更新哪个对象。
如果您更喜欢以其他方式进行,您可以在 url 中发送对象的 pk 或 slug :
url(r'^update/(?P<pk>\d+)', views.UpdateProfile.as_view(), name='update')
此处默认的 get_object
方法将捕获 args 中的 pk 并找到您要更新的用户。
请注意,第一种方法仅在用户想要更新其个人资料并经过身份验证(self.request.user
)时才有效(如我所写),而第二种方法允许您实际更新您想要的任何用户,一旦您拥有此用户的 pk(accounts/update/1
,将使用 pk=1 等更新用户)。
一些文档 here,get_object() 部分
Returns the object the view is displaying.
By default this requires self.queryset
and a pk
or slug
argument
in the URLconf, but subclasses can override this to return any object.
我是 Django
的新手,正在用 Django 1.11 编写应用程序。
我想创建一个 Profile update
页面。
我创建了一个应用程序 accounts
来管理所有与个人资料相关的活动,并创建了一个 class
from django.contrib.auth.models import User
# Create your views here.
from django.views.generic import TemplateView, UpdateView
class ProfileView(TemplateView):
template_name = 'accounts/profile.html'
class ChangePasswordView(TemplateView):
template_name = 'accounts/change_password.html'
class UpdateProfile(UpdateView):
model = User
fields = ['first_name', 'last_name']
template_name = 'accounts/update.html'
并在 myapp/accounts/urls.py
from django.conf.urls import url
from . import views
app_name = 'accounts'
urlpatterns = [
url(r'^$', views.ProfileView.as_view(), name='profile'),
url(r'^profile/', views.ProfileView.as_view(), name='profile'),
url(r'^change_password/', views.ChangePasswordView.as_view(), name='change_password'),
url(r'^update/', views.UpdateProfile.as_view(), name='update'),
url(r'^setting/', views.SettingView.as_view(), name='setting')
]
当我访问 127.0.0.1:8000/accounts/update
时,它给出
AttributeError at /accounts/update/
Generic detail view UpdateProfile must be called with either an object pk or a slug.
因为,我希望登录用户编辑 his/her 个人资料信息。我不想在 url.
中传递pk
如何在 Django 1.11 中创建配置文件更新页面?
class UpdateProfile(UpdateView):
model = User
fields = ['first_name', 'last_name']
template_name = 'accounts/update.html'
def get_object(self):
return self.request.user
正如错误告诉您的那样,如果您不精确处理对象,则必须 return pk 或 slug。所以通过覆盖 get_object 方法,你可以告诉 django 你想要更新哪个对象。
如果您更喜欢以其他方式进行,您可以在 url 中发送对象的 pk 或 slug :
url(r'^update/(?P<pk>\d+)', views.UpdateProfile.as_view(), name='update')
此处默认的 get_object
方法将捕获 args 中的 pk 并找到您要更新的用户。
请注意,第一种方法仅在用户想要更新其个人资料并经过身份验证(self.request.user
)时才有效(如我所写),而第二种方法允许您实际更新您想要的任何用户,一旦您拥有此用户的 pk(accounts/update/1
,将使用 pk=1 等更新用户)。
一些文档 here,get_object() 部分
Returns the object the view is displaying. By default this requires
self.queryset
and apk
orslug
argument in the URLconf, but subclasses can override this to return any object.