Reputation: 1448
I'm working on a Django web app and I have a problem. I have a page where i list all the results in a MySQL table, and it works fine.
{% for row in db_list %}
<tr>
<th scope="row">{{ row.description }}</th>
<td>
<button type="submit" name="instance" value="{{ row }}" class="btn btn-primary" title="OPEN TABLE" id="test"><i class="fas fa-list-ul"></i></button>
</td>
</tr>
{% endfor %}
Depending on the selection, I have to show the relations of the chosen table, and it works fine with a workaround, but it's not generalistic.
What I'm doing is, as you see, transferring back a single row of the json.
The db_list is:
{
"db_list": [
{
"description": "description",
"table_name": "table_name",
"database_name": "database_name"
},
{
"description": "description",
"table_name": "table_name",
"database_name": "database_name"
},
{
"description": "description",
"table_name": "table_name",
"database_name": "database_name"
}
]
}
I would expect to transfer a json like this:
{
"description": "description",
"table_name": "table_name",
"database_name": "database_name"
}
'{
\'description\': \'description\',
\'table_name\': \'table_name\',
\'database_name\': \'database_name\'
}'
'"{
\'description\': \'description\',
\'table_name\': \'table_name\',
\'database_name\': \'database_name\'
}"'
If i try to do dumped_json['description'] it says the parameter must be integer.. Because of course it still is not a dict. Even if i dumped it.
I already tried surrounding with a dict(), but no.
I already tried replacing the quotes with other quotes or with nothing, but no. Can't dump without quotes, and I keep seeing escapes if I replace with double quotes.
The problem is: it technically keeps being a string. If i try to print it, of course it has no \ escapes. the [0] of the json is the { (instead, is the " in case i did the dump) and not the '. Already tried the ascii decode and all this stuff. It seems the \ escapes are not "physically" present in the string..
Upvotes: -1
Views: 163
Reputation: 1448
I want to give credit to my boy @Seth250 who gave me half of the solution
What I had to do is simply using the literal_eval but, instead of dumping it with the json lib, simply surrounding the literal_eval'ed string into a dict.
instance = ast.literal_eval(string_with_escapes)
instance_json = dict(instance)
the result will, infact, be:
{
'description': 'description',
'table_name': 'table_name',
'database_name': 'database_name'
}
Upvotes: 0