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.
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)"
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.
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.
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
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.
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
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.
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
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
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.
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
$ ./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に渡す文字も同様
Recommended Posts