Python Django tutorial tutorial

This is a tutorial for Python Django Tutorial.

django Study python through the framework, By the way, I aim to be able to create web applications.

Tutorial Summary

Somehow, while writing this article, I found an official tutorial and explanation page, I will redevelop the wheels without worrying about it. http://eiry.bitbucket.org/tutorial/index.html

This page explains how to create a python environment and how to proceed with the tutorial.

I thought, but since there was already a nice document on how to create a python environment I will introduce and supplement it.

For windows → http://eiry.bitbucket.org/win/tutorial/installation.html For mac → http://eiry.bitbucket.org/mac/tutorial/installation.html

By the way, my environment is as follows.

OS:Mac OSX10.11 python:3.4.2 django:1.8.5

The above page explains how to install python3.4 and virtualenv. In addition to this, it is recommended to install virtualenvwrapper as well.

As the name wrapper suggests, virtualenv is a little easier to use.

$ mkdir -p ~/PycharmProjects/practice #Prepare directory for virtual environment $ cd ~/PycharmProjects/practice/ #Move to the prepared directory $ virtualenv --python="which python3.4" virtualenv #Creating a virtual environment $ source virtualenv/bin/activate #activation

 > When a series of operations uses virtualenvwrapper

>> ```bash
$ mkvirtualenv practice  #Create virtual environment
$ workon practive  #activation. (No need to run after mkvirtualenv)

This is all you need. It takes some time to pass the path and set environment variables, but there is no loss at last.

What is path? There was a nice article on qiita, so please refer to it. "What is it through PATH?" "Understanding the setting of environment variables to pass PATH (Mac OS X)"

About source code

The source code of the tutorial is on github. It is difficult to write code while watching the tutorial from 0, so please take advantage of it.

Github storage https://github.com/usa-mimi/tutorial

It's okay to use it on the command line, but it's hard to get used to We recommend using a GUI tool such as SourceTree.

Some people have written about how to use it, so I will introduce it. How to use Source Tree SourceTree Setting Procedure Memorandum [Windows] Git starting from SourceTree

I will explain a little about the operation of SourceTree.

Clone from github

The location of the source code is called the repository. The first thing to do is to go to the repository on github Bring it to your PC.

This operation is called "clone remote source locally" or "clone remote to local".

In SourceTree, select "Clone from URL" on the first screen.

Screenshot 2015-11-17 22.40.53.png

Please open the github page in your browser and copy the clone URL. There is a link on the right side of the screen, and clicking the icon will copy the link to the clipboard. \ # You can copy this directly → https://github.com/usa-mimi/tutorial.git

Screenshot 2015-11-17 22.43.11.png

Open the SourceTree screen again, paste the URL into the source URL, and Select the save location and press the clone button to clone the source code locally.

Kobito.t0Cg2q.png

Try to move the cloned code

If you can clone it normally, the file should be expanded in the above storage location with the following configuration.

tutorial/  #Directory created when cloned with git (directory specified by SourceTree)
    ├ .git/  #Since it is a hidden file, it cannot be seen depending on the settings.
    ├ .gitignore  #Same as above
    ├ ...
    └ tutorial/  #The explanation will be explained here as root unless otherwise noted.
          ├ manage.py  #Specify when executing django commands
          ├ requirements.txt  #List of libraries to install with pip
          ├ db.sqlite3 #It is created by executing the migrate command. It does not exist immediately after cloning.
          ├ ...
          └ tutorial/
                ├ ...
                ├ urls.py  #project URL
                └ settings.py  #project configuration file

Install the required python libraries

Create a virtual environment with an appropriate name at the terminal (prompt) and create a second tutorial directory. Move to (the directory where manage.py is located) and execute the following command.

(tutorial) $ pip install -r requirements.txt

By the way, the content of requirements.txt is the output result of the $ pip freeze command.

Create database

Execute the migrate command to create a db. A database file for sqlite called db.sqlite3 is created in the same hierarchy as manage.py.

(tutorial) $ ./manage.py migrate

Run

You can check the operation by accessing http: // localhost: 8000 with a browser after executing the following command. \ # The contents of this area will be explained in the tutorial.

(tutorial) $ ./manage.py runserver

View the source code

There are two branches, master and develop. You can check the commit of the master branch anywhere with runserver. The develop branch commits the edits in detail, so it may not work in some places.

スクリーンショット 2015-11-17 23.25.43.png

A list of commits is displayed in ①. There are two rows of circles on the left side, the left side is the master branch and the right side is the develop branch.

In ②, a list of changed files from the previous commit is displayed.

The difference from the previous commit "on the same branch"

○ ┐ #I'm in a commit here right now │ ● #Not the difference here │ ● │ ● │ ● ○ ┤ #Difference from here │ ●


 ③ is the change contents of the file selected in ②.
 The added lines are displayed in green, and the deleted lines are displayed in red.

## Source code switching

 If you double-click the appropriate line in the part ①, `HEAD` will move there.
 In the tutorial, start from which commit (tag) like `Source: 7f5128a → 3efdc15`
 It describes which commit (tag) was finally made.

 If you want to start the tutorial from the middle, please switch the source code referring to this hash value (or tag).

 Don't forget to run `$ ./manage.py migrate` after switching.
 \ # `db.sqlite3` If you execute it after deleting the file, the entered data will be deleted.

 If you want to undo after trying out the source code
 Type `$ git reset --hard HEAD` in the terminal to get it back.

 > All changes will disappear.
 If you want to keep the edited contents, please commit after cutting an appropriate branch.

# For those who use python2 system

 django works fine with both python2 and python3, but it requires a bit of tedious writing when using Japanese.
 The writing style may change depending on how the character string is handled, so please use python3 as much as possible.

 Since you can select python to use when creating a virtual environment with virtualenv, it is good to specify python3 at that time.
 For example, my PC has both python2 and 3, and if I run it with just `python`, python2.7.9 is used.
 However, specifying `python3` when creating a virtual environment will change the version of python that is executed.

```bash
$ python2 -V
Python 2.7.9

$ python3 -V
Python 3.4.2

$ python -V  #When I run python, python2 runs
Python 2.7.9

$ mkvirtualenv tutorial -p python3  #Create a tutorial environment by specifying python3
...
(tutorial)$ python -V  #When I run python, python3 runs
Python 3.4.2

What to do if a character code error occurs in python2

$ ./manage.py 
... #Stack trace
SyntaxError: Non-ASCII character '\xe8' in file .../tutorial/polls/models.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

If you get an error like this when you execute it, it is a character code related error.

If you want to use Japanese in the file, add the following line to the beginning of the file. # -*- coding: utf-8 -*-

For those who write the source with notepad of windows etc., the character code is shift-jis # -*- coding: sjis -*- Please write.

Also, when assigning Japanese characters to variables or using them as arguments, add ʻu` to indicate that the character string is Unicode.

s = 'Japanese characters`  #OK with python3, error with python2

s = u'Japanese characters`  # OK

f(u'argument')  # argumentに渡す文字も同様

Tutorial Summary

Recommended Posts

Python Django Tutorial (5)
Python Django Tutorial (2)
Python Django Tutorial (8)
Python Django Tutorial (6)
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
Python Django tutorial summary
Python tutorial
Python Django Tutorial Cheat Sheet
Python tutorial summary
django tutorial memo
Start Django Tutorial 1
Django 1.11 started with Python3.6
[Docker] Tutorial (Python + php)
Django python web framework
django oscar simple tutorial
Django Python shift table
Try Debian + Python 3.4 + django1.7 ...
[Personal notes] Python, Django
Python OpenCV tutorial memo
[Python tutorial] Data structure
Cloud Run tutorial (python)
Python Django CSS reflected
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Introduction to Python Django (2) Win
[Python tutorial] Control structure tool
Python
Do Django with CodeStar (Python3.8, Django2.1.15)
Create ToDo List [Python Django]
Getting Started with Python Django (1)
Get started with Django! ~ Tutorial ④ ~
Django
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Get started with Django! ~ Tutorial ⑥ ~
Install Python 3.7 and Django 3.0 (CentOS)
[Python] Decision Tree Personal Tutorial
GAE + python + Django addictive story
Getting Started with Python Django (6)
Getting Started with Python Django (5)
Until Python [Django] de Web service is released [Tutorial, Part 1]
EEG analysis in Python: Python MNE tutorial
8 Frequently Used Commands in Python Django
Python practice_Virtual environment setup ~ Django installation
Create new application use python, django
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Run python3 Django1.9 with mod_wsgi (deploy)
Django Girls Tutorial Summary First Half
Stumble when doing the django 1.7 tutorial
Deploy the Django tutorial to IIS ①
Install Python framework django using pip
Introduction to Python Django (2) Mac Edition
[Python Tutorial] An Easy Introduction to Python
Learning history for participating in team app development in Python ~ Django Tutorial 5 ~
Learning history for participating in team app development in Python ~ Django Tutorial 4 ~
Learning history for participating in team app development in Python ~ Django Tutorial 1, 2, 3 ~