سؤال

Odd issue. I go through the process of editing a user profile then at the end return the user to a profile details page but my url remains with a /edit.

Basically I can edit a profile which takes me to the profile/edit which saves correctly and then I load the profile details page after however my url route does not change. A minor issue but an annoyance. Code is provided below:

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">
هل كانت مفيدة؟

المحلول

You can't change the URL displayed in the browser yourself. Instead of rendering the detail page, you need to redirect to it. Like:

return HttpResponseRedirect(my_success_url)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top