Update/Change Django中ModelForm中当前User实例的信息
Update/Change information of current User instance in ModelForm in Django
如何通过扩展 一对一关系 个人资料页面来编辑用户(first_name、last_name、电子邮件)的用户实例信息。在我的代码下面没有给我任何错误,但也没有为用户更新数据库中的给定信息。用户模型不是我创建的,但我使用 django.contrib.auth.models.User 我也不想使用 Django 的 UpdateView class。任何帮助将不胜感激。
Django 版本
asgiref==3.3.1
Django==3.1.5
django-crispy-forms==1.10.0
Pillow==8.1.0
pytz==2020.5
sqlparse==0.4.1
代码:
users/forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Profile
class UserUpdateForm(forms.ModelForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.EmailField()
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image']
users/views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import UserUpdateForm, ProfileUpdateForm
from django.contrib import messages
@login_required
def profile(request):
if request.POST == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, "Your profile has been updated !")
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'title': "Profile",
'act': 'profile',
'u_form': u_form,
'p_form': p_form,
}
return render(request, 'users/profile.html', context)
users/profile.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} {{ title }} {% endblock title %}
{% block content %}
<div class="content-section align-content-center">
<div class="media py-3">
<img src="{{ user.profile.image.url }}" alt="" class="rounded-circle img-thumbnail" height="10" width="180">
<div class="media-body">
<h2 class="account-heading pl-3">{{ user.get_full_name }}</h2>
<p class="text-secondary pl-3">{{ user.email }}</p>
</div>
</div>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Profile Info</legend>
{{ u_form|crispy }}
{{ p_form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Update</button>
</div>
</form>
</div>
{% endblock %}
已解决
错误出现在配置文件视图下方,我正在检查 request.POST == 'POST' 是否应该如下所示。作为 request.POST returns 查询集不是字符串。
@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, "Your profile has been updated !")
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'title': "Profile",
'act': 'profile',
'u_form': u_form,
'p_form': p_form,
}
return render(request, 'users/profile.html', context)
如何通过扩展 一对一关系 个人资料页面来编辑用户(first_name、last_name、电子邮件)的用户实例信息。在我的代码下面没有给我任何错误,但也没有为用户更新数据库中的给定信息。用户模型不是我创建的,但我使用 django.contrib.auth.models.User 我也不想使用 Django 的 UpdateView class。任何帮助将不胜感激。
Django 版本
asgiref==3.3.1
Django==3.1.5
django-crispy-forms==1.10.0
Pillow==8.1.0
pytz==2020.5
sqlparse==0.4.1
代码:
users/forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Profile
class UserUpdateForm(forms.ModelForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.EmailField()
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image']
users/views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import UserUpdateForm, ProfileUpdateForm
from django.contrib import messages
@login_required
def profile(request):
if request.POST == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, "Your profile has been updated !")
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'title': "Profile",
'act': 'profile',
'u_form': u_form,
'p_form': p_form,
}
return render(request, 'users/profile.html', context)
users/profile.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} {{ title }} {% endblock title %}
{% block content %}
<div class="content-section align-content-center">
<div class="media py-3">
<img src="{{ user.profile.image.url }}" alt="" class="rounded-circle img-thumbnail" height="10" width="180">
<div class="media-body">
<h2 class="account-heading pl-3">{{ user.get_full_name }}</h2>
<p class="text-secondary pl-3">{{ user.email }}</p>
</div>
</div>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Profile Info</legend>
{{ u_form|crispy }}
{{ p_form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Update</button>
</div>
</form>
</div>
{% endblock %}
已解决
错误出现在配置文件视图下方,我正在检查 request.POST == 'POST' 是否应该如下所示。作为 request.POST returns 查询集不是字符串。
@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, "Your profile has been updated !")
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'title': "Profile",
'act': 'profile',
'u_form': u_form,
'p_form': p_form,
}
return render(request, 'users/profile.html', context)