Django 成功重定向到配置文件表单在/profile/join()处给出TypeError-参数必须是str或bytes,而不是';非类型';
我有一个名为UserProfile的扩展用户模型,更新视图使用successessagexin在成功更新时重定向。问题是django代码中有东西试图将一个未设置的变量(路径)连接到该路径,我不知道为什么 user\u profile/views.pyDjango 成功重定向到配置文件表单在/profile/join()处给出TypeError-参数必须是str或bytes,而不是';非类型';,django,python-3.x,django-views,django-2.2,Django,Python 3.x,Django Views,Django 2.2,我有一个名为UserProfile的扩展用户模型,更新视图使用successessagexin在成功更新时重定向。问题是django代码中有东西试图将一个未设置的变量(路径)连接到该路径,我不知道为什么 user\u profile/views.py class UserProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView): model = UserProfile form_class = UserPro
class UserProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = UserProfile
form_class = UserProfileChangeForm
success_url = reverse_lazy("user_profile:profile")
# success_url = "/success/"
success_message = "Profile updated"
def get_object(self, *args, **kwargs):
return self.request.user
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, instance=request.user)
if form.is_valid():
profile = form.save(commit=False)
profile.save()
return render(request, self.template_name, {"form": form})
提交具有更改数据的表单时堆叠跟踪(数据实际上会得到更新,因此这纯粹是一个重新显示问题)
我错过了什么?根据文档,设置success\u url就足够了,而且只需要“/profile/”链接,因为没有人可以维护另一个用户配置文件(即url中不需要pk、slug等)你自己做得太多了。您应该让Django执行样板代码。这将调用,然后将返回app/modelname\u form.html
作为模板名。因此,不要覆盖post
方法:
class UserProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = UserProfile
form_class = UserProfileChangeForm
success_url = reverse_lazy('user_profile:profile')
success_message = 'Profile updated'
def get_object(self, *args, **kwargs):
return self.request.user
# no post method override
class UserProfileView(LoginRequiredMixin、SuccessMessageSixin、UpdateView):
model=UserProfile
form\u class=UserProfileChangeForm
success\u url=reverse\u lazy('user\u profile:profile'))
成功消息='Profile updated'
def get_对象(self、*args、**kwargs):
返回self.request.user
#无post方法覆盖
您没有指定模板
。此外,如果成功发布,请不要呈现响应。它使用的表单继承了UserChangeForm,因此模板会自动设置为userprofile\u form.html。如果我将该呈现取出,然后我得到一个错误视图user_profile.views.UserProfileView没有返回HttpResponse对象。它没有返回任何结果。
-现在我有点困惑了-我应该在这里呈现什么?真的不应该定义post
。UpdateView将为您处理此问题。
class UserProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = UserProfile
form_class = UserProfileChangeForm
success_url = reverse_lazy('user_profile:profile')
success_message = 'Profile updated'
def get_object(self, *args, **kwargs):
return self.request.user
# no post method override