I'm just studying Django from Udemy and playing with it, so I'm improving the TODO app of Udemy's Django course and the in-house SNS, so I think that the level is the same. [Thorough commentary! ] Master the basics of Django and create 3 apps!
Therefore, I am doing it with the idea of making something that moves completely out of sight, such as practicality. I think there are many points that are difficult to understand in the first article, but I would appreciate it if you could watch over with warm eyes.
When registering image data in Django, it was very troublesome to type in the data one by one, select the image, and so on, so I was wondering if there was any good way.
$ python manage.py runscript <file name>
I learned that python files can be executed with, so I decided to use this.
I won't go into details about runscript, but this will execute the run method of any file in the scripts directory in the directory where manage.py is located.
Since I created a file called add_dummies.py here, add_dummies.py will be included in
Since it was troublesome to select an image, I decided to generate an image by referring to the following article. Create a dummy image with Python + PIL.
The code I actually used to generate it is below. The only place I arranged is that the color and size of the image are changed with random numbers so that the image can be identified at a glance.
add_dummyies.py
from PIL import Image,ImageDraw, ImageFont
import random
colors = [(255,0,0), (0,255,0), (0,0,255),(0,0,0),(255,255,255)]#List of red, green, blue, black and white colors
def make_image(N):
"""
Create image file
The return value is the name of the file
"""
screen = (200+random.randint(0,100), 200+random.randint(0,100))
pen_color=random.choice(colors)
bg_color=random.choice(colors)
img = Image.new('RGB', screen, bg_color)
x, y = img.size
u = x - 1
v = y - 1
draw = ImageDraw.Draw(img)
draw.line((0, 0, u, 0), pen_color)
draw.line((0, 0, u, v), pen_color)
draw.line((0, 0, 0, v), pen_color)
draw.line((u, 0, 0, v), pen_color)
draw.line((u, 0, u, v), pen_color)
draw.line((0, v, u, v), pen_color)
filename="/sample_{}.jpg ".format(N)
img.save("media"+filename)
return filename
This will generate any n image files and save the images in the location ./media/<image file>
as seen from the directory where manage.py resides.
In addition, the model of this app is defined as follows.
models.py
from django.db import models
class RecommendedBook(models.Model):
author = models.CharField(max_length=100)
bookTitle = models.CharField(max_length=50)
content = models.TextField()
bookImage=models.ImageField(upload_to="")
genre = models.CharField(max_length=200, null=True, blank=True, default="")
good = models.IntegerField(null=True, blank=True,default=0)
goodtext = models.CharField(max_length=500,null=True,blank=True, default="")
notGood = models.IntegerField(null=True, blank=True,default=0)
notGoodtext = models.CharField(max_length=500,null=True,blank=True, default="")
I used the Faker function because I wanted to save other elements as well, but I will omit that because it is not the subject of this time. Add the following run method to add_dummies.py to execute it. Now that we have to add other elements as well:
add_dummies.py
from book.models import RecommendedBook
import random,string
from faker import Faker
fakegen=Faker("ja_JP")
genres=["Technical book","English","Math","novel","Impressed","pleasant"]#Set the genre to an appropriate list and randomly retrieve it later
fakegen = Faker()
def run():
N=5#Create about 5 data
for entry in range(N):
imgfile=make_image(entry)
bookGenre=random.choice(genres)+" "+random.choice(genres)#Set about two genres
dummy=RecommendedBook(author=fakegen.name(), bookTitle=fakegen.word(),
content=fakegen.text(), genre=bookGenre,
bookImage=imgfile
)
dummy.save()
When you run it and look into the management screen
The new data was saved successfully!
That's it! ~~ I did some research on my own, but I couldn't find a good method, so it turned out to be such a muddy method. ~~ I would like to introduce the upward compatibility of this article below. Create a dummy image file in Python without a dummy image file in Django and test the image upload
Also, since it is relatively slow to register data because it is rotated in a for loop, it does not matter much if the number of loops is small, but if it increases to a certain extent, it will take a considerable amount of time. I thought it was a problem. I would be grateful if you could teach me, "There is a way to make it easier!" "This will make it faster!" For the time being, I will put the github page where the code of this article is located.
Thank you for reading until the end!
Recommended Posts