Django用户更新

qpgpyjmq  于 2023-07-01  发布在  Go
关注(0)|答案(2)|浏览(104)

我想更新用户的数据,但它不是覆盖数据,而是尝试注册一个新用户。我该怎么做?
这个类看起来像这样:

class SignUpForm(UserCreationForm):
    email = forms.EmailField(label="", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Email Address'}))
    first_name = forms.CharField(label="", max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'First Name'}))
    last_name = forms.CharField(label="", max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Last Name'}))

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')

    def __init__(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)

        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['username'].widget.attrs['placeholder'] = 'User Name'
        self.fields['username'].label = ''
        self.fields['username'].help_text = '<span class="form-text text-muted"><small>Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</small></span>'

        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['placeholder'] = 'Password'
        self.fields['password1'].label = ''
        self.fields['password1'].help_text = '<ul class="form-text text-muted small"><li>Your password can\'t be too similar to your other personal information.</li><li>Your password must contain at least 8 characters.</li><li>Your password can\'t be a commonly used password.</li><li>Your password can\'t be entirely numeric.</li></ul>'

        self.fields['password2'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['placeholder'] = 'Confirm Password'
        self.fields['password2'].label = ''
        self.fields['password2'].help_text = '<span class="form-text text-muted"><small>Enter the same password as before, for verification.</small></span>'

views.py部分如下所示:

def update_user(request):
    if request.user.is_authenticated:
        current_user = User.objects.get(id=request.user.id)
        profile_user = Profile.objects.get(user__id=request.user.id)
        # Get Forms
        user_form = SignUpForm(request.POST or None, request.FILES or None, instance=current_user)
        profile_form = ProfilePicForm(request.POST or None, request.FILES or None, instance=profile_user)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()

            login(request, current_user)
            messages.success(request, ("Your Profile Has Been Updated!"))
            return redirect('home')

        return render(request, "update_user.html", {'user_form':user_form, 'profile_form':profile_form})
    else:
        messages.success(request, ("You Must Be Logged In To View That Page..."))
        return redirect('home')
ekqde3dh

ekqde3dh1#

你正在使用UserCreationForm,它将始终创建一个用户示例。您可以在www.example.com上查看它的代码https://github.com/django/django/blob/main/django/contrib/auth/forms.py#L84。

class SignUpForm(UserCreationForm):

class SignUpForm(UserChangeForm):

用于导入表单:

from django.contrib.auth.form import UserChangeForm
mm9b1k5b

mm9b1k5b2#

from django.contrib.auth.decorators import login_required

def update_user(request):
current_user = request.user
profile_user = Profile.objects.get(user=current_user)

user_form = UpdateUserForm(request.POST or None, instance=current_user)
profile_form = ProfilePicForm(request.POST or None, request.FILES or None, instance=profile_user)

    if user_form.is_valid() and profile_form.is_valid():
      user_form.save()
      current_user.profile = profile_user
      current_user.save()
      messages.success(request, "Your Profile Has Been Updated!")
      return redirect('home')

     return render(request, "update_user.html", {'user_form': user_form, 'profile_form': profile_form})

相关问题