我似乎无法找出我的错误在哪里。当我更新用户配置文件时,我收到一条成功消息,但实际上没有任何内容保存到配置文件模型中。
网址
from django.urls import path
from .views import (
user_detail_view,
user_redirect_view,
user_update_view,
profile_update_view,
UserDeleteView,
)
app_name = "users"
urlpatterns = [
path("~redirect/", view=user_redirect_view, name="redirect"),
path("~update/", view=user_update_view, name="update"),
path("~update-profile/", view=profile_update_view, name="profile_update"),
path("<str:username>/delete/", UserDeleteView.as_view(), name="user_delete"),
path("<str:username>/", view=user_detail_view, name="profile"),
]
形式
class UserProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image', 'instagram', 'twitter', 'public_encryption_key', 'user_info']
意见
class ProfileUpdateView(LoginRequiredMixin, UpdateView):
model = Profile
template_name = 'users/user_profile_form.html'
fields = ["image", 'instagram', 'twitter', 'public_encryption_key', 'user_info']
def get_success_url(self):
return reverse("users:profile", kwargs={"username": self.request.user.username})
def get_object(self):
return User.objects.get(username=self.request.user.username)
def form_valid(self, form):
form.instance.profile = get_author(self.request.user)
form.save()
return super().form_valid(form)
profile_update_view = ProfileUpdateView.as_view()