0

Working in Django 2.1, I'm trying to genuinely forge a GET request that will resolve as a valid form after a redirect in the most DRY possible way.

Basically I am in a view (some_view below) and I want to generate a GET request without prior knowledge on the way the form fields have to be serialized.

urls.py

path( r'select/', views.filtered_view, name='filter-view' )
path( r'save/', views.save_view, name='save-view' )

views.py

# a form class with 'non-trivial' fields (e.g. not ints)
class FilterForm(forms.Form):
    date = forms.DateInput()
    user = forms.ModelChoiceField(queryset=User.objects.all())

def filtered_view( request ):
    filter_form = FilterForm(request.GET)
    # do something with the form...

# another view somewhere 
def some_view(request):
    # this view does not contains GET or POST parameters related to FilterForm.
    ...
    user = User.objects.get( some_filter )
    date = compute_some_datetime_date_object

    # generates here a GET request that will instantiate the FilterForm 
    # correctly
    someargs = ...
    return redirect( 'filter-view' + someargs )

I know I can do something like (skipping the urllib.parse.urlencode...):

return redirect( 'filter-view' ) + \
  '?user='+ str(user_object.id) + \
  '&date' + date.strftime('%Y-%m-%d')

but it assumes I know that the pk is used to pass user object (depends on the form) and that I know also the format to use for the date...

What I would like to do is something like:

ff = FilterForm(data={'user':user_object,'date':date})
return redirect( 'filter-view' ) + ff.as_url_parameters()

Thanks for the help.

Bob Morane
  • 183
  • 1
  • 7
  • I'm a little confused as to the intention here. It appears you are trying to do manually what the HTML form will do automatically if you give it `method="GET"` (which is the default anyway) and the appropriate `action` attribute? – Robin Zigmond Apr 08 '19 at 08:53
  • It will of course do that if a user submits a view from a template. But in my case I want to **redirect** the user to a view which depends on some GET parameter. As these GET parameters depends on the form, I don't know their exact format – Bob Morane Apr 08 '19 at 09:05
  • so you want to dynamically determine which page to redirect to depending on what is submitted in the form? That should be straightforward enough using `if` statements, if there's something in your exact requirements making it hard then you need to give more detail. Note that what you've put in the question looks like your redirect will always be to the `filter-view` page and it's only the GET parameters that would alter (and in that case, as I said, the form itself will do the necessary work). – Robin Zigmond Apr 08 '19 at 09:10
  • Ah, now I see what you mean... I've edited the comment in the code to be more specific. The point is that `some_view` has not been called with a FilterForm in the request. It comes from completely different context, thus I cannot instantiate the FilterForm in the traditional way in this view – Bob Morane Apr 08 '19 at 09:48
  • Apologies, I'm still struggling to understand this. If I've got it right so far, the user is going to the page handled by the `some_view` view, and you want it to redirect to the `filtered_view` page, with some query parameters that are dynamically determined. And that these parameters do NOT come from a form. So the obvious question is: what is the logic you want implemented for what the query parameters should be? – Robin Zigmond Apr 08 '19 at 09:57
  • Also it looks like [this question](https://stackoverflow.com/questions/3765887/add-request-get-variable-using-django-shortcuts-redirect) may be of some assistance for what you're trying to do? – Robin Zigmond Apr 08 '19 at 10:04

0 Answers0