We will proceed on the premise of a Mac environment.
Check the Python version as follows.
$ python3 --version
Python 3.5.2
This time, we'll start by creating a Django application.
Create an application in your project. Here, we will create a blog following the Django Girls Tutorial.
$ python3 manage.py startapp blog
The directory structure is as follows.
myvenv
├── bin
├── blog
├── db.sqlite3
├── include
├── lib
├── manage.py
├── myapp
└── pyvenv.cfg
After creating the application, first change the configuration file. This is to define the application to use on Django.
Find the code below and add `` `blog``` to the bottom.
myapp/settings.py
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
blog
To use inmodel
I will make.
model
Is it a convenient object that can store data?
You can have a method in this Model`` itself, and you can handle the communication with DB without worrying about SQL etc. thanks to this
Model```.
Now let's define Model
for `blog
.
blog/models.Open py and rewrite it as follows.
#### **`blog/models.py`**
```py
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
The explanation is a little long here, but the following is an excerpt from the explanation of Django Girls Tutorial only where necessary.
class Post (models.Model): --This line defines this model (this is the object).
- class is a keyword that indicates that you are defining an object.
Let's define the property: title、text、created_date、published_date、author First you have to decide on the type of field. text? Numbers? date? Relationships with other objects, such as users
It sounds like you're declaring the type of property you want to use in your model.
As an aside, in Ruby on Rails,
$ rails g model post author title text
Since the migration file was created by declaring like, it seems a little troublesome compared to that.
Now that we have defined the model, we will create a migration file.
$ python3 manage.py makemigrations blog
Migrations for 'blog':
0001_initial.py:
- Create model Post
This will allow Django to create a migration file for you to put in your database, so we'll do the migration.
$ python3 manage.py migrate blog
Operations to perform:
Apply all migrations: blog
Running migrations:
Rendering model states... DONE
Applying blog.0001_initial... OK
The `` `Post``` model is now successfully reflected in the DB (SQLite).
Django allows you to check the model with a GUI via a browser so that even beginners can easily understand it. This seems to be called Django admin.
First, open the blog / admin.py
file and modify Django admin for the Post model you just created.
blog/admin.py
from django.contrib import admin
from .models import Post
# Register your models here.
admin.site.register(Post)
This completes the settings.
Start your server and access Django admin (http://127.0.0.1:8000/admin) from your browser.
Server startup
$ python3 manage.py runserver
Hopefully you can access a web page like this.
However, the login screen is displayed, and you don't remember setting it, right?
That should be it, you need to create a separate superuser
(site administrator) to log in.
Now let's create `` `superuser```.
Creating superuser
$ python manage.py createsuperuser
Username (leave blank to use 'your_name'): admin
Email address: [email protected] #Set email address individually
Password: #Enter password
Password (again): #Password again
Superuser created successfully.
This completes the creation of `superuser`
.
Go to Django admin (http://127.0.0.1:8000/admin) again and try logging in.
Log in with the `` `superuser``` you created earlier.
If you can log in successfully, you can access such a screen.
The model name was declared as `Post```, but it is automatically pluralized as
`Posts```.
Let's try adding an article by adding Posts
on the screen.
After registering, a new article was created on the DB.
Unicode error
will occur, so it is recommended to use python3 series.
Since python2 series is very troublesome to handle Japanese, python3 series is useful even if these points are improved.This completes the DB confirmation. From now on, if you want to check the DB, ask Django admin to help you.
Now you can set the model, reflect it in the DB, and check it.
Next time, I will create an original template and go to the point where I can edit the View side.
We are waiting for you to follow us!
Service introduction Please feel free to contact us if you are interested in "Kikagaku", a one-on-one tutoring service for machine learning that allows you to learn "math → programming → web applications" all at once.
Recommended Posts