Reputation: 115
I am trying to display the images from databse to HTML template,but I am getting an import error:
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: no such table: Image
here are the files:models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Image(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
image = models.FileField(upload_to='post_image', blank=True)
from django.shortcuts import render
from django.db import connection
from accounts.models import Image
from django.contrib.auth.models import User
# Create your views here.
def home(request):
cursor = connection.cursor()
cursor.execute('''SELECT image FROM Image where 'user'="admin"''')
row = cursor.fetchone()
print (row)
args = {'row':row}
return render(request, 'accounts/home.html', args)
{% extends 'base.html' %}
<h1>Home</h1>
<html>
<head>
<title>HOME</title>
{% block head %}
<title>HOME</title>
{% endblock %}
</head>
<body>
<h2 align="center"> SQL Queries display </align> </h2>
{% block content %}
{{ row }}
{% endblock %}
{% block body %}
<h1>Base</h1>
{% endblock %}
</body>
</html>
I am storing data on the file system, I have uploaded two images for the user 'admin'.
Upvotes: 1
Views: 45
Reputation: 723
As your App is named "accounts", Django is probably creating the table as "accounts_image", and the resulting query should be:
"SELECT image FROM accounts_image WHERE ..."
Why don't you use the ORM to get the data? It'll be easier for you:
row = Image.objects.get(user__username="admin")
Also, remember that the "user" field in the Image model is a ForeignKey, which means that you can't query user="admin".
Upvotes: 1