Learn how to create a simple blog with django at the Django Girls Tutorial (https://tutorial.djangogirls.org/en/). This article was written by me as a notebook for the review. Here is a summary of the steps and minimum operations for creating a django project.
Environment: Windows 10 + Visual Studio Code
Create a new working folder and move it.
terminal
> mkdir djangogirls
> cd djangogirls
Build and apply a virtual environment.
terminal
> python -m venv myvenv
> myvenv/Scripts/activate
Install django with pip. (Tutorial django version is 2.2.4)
terminal
(myvenv)> python -m pip install --upgrade pip
(myvenv)> pip install django==2.2.4
Create a new project mysite. The project name is arbitrary.
terminal
(myvenv)> django-admin startproject mysite .
Change the values of the following items in setting.py on mysite.
mysite/setting.py
# 1.Set time zone
TIME_ZONE = 'Asia/Tokyo'
# 2.Set language
LANGUAGE_CODE = 'ja'
# 3.Set static file location
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# 4.Set host address
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
# 5.Database settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Create a new app called blog on mysite.
terminal
(myvenv)> python manage.py startapp blog
Notify the project that you have added the app.
mysite/setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig', #Added line
]
Add the Post model to your blog app.
blog/models.py
from django.conf import settings
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
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
Notify the DB that you have added the model. This operation is only necessary when creating a model.
terminal
(myvenv)> python manage.py makemigrations blog
[Migrate] the model (https://wa3.i-3-i.info/word17035.html). Do this if the model configuration changes in the future.
terminal
(myvenv)> python manage.py migrate blog
Django has a feature called Django Admin A management screen that cannot be seen unless you log in with administrator privileges is automatically created for each project.
First, register a user with administrator privileges.
terminal
(myvenv)> python manage.py createsuperuser
Follow the instructions to register the information.
terminal
Username: ola
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
After that, you can log in to the management screen as a registered user.
Make the created Post model visible on the management screen.
blog/admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
terminal
(myvenv)> python manage.py runserver
Once started, you can access the admin screen, for example, from http://127.0.0.1:8000/admin/.
Please refer to here for deployment. It describes how to publish a small service using python anywhere for free.
In Second half, set the URL and view. (making)
Recommended Posts