We will proceed on the premise of a Mac environment.
Check the Python version as follows.
$ python3 --version
Python 3.5.2
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
myapp
Directory 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
The database is the place to store the data used by your application. You usually configure the database for each web application.
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.
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.
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.
Hopefully, the following screen will be displayed on the browser.
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.
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