-2

I want to build a url. My function adds the word kwargs to the result, which it should not.

custom_redirect('mieteinheit', kwargs={"wohnungsgruppenname": form.cleaned_data["wohnungsgruppenname"]})

def custom_redirect(url_name, *args, **kwargs):
    #some code is passed
    "?%s" % urllib.parse.urlencode(kwargs)

This creates:

?kwargs=%7B%27wohnungsgruppenname%27%3A+%27ghs%27%7D

But it should create

?wohnungsgruppenname%3Dwohnungsgruppenname

Any ideas?

Ralf
  • 16,086
  • 4
  • 44
  • 68
Josef Soe
  • 91
  • 8

1 Answers1

0

You missunderstood how keyword arguments are used.

Instead of calling your function like you do, try this instead:

custom_redirect(
    'mieteinheit', 
    wohnungsgruppenname=form.cleaned_data["wohnungsgruppenname"])

This way, the word 'wohnungsgruppenname' will become a key in the dict kwargs. The way you did it, it creates a nested dict, which is not what you nedd.


See this answer for a better explanation of how kwargs works.

Ralf
  • 16,086
  • 4
  • 44
  • 68