[PYTHON] Django Crispy Tutorial (Environment Building on Mac)

Introduction

F67A61D9-134B-471B-867B-BAF163845843.jpeg

Hello. This is Sugimoto.

I wrote this article for you, "I want to make a website with Python." "I read the net and reference books about Django, but I don't understand."

I am writing for beginners and beginners, so I will not explain it in depth. please note that.

Also, the following assumptions are made.

  • Python3 or above is installed.
  • I know a few Linux commands.
  • Contains text editors like VS Code and Atom.
  • I feel like running Django for the time being.

This time, I will create a simple blog application with Django.
Then, Let's Django!

Table of contents
Step No. Contents
1 Virtual environment construction / setting
2 Create project (folder)
3 Corresponds to Japan time and Japanese
4 Display Django page
5 What's next?

Virtual environment construction / setting

In this item, you will create a virtual environment and install Django in that virtual environment. (Roughly speaking, a virtual environment is a "pseudo OS". We will create a web application on this pseudo OS.)

First, create a directory on your desktop for this tutorial. The directory name can be anything, but this time it's "django_work". 作業フォルダ.png

I will enter this immediately.
Open a terminal in Finder → Applications → Utilities → Terminal.

Did you see a black screen? When it comes up, enter the following command into the django_work directory.

Terminal


$ cd Desktop/django_work

Let's create a virtual environment here! Enter the following command in the terminal.

Terminal


$ python3 -m venv django_venv

Then you should have a directory called django_venv in your django_work directory. This is a virtual environment.

This completes the construction of the virtual environment. It was easy!

This is just "creating a virtual environment". You have to start the virtual environment.

Continue to type the following command while still in the django_work directory. Start the virtual environment.

Terminal


$ source django_venv/bin/activate

The virtual environment is now started. It was easy again!

Finally, install Django. Enter and execute the following commands line by line.

Terminal


(django_venv) $ pip install --upgrade pip

Terminal


(django_venv) $ pip install django==2.2.6

You've now installed Django in your virtual environment. By the way, the version of Django is 2.2.6.

This completes the construction and settings of the virtual environment.

Create project (directory)

project is a folder that contains configuration files and applications to be created in the future.
The configuration file is automatically generated by typing the following command.

Enter the following command while the virtual environment is still running. The project name is "sakusaku_blog".

Terminal


(django_venv) $ django-admin startproject sakusaku_blog

The project is now created. If you look in the django_work directory on your desktop, you'll see that a directory called "sakusaku_blog" has been created. Finally, in

, type the following command in the terminal to enter the project.

Terminal


(django_venv) $ cd sakusaku_blog

The project (directory) has been created. Please let the terminal wait for a while in this state. It will be used in the item "Displaying the Django page" that follows.

Corresponds to Japan time and Japanese

When you run the django-admin startproject command earlier, the Django configuration file is automatically generated. The name of the configuration file is "settings.py". Rewrite the relevant part in this settings.py to correspond to Japanese and Japan time.

Open the sakusaku_blog directory in the django_work directory with a text editor (VSCode, Atom, etc.). Then you will find the "settings.py" file in the sakusaku_blog directory. Look at the contents of this file.

I think there is a code "LANGUAGE_CODE ='en-us'" around line 106.
You can set the language to Japanese by rewriting this "'en-us'" part to "'ja-JP'".
Also, if you rewrite the value of the code "TIME_ZONE ='UTC'" to "'Asia / Tokyo'" around the 108th line, you can set it to Japan time. 設定1.png 設定2.png

Now you can change to Japanese and Japan time!

That's all for making Japan time and Japanese compatible.

Display Django page

Start the server and finally display the Django page!

Please open the terminal. You should be in sakusaku_blog now.

Type the following command in the terminal to start the server.

Terminal


(django_venv) $ python3 manage.py runserver

The server has started! Try accessing http: // localhost: 8000 !
You should see the Django default page. Djangoページ.png

Congratulations! Now it's sunny and Django's environment construction is complete!

From the next time, we will create an application! looking forward to!

What's next?

Thank you for reading this far.

I hope you find it surprisingly easy to launch Django.

I'm currently writing "How to create an application" and "Django crispy tutorial (Environment construction on Windows)", so please wait for a while.

(I'd be happy if you could press the "Like" button at the end)

Let's study Django together ٩ ('ω') و

Recommended Posts