[英]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.