Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]

Composition of commentary articles

No. title
1 Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
2 Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
3 Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
4 Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
5 Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
6 Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]

Development environment

We will proceed on the premise of a Mac environment.

Check the Python version as follows.

$ python3 --version
Python 3.5.2

Project creation

Let's check the directory structure easily as a review of the last time.

Check directory location and directory structure


$ pwd
/Users/#{myname}/Desktop/sample_app/myapp
$ tree -L 1 .
.
├── bin
├── include
├── lib
└── pyvenv.cfg

Now let's create a Django project. By the way, let's proceed while checking the changes in the directory structure.

Project creation


$ django-admin startproject myapp .
$ ls
bin   include   lib   manage.py   myapp   pyvenv.cfg
$ tree -L 1 .
.
├── bin
├── include
├── lib
├── manage.py
├── myapp
└── pyvenv.cfg

myappDirectory has been added. You can also see that `manage.py``` has also been added. By the way, `.``` Is not a typo, but represents the directory at the current location (current directory).

manage.py is a script for site management.


 This allows you to run your web server on your computer without having to prepare any other software.
 Similar to the rails command in Ruby on Rails.

 Also check the contents of the created ``` myapp```.


#### **`Check the directory contents of myapp`**
```bash

$ tree -L 1 myapp/
myapp/
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

myapp/urls.py is a configuration file that associates files with URLs.


 Similar to Ruby on Rails ``` config / routes.rb```.


#### **`myapp/settings.py is the application's configuration file.`**
```py is the application's configuration file.

 Similar to Ruby on Rails ``` config / application.rb```.

# Change time zone

 First, edit this ``` myapp / settings.py```.
 Find the following three in `` `myapp / settings.py``` and change them.


#### **`Change time zone to Japan`**
```bash

LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
USE_TZ = False  #Changed to False by Default because of UTC timezone

Database (DB)

The database is the place to store the data used by your application. You usually configure the database for each web application.

Database software type

You can write the data to be saved in a text file such as Notepad, but it is convenient and software for saving the database is available for free. I think the following three are famous database software.

MySQL and PostgreSQL are fast and practical, so it is recommended to use either one when building a production environment (pointing to the server accessed by the user). However, it requires database software settings separately from Django, which may be a little difficult for beginners. Therefore, we will proceed with application creation using SQLite, which requires almost no settings.

If you have a small application (for example, about 100 users), SQLite can handle it with plenty of room, so this time there is no problem at all. As you become more accustomed to creating web applications and become more aware of performance improvements, consider MySQL and PostgreSQL.

Database settings

If you use SQLite, the settings are already described in `` `myapp / settings.py```.

myapp/settings.py


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Now, let's migrate the DB. You may not be familiar with the word migration, but there is a file that summarizes the commands for DB setting, and it is an image that executes it at once by migration.

Migrate DB


$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK

You have now created a database. If you work on a web framework, it's very easy to set up a single database. Normally, it is difficult to just type in SQL commands for database settings (which are relatively long and difficult to read), create a database, create a table, create a column, create an index, etc.

You have to understand it from the detailed structure inside! I'm sure some of you say that, but I personally like the learning style of learning from the outer frame and getting to know the details as you get used to it, so this time I will use these convenient functions. To go.

Start the server

After creating the project and setting the DB, it is very simple, but one Web application is completed.

First, let's start the server with the following command,

Start the server


$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
November 09, 2016 - 03:04:37
Django version 1.8, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Did the server start successfully like this? All you have to do is enter python manage.py runserver. Similar to Ruby on Rails rails server.

Since we haven't specified an IP address or port number this time, you can check the operation of the Web application by accessing the following URL that is used as standard in Django. http://127.0.0.1:8000/

Now, enter this URL in your browser to access it. スクリーンショット 2016-11-09 3.08.06.png

Hopefully, the following screen will be displayed on the browser. スクリーンショット 2016-11-09 3.08.19.png

At a minimum, you've created a web application with Django.

From the next time onward, we will edit this project and finish it as an original web application.

Next commentary article

Explanation of Python Web application (Django) in an easy-to-understand manner even for beginners (3) [Application creation / DB setting]

References

bonus

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

Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
Django tutorial summary for beginners by beginners ① (project creation ~)
Web application creation with Django
[For beginners] Summary of standard input in Python (with explanation)
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
[Python] Web application design for machine learning
WebApi creation with Python (CRUD creation) For beginners
[For beginners] Try web scraping with Python
Beginners use Python for web scraping (4) -3 GCE VM instance creation and scraping on VM
Explanation of creating an application for displaying images and drawing with Python
WEB application development using Django [Admin screen creation]
Easy understanding of Python for & arrays (for super beginners)
[Python] Beginners troubleshoot while studying Django web applications
Basic story of inheritance in Python (for beginners)
Beginners can use Python for web scraping (1) Improved version
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Web application made with Python3.4 + Django (Part.1 Environment construction)
Explanation of NoReverseMatch error in "python django super introduction"
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell
python textbook for beginners
Application of Python 3 vars
OpenCV for Python beginners
[For beginners] Basics of Python explained by Java Gold Part 2
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
[Python] Introduction to graph creation using coronavirus data [For beginners]
(For beginners) Try creating a simple web API with Django
[For beginners] Basics of Python explained by Java Gold Part 1
Learning flow for Python beginners
Python3 environment construction (for beginners)
Comparison of 4 Python web frameworks
Overview of Docker (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Web application with Python + Flask ② ③
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Web application with Python + Flask ④
Development of WEB application using Django [Add data from management screen]
If you know Python, you can make a web application with Django
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
Python x Flask x PyTorch Easy construction of number recognition web application
Python techniques for those who want to get rid of beginners
Automatic creation of 2021 monthly calendar (refill for personal organizer) by Python