2

I can't figure out how to upload an image file in the database per Django rules. I added media roots and media URLs too in my settings.py. I get the error which says:

can only concatenate str (not "InMemoryUploadedFile") to str

This is clearly because I am not being able to send this kind of data in the SQL database using raw SQL

Here is my function in the views.py

@login_required
def course_add(request):
    if request.method=='POST':
        bullyname=request.POST['bullyname']
        contact=request.POST['contact']
        email=request.POST['email']
        facebookLink=request.POST['facebookLink']
        instaLink=request.POST['instaLink']
        linkedinLink=request.POST['linkedinLink']
        snapLink=request.POST['snapLink']
    
        file=request.FILES.get('myfile')
      
        #database connection to the app
        db_cursor=connections['default'].cursor()
    
        #Sql Query for course add
        db_cursor.execute("INSERT INTO University_complains (name,contact,email,facebookid,instaid,linkedinid,snapid,prove) VALUES('"+bullyname+"','"+contact+"','"+email+"','"+facebookLink+"','"+instaLink+"','"+linkedinLink+"','"+snapLink+"','"+file+"')")
    
     return render(request, 'course_add.html')

Here is the class in the models.py section

class Complains(models.Model):
    name=models.CharField(max_length=50,null=False,verbose_name="Bull Name",primary_key=True)
    contact=models.CharField(max_length=15,null=False,verbose_name="Contact No")
    email=models.CharField(max_length=30,verbose_name="E mail")
    facebookid=models.CharField(max_length=100,verbose_name="Facebook ID")
    instaid=models.CharField(max_length=100,verbose_name="Instagram ID")
    linkedinid=models.CharField(max_length=100,verbose_name="Linkedin ID")
    snapid=models.CharField(max_length=100,verbose_name="Snapchat ID")
    prove=models.ImageField(upload_to='proves/')

Also, this is my settings.py for the media section

STATIC_URL = 'static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
farhan
  • 33
  • 4

0 Answers0