Django Redirect with non-default table

Viewed 45

In my Django project, I have a generic view. It has a single select form in which the user can choose a formset from a list of formsets to load.

Then, a POST request is sent to the server, requesting the new formset. The view is reloaded with the new formset while remaining on the same URL.

Here's an example:

Assume there are two tables: default_formset and formset_2

If I am making updates on my default_formset the redirect works perfectly. The issue I am having is with 'updating' non-default formsets like formset_2.

When an update is made on formset_2 on redirect it will load up the default_formset rather than loading up formset_2

In foo.py:

def generic_view(request):
    #Here, the user's input determines what formset is rendered
    formset_names = SingleSelectForm(formsets, 'formset_name', request.POST)
    
    #This variable gets the user's input
    formset_selected = request.POST.get('formset_name', 'default_formset')
    
    ''' '''
    Forms are initialized
    ''' '''
    
    if request.POST:
        if 'formset_name' in request.POST:
            formset = the_formset(form_kwargs=empty_forms, initial=initial_values)
    
        if 'update' in request.POST:
            if formset.is_valid():
                for form in formset:
                    if form.has_changed():
                        # Do some mysql stuff
                         return HttpResponseRedirect("/foo/")

In urls.py:

from website.foo import foo

    urlpatterns = [
        path('foo/', foo.generic_view, name='generic_view'),
    ]

If a user updated a row in formset_2, is it possible to reload this generic_view with the updated formset_2 instead of default_formset?

0 Answers
Related