简体   繁体   English

我如何在 django 中存储我在枕头代码上编辑的图片并将其上传给用户?

[英]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这是我的意见.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这是我的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这是我的 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 的用户扩展模型中上传个人资料图片 - How can I upload profile picture in user extended model in Django 如何将图片上传到heroku上的django应用中并显示出来? - How can I upload a picture to my django app on heroku and get it to be displayed? 如何从网络摄像头捕获图片并将其存储在 Django 的 ImageField 中? - How can I capture a picture from webcam and store it in a ImageField in Django? 如何从我的 django 模板中正确调用将行插入 db 的类函数? - How can i call class function thats insert row into db properly from my django template? 我如何在 Python 代码(TKinter、Pillow、customtkinter)中解决这个问题 - How i can solve this problem in Python code (TKinter, Pillow, customtkinter) 如何在django网站页面上编辑内容? - How can i edit the content on my django website pages? 如何在Django中验证表单或视图,以便它们只能将用户模型编辑为属于该数据的用户模型? - How can I validate my forms or views in Django so that they can only edit the User Model only to those that belong to that data? 如何从网络摄像头捕获图片并将其存储在 Django 的 ImageField 或 FileField 中? - How can I capture a picture from webcam and store it in a ImageField or FileField in Django? 如何修复我的 Django model 以存储用户输入的多个字符串列表。 将来可以检索哪个 object? - How can I fix my Django model to store multiple lists of strings inputted by the user. Which can then be retrieved in the future as an object? 如何在Python枕头上加载图像? - How can I load an image on Python Pillow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM