简体   繁体   English

在 Django 中更新自定义用户 (AbstractUser)

[英]Updating custom user (AbstractUser) in Django

I created a custom user using CBV and i have succeeded in implementing signup view however i am finding difficult to implement the update view.我使用 CBV 创建了一个自定义用户,并且我已经成功地实现了注册视图,但是我发现很难实现更新视图。 I have implemented the update view using the following code:我已经使用以下代码实现了更新视图:

models.py模型.py

class CustomUser(AbstractUser):
   state = models.CharField(choices=States, default ='abia', max_length=50 )
   city = models.CharField(max_length=50)  
   local_government = models.CharField(max_length=50)  
   phone_number = models.CharField(max_length=50, blank= True)  
   picture = models.ImageField(upload_to='user/photos', blank=False, null=False, validators= 
    [clean_image])

forms.py表格.py

  class CustomUserCreationForm(UserCreationForm):

    class Meta:
      model = CustomUser
      fields = UserCreationForm.Meta.fields + ('state', 'city', 'local_government', 'phone_number', 
         'picture',)

  class CustomUserChangeForm(UserChangeForm):

    class Meta:
       model = CustomUser
       fields = UserCreationForm.Meta.fields + ('state', 'city', 'local_government', 'phone_number', 
       'picture',)

admin.py管理文件

 class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser

   list_display = ['id', 'first_name', 'last_name', 'email','subscription_plan', 'state', 'city', 
     'local_government', 'phone_number', 'picture']

views.py (update): views.py(更新):

 class UpdateUser( LoginRequiredMixin, UpdateView):
   model = CustomUser
   form_class = CustomUserChangeForm
   template_name = 'profile/profile-edit.html'
   login_url = 'login'

The UpdateUser page just reload when i click to update profile without committing the update.当我点击更新配置文件而不提交更新时,UpdateUser 页面会重新加载。 Any help will be appreciated.任何帮助将不胜感激。

由于您正在更新用户对象,因此您应该将 pk 值发送到模板中的 URL 标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM