Django视图呈现奇怪的url路径

时间:2014-03-17 05:36:45

标签: django django-forms django-views django-urls

奇怪的问题。我将完成编辑用户配置文件的过程,然后在最后将用户返回到配置文件详细信息页面,但我的网址仍然是/ edit。

基本上我可以编辑一个配置文件,它会将我带到正确保存的profile/edit,然后我加载配置文件详细信息页面然后我的网址路由不会更改。一个小问题,但一个烦恼。代码如下:

views.py

def index(request):
....
return render(request, "profile/details.html", {'user': user})

def edit(request):

user_obj = User.objects.get(pk=request.user.id)
user_pro_obj = UserProfile.objects.get(user=request.user.id)

if request.method == "POST":
  uform = UserForm(data = request.POST, instance=user_obj)
  pform = UserProfileForm(data = request.POST, instance=user_pro_obj)

  if uform.is_valid() and pform.is_valid():
    user = uform.save()
    profile = pform.save(commit=False)
    profile.user = user
    profile.save()

    user = get_object_or_404(User, id=request.user.id)
    return render(request, "profile/detail.html", {'user': user})

else:
  uform = UserForm(instance=user_obj)
  pform = UserProfileForm(instance=user_pro_obj)

return render(request, "profile/edit.html", {'uform': uform, 'pform': pform}

urls.py

url(r'^$', views.index, name='profile_index'),
url(r'^edit$', views.edit, name='profile_edit'),

edit.py

<form action={% url 'profile_edit' %} method="post">{% csrf_token %}
{{ uform.as_p }}
{{ pform.as_p }}
<input type="submit" value="Submit">

1 个答案:

答案 0 :(得分:3)

您无法自行更改浏览器中显示的网址。您需要重定向到详细信息页面,而不是rendering详细信息页面。像:

return HttpResponseRedirect(my_success_url)