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
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:
Where
current_tabis 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.