【发布时间】:2019-11-29 05:20:17
【问题描述】:
我有一个表单,其中显示了用户个人资料中的一些字段以及其他内容。在此表单中,配置文件中的字段被禁用。如果详细信息已过时,用户可以转到编辑配置文件视图并更新详细信息。为此,我在表单模板中包含了一个按钮。更新配置文件后,我希望用户返回表单。最后一部分是我遇到问题的部分。
从表单中编辑个人资料的链接如下所示:
<a class="btn btn-outline-info" href="{% url 'profile'%}?next={% url 'submission_resume' %}">Edit Profile</a>
这是来自表单的 url 的外观:
http://127.0.0.1:8000/users/profile/?next=/submissions/resume/
问题是在编辑配置文件视图中我不知道如何实现条件重定向:
def profile(request):
""" View and edit the profile. It requires user to be login """
if request.method == 'POST':
form = UserModifyForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
messages.success(request, f'Your account has been updated!')
#??? if next in url go back to the form ???
#??? return redirect(???) ???
#??? else: ???
return redirect('profile')
else:
form = UserModifyForm(instance=request.user)
#--> Dict for template with the forms
context = {
'form': form,
}
#--> Show profile page
return render(request, 'user_profile.html', context)
#---
【问题讨论】: