Friday, 8 January 2021

Django: Parse HTML (containing form) to dictionary

For testing the HTML that gets created by my Django application, I would like to extract specific attributes and values from the <form>...</form> tags in the response.content which I get from my Django TestClient:

response = client.get('/foo/')

# response contains <form> ...</form>

data = parse_form(response.content)

data['my-input']='bar'

response = client.post('/foo/', data)

For this example:

<form>
 <input type="text" name="my-input" value="initial">
</form>

The result should be:

{'my-input': 'initial'}

I read the testing responses docs but could not find anything like this.

There is an feature request, but it was rejected several years ago: https://code.djangoproject.com/ticket/11797



from Django: Parse HTML (containing form) to dictionary

No comments:

Post a Comment