[PYTHON] Django Girls Tutorial Summary First Half

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

1. Build a virtual environment and install django

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

2. Create a project

Create a new project mysite. The project name is arbitrary.

terminal


(myvenv)> django-admin startproject mysite .

3. Change the configuration file (setting.py)

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'),
    }
}

4. Add app

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
]

5. Add a model to your app

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

6. Register the administrator

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.

7. Make the model visible from the admin screen

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)

8. Start the server

terminal


(myvenv)> python manage.py runserver

Once started, you can access the admin screen, for example, from http://127.0.0.1:8000/admin/.

9. Deploy the project

Please refer to here for deployment. It describes how to publish a small service using python anywhere for free.

Latter half

In Second half, set the URL and view. (making)

Recommended Posts

Django Girls Tutorial Summary First Half
Python Django tutorial summary
Django Girls Tutorial Note
AtCoderBeginnerContest175 Review & Summary (first half)
AtCoderBeginnerContest169 Review & Summary (first half)
AtCoderBeginnerContest174 Review & Summary (first half)
AtCoderBeginnerContest173 Review & Summary (First Half)
AtCoderBeginnerContest165 Review & Summary (first half)
AtCoderBeginnerContest170 Review & Summary (first half)
AtCoderBeginnerContest167 Review & Summary (first half)
AtCoderBeginnerContest177 Review & Summary (first half)
AtCoderBeginnerContest168 Review & Summary (first half)
AtCoderBeginnerContest178 Review & Summary (first half)
AtCoderBeginnerContest171 Review & Summary (first half)
AtCoderBeginnerContest166 Review & Summary (first half)
AtCoderBeginnerContest161 Review & Summary (first half)
AtCoderBeginnerContest172 Review & Summary (first half)
AtCoderBeginnerContest176 Review & Summary (first half)
Django Summary
Django Summary
Django tutorial summary for beginners by beginners ③ (View)
Django tutorial summary for beginners by beginners ⑤ (test)
Python Django Tutorial (5)
Python Django Tutorial (2)
First Django Challenge
Python tutorial summary
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Django girls-3 workflow
Django filter summary
Python Django Tutorial (7)
Python Django Tutorial (1)
First Django development
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
Django tutorial summary for beginners by beginners ⑦ (Customize Admin)
Django tutorial summary for beginners by beginners ① (project creation ~)
Django tutorial summary for beginners by beginners ④ (Generic View)
Machine learning tutorial summary
Django Polymorphic Associations Tutorial
django oscar simple tutorial
Summary of stumbling blocks in Django for the first time
AtCoderBeginnerContest178 Review & Summary (second half)
Get started with Django! ~ Tutorial ⑤ ~
AtCoderBeginnerContest161 Review & Summary (second half)
AtCoderBeginnerContest164 Review & Summary (second half)
AtCoderBeginnerContest176 Review & Summary (second half)
Get started with Django! ~ Tutorial ④ ~
AtCoderBeginnerContest168 Review & Summary (second half)
Get started with Django! ~ Tutorial ⑥ ~
AtCoderBeginnerContest166 Review & Summary (second half)
AtCoderBeginnerContest171 Review & Summary (second half)
AtCoderBeginnerContest174 Review & Summary (second half)
Python Django Tutorial Cheat Sheet
AtCoderBeginnerContest173 Review & Summary (second half)
[Learning memo] Django command summary
AtCoderBeginnerContest177 Review & Summary (second half)