python - Django edit auth user profile -


i'm new django , writing application in django 1.11.

i want create profile update page.

i have created app accounts manage profile related activities , created class

from django.contrib.auth.models import user  # create views here. 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' 

and in myapp/accounts/urls.py

from django.conf.urls import url  . 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') ] 

when access 127.0.0.1:8000/accounts/update, gives

attributeerror @ /accounts/update/  generic detail view updateprofile must called either object pk or slug. 

since, want logged in user edit his/her profile information. don't want pass pk in url.

how create profile update page in 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 

as error told you, have return pk or slug if you're not precising object. overridding get_object method, can tell django object want update.

if prefer on way, can send pk or slug of object in url :

url(r'^update/(?p<pk>\d+)', views.updateprofile.as_view(), name='update') 

here default get_object method catch pk in args , find user want update.

note first method works (as wrote it) if user want update profile , authenticated (self.request.user) , second way allows update whatever user want, have pk of user (accounts/update/1, update user pk=1, etc...).

some doc here, get_object() section

returns object view displaying. default requires self.queryset , pk or slug argument in urlconf, subclasses can override return object.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -