简体   繁体   中英

how can i store picture thats was i edit on my pillow code in django and upload it to the user?

I'm trying to make website that can print the name of user in greeting cards but i have issue

I'm trying to save the pics on my database but icant see it save on my database as you see my code in the bottom

this my views.py

from django.shortcuts import render
from .models import Name
from django.shortcuts import redirect
from PIL import Image,ImageDraw , ImageFont

# Create your views here.
def Home(request):
        name_input = str(request.POST.get('user_name'))

        img = Image.open("C:\\Users\\kmm\\Desktop\\my_django_stuff\\Eid_G\\files\\covers\\mypic.png")

        d = ImageDraw.Draw(img)

        fnt = ImageFont.truetype('C:\\Users\\kmm\\Desktop\\fonts\\static\Cairo-Bold.ttf',40)

        message = name_input

        d.text((540,1020),message, font=fnt, fill=(237, 185, 117),anchor="ms")

        saved_i = img.save(name_input+'.png')
        save_in_model = Name(massenger_name=name_input,image=saved_i)
        save_in_model.save()

        return render(request , 'index.html')

and this my models.py

from django.db import models
# Create your models here.

class Name(models.Model):
    massenger_name = models.CharField(max_length=255,null=True,blank=True)
    action_time = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to='images/',blank=True,null=True)

    def __str__(self):
        return str(self.massenger_name)

and this my index.html

<!DOCTYPE html>
<html lang="en" dir="rtl">
  <head>
    <meta charset="utf-8">
    <title></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
    {% load static %}
    <link rel="stylesheet" href="{% static 'CSS/style.css' %}">
  </head>
  <body>
    <div id="form">
      <form class="row" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
          <div class="">
            <input type="textarea" class="form-control" placeholder="أكتب أسمك (مثلا/أخوكم عبدالله العتيبي)" id="text_name" name="user_name" required>
          </div>
          <div class="col-auto">
            <button type="submit" class="btn btn-primary mb-3" id="button">حمل الصورة</button>
          </div>
      </form>

      {{ Name.Image }}
    </div>

  </body>
</html>

You need to add filename to

image = models.ImageField(upload_to='images/',blank=True,null=True)

like this piece of code

def upload_file_name_path(instance, filename_ext):
    ext = filename_ext.split('.')[-1]
    return f'images/{URL_SAFE(instance.name)}-{randint(1000, 10000)}.{ext}'

class Name(models.Model):
    massenger_name = models.CharField(max_length=255,null=True,blank=True)
    action_time = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to=upload_file_name_path,blank=True,null=True)

    def __str__(self):
        return str(self.massenger_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM