Continued from template addition
The function called posts that we are currently creating is based on the image of a function for inputting articles such as blogs, so a database that stores titles, articles, dates, and images is required. I will add this to models.py.
models.py
from django.db import models
#We will define it as a class called Post.
class Post(models.Model):
#Definition for title, use CharField for string type
#max_It is possible to limit the number of characters by length
title = models.CharField(max_length=100)
#Define date and time, use DateTimeField for date data
published = models.DateTimeField()
#Definition of image data, use ImageField for image data
#You can specify the save destination of the image data as an argument (this time, specify the media folder)
image = models.ImageField(upload_to='media/')
#TextField is used for definition of sentences and text type
#Used for long sentences
body = models.TextField()
Next, execute the command to create the database table based on models.py.
$ python3.6 manage.py makemigrations
Migrations for 'posts':
posts/migrations/0001_initial.py
- Create model Post
If there is a new definition file, it will create a file to put into the database.
Then read the newly created file and create a table.
$ python3.6 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
Applying posts.0001_initial... OK
Access the database and check if the table exists.
$ sqlite3 db.sqlite3
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .tables
auth_group django_admin_log
auth_group_permissions django_content_type
auth_permission django_migrations
auth_user django_session
auth_user_groups posts_post #Newly created table
auth_user_user_permissions
You can see that a table called "posts_post" has been created.
Recommended Posts