+ 2

Help me

def show_data(request): db = sqlite3.connect(":memory:") db = sqlite3.connect("/home/user/Desktop/data.db") cursor = db.cursor() cursor.execute("select * from user") db.commit() x = cursor.fetchall() return render(request , 'first/template.html' , x) How to fix "context must be a dict rather than list."

20th Sep 2020, 11:52 AM
Rice Noodle
Rice Noodle - avatar
4 Answers
+ 4
The item x is a list, but you need to pass a dictionary in the render function. And you can reference a key value pair using the key in the template. You could do: return render(request, 'first/template.html', {data: x}) In the template you could iterate through the list.
20th Sep 2020, 12:21 PM
Arnesh
Arnesh - avatar
+ 2
Your data will be needed as a dictionary.
20th Sep 2020, 12:12 PM
JaScript
JaScript - avatar
+ 2
Arnesh Thanks it works i am verry happy :D
20th Sep 2020, 12:26 PM
Rice Noodle
Rice Noodle - avatar
+ 2
Rice Noodle what you did is not in anyway related to how database works in django. The error is from the last line, when the render function accepts "x" as it's argument, it expects a context of type dict but everything in your code are bugs too. You can learn the full django tutorial from youtube, search 🔍 for this user "CoreyMS" or "Corey Schafer"
20th Sep 2020, 12:26 PM
Mirielle
Mirielle - avatar