我有一个名为UserProfile的扩展用户模型,更新视图使用SuccessMessageMixin在成功更新时进行重定向。问题是django代码中的某些内容试图将未设置的var(路径)连接到路径,但我不确定为什么。
user_profile / views.py
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})
提交具有更改的数据的表单时的堆栈跟踪(数据实际上确实已更新,因此这纯粹是一个重新显示的问题)。
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\ntpath.py" in join
89. for p in map(os.fspath, paths):
During handling of the above exception (expected str, bytes or os.PathLike object, not NoneType), another exception occurred:
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\contrib\auth\mixins.py" in dispatch
52. return super().dispatch(request, *args, **kwargs)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "D:\users\mjnic\OneDrive\Workspaces\Django\pyp\src\pyp\user_profile\views.py" in post
30. return render(request, self.template_name, {"form": form})
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\loader.py" in render_to_string
61. template = get_template(template_name, using=using)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\loader.py" in get_template
15. return engine.get_template(template_name)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\backends\django.py" in get_template
34. return Template(self.engine.get_template(template_name), self)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\engine.py" in get_template
143. template, origin = self.find_template(template_name)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\engine.py" in find_template
125. template = loader.get_template(name, skip=skip)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\loaders\base.py" in get_template
18. for origin in self.get_template_sources(template_name):
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\template\loaders\filesystem.py" in get_template_sources
36. name = safe_join(template_dir, template_name)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\utils\_os.py" in safe_join
32. final_path = abspath(join(base, *paths))
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\ntpath.py" in join
115. genericpath._check_arg_types('join', path, *paths)
File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\genericpath.py" in _check_arg_types
149. (funcname, s.__class__.__name__)) from None
Exception Type: TypeError at /profile/
Exception Value: join() argument must be str or bytes, not 'NoneType'
我想念什么?根据文档,设置success_url应该就足够了,并且只需要“ / profile /”链接,因为没有人可以维护另一个用户的配置文件(即,URL中不需要pk,slug等)
答案 0 :(得分:1)
您自己做的太多了。您应该让Django UpdateView
[Django-doc]执行样板代码。这将调用get_template_names()
method [Django-doc],它实际上将返回app/modelname_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