Part 1 https://qiita.com/TuruMaru/items/4df41577fb9f722c7864
I'm going to create a database, but to do that, I first need to write the contents to the table in models.py
.
posts/models.py
from django.db import models
#Import to use timezone
from django.utils import timezone
# Create your models here.
#models.It is a rule to write a model
class Posts(models.Model):
#Meta and object are not magic
class Meta(object):
#Specify the name of the table to be created
db_table = 'posts'
#Column name=Data format(The name displayed on the management screen,Other constraints)
text = models.CharField(verbose_name='Text', max_length=255)
created_at = models.DateField(verbose_name='Created date', default=timezone.now)
#Set to be displayed on the management screen(Magic)
def __str__(self):
return self.text, self.created_at
Now you are ready to create a table called posts. Based on these, we will operate in the terminal.
$ python manage.py makemigrations posts
Migrations for 'posts':
posts/migrations/0001_initial.py
- Create model Posts
You have now created the 0001_initial.py
file.
This file contains the contents of the database to be created.
However, this command has not yet created the database. I'm ready.
$ python manage.py sqlmigrate posts 0001
BEGIN;
--
-- Create model Posts
--
CREATE TABLE "posts" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(255) NOT NULL, "created_at" date NOT NULL);
COMMIT;
You can see the SQL code when the table is created with this command.
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
Applying posts.0001_initial... OK
Yes, this is done. Thank you very much.
A little operation is required to check on the management screen, but I will omit it here.
If you do not pass the path, the code you wrote will not be displayed in the browser. There are other settings as well, so let's do it first.
mysite/setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Additional part from here
'posts',
'templates',
]
After creating the application, add it to ʻINSTALLED_APPS`.
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#↓ Add one line here
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Tell python that you created the templates directory.
Also, I will write a little views.py and html to check the operation.
posts/views.py
from django.shortcuts import render
# Create your views here.
from django.views.generic import View
class IndexView(View):
def get(self,request, *args, **kwargs):
return render(request, 'posts/post.html')
index = IndexView.as_view()
templates/base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block page_title %}{% endblock %}</title>
</head>
<body>
<hr>
<h1>{% block title %}{% endblock %}</h1>
<hr>
{% block content%}{% endblock %}
<hr>
</body>
</html>
templates/posts/post.html
{% extends "base.html" %}
{% block page_title %}post{% endblock %}
{% block title %}post{% endblock %}
{% block content %}
<h1>Posts</h1>
{% endblock %}
Usually, base.html
is used as the base of all pages, and post.html
and others fill in the contents.
Next, I will finally write ʻurls.py`.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('posts.urls')),
]
posts/urls.py
from django.urls import path
from . import views
app_name = 'posts'
urlpatterns = [
path('', views.index, name='index'),
]
If you make a call like this, start a local server and check it.
$python manage.py runserver 3000
Then type localhost: 3000
on Google or something.
If it comes out like this, the pass relationship is complete. Let's write the code of the bulletin board application from the next.
Recommended Posts