2

I have profile edit page with several tabs. Each tab is a separate page, so when I access it, I add "tab" parameter:

/edit/?tab=general 
/edit/?tab=contacts

When user finishes to fill a form on one of these tabs (for example, /edit/?tab=general), he submit it. If form is not valid, I need to render it with errors. Browser URL input need to be filled with /edit/?tab=general, but when I render page, URL is /edit/.

Can I change it somehow? Thanks.

Code:

def _profile_edit_general(request):
profile = request.user.get_profile()
if request.method == 'POST':
    form = forms.ProfileEditGeneralForm(request.POST, instance=profile)
    if form.is_valid():
        form.save()
else:
    form = forms.ProfileEditGeneralForm(instance=profile)
return render_template(request, 'profiles/edit/general.html', {
    'profile_edit_general_form': form
})

@login_required
def profile_edit(request):
if request.method == 'POST':
    if 'profile_edit_general_submit' in request.POST:
        return _profile_edit_general(request)
else:
    tab = request.GET.get('tab')
    if tab == 'general':
        return _profile_edit_general(request)
    else:
        return Http404

1 Answer 1

3

It sounds like the problem is that your form action doesn't include the query string.

Normally with Django forms and views, you'll be posting to the same URL as you're already on, which means you can do something like this:

<form method="post" action="?tab={{ current_tab }}">

...

</form>

Where current_tab is a context variable indicating which tab is selected. Based on your code at the moment, you'll need to add it to the dict in your render_template call. If you're using JavaScript to switch tabs, you'll need to update the action attribute when the tab switches too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.