(Django) First time teaching how to use a frontend framework.
This is the tutorial for Yuntech OSC.
English version, but no any future plang to translate to zh-TW.lol
Feel free to corect anything through PM.
Also we have a Gitbook version at here.
Installation
Pipe is a external import manage system.
This sesction will tell you how to install pipe into your ubuntu linux.
~$ sudo apt-get install python3-pip
install pip in python3
~$ sudo apt-get install python3-venv
venv should contain in python3 package.If not, just apt one for yourself!
~$ mkdir ~/django-tutorial
~$ mkdir ./django-tutorial/django-venv
Create a app folder for django in your home folder
Also another folder for installing virtual environment which named venv in app folder.
~/django-tutorial$ python3 -m venv django-venv
After that, create your venv in django-venv folder.
~/django-tutorial$ . ./django-venv/bin/active
and than active your venv.
(django-venv) ~/django-tutorial$
If you see there is a pair of parentheses contains your venv folder’s name,
It means you are in it!
(django-venv) ~/django-tutorial$ pip install django
Which pip will handle all installation of django without root permission.
Be advised that you must install every python package you need in venv.
Or it might cause any other unknown interference by other python script.
Examination
(django-venv) ~/django-tutorial$ python
open python interpreter
>>> import django
>>> django.VERSION
import django and print it’s version
(1, 11, 1, ‘final’, 0)
this shows we installed 1.11.1 version’s django.
MVC & MTV Framework
MVC
Model / View / Controller
We don’t talk too much, just give you some concept.
Model hook with Database or any other data storage, it’s provide an interface to access with data(s).
View defined how a page elements shows.
and Controller responsible for receive parameter and send out after processed.
MTV
Model / Template / View
Compare with MVC framework, MTV is more detailed define other stuff which Controller responsible for.
All you have to know is: Template = View, View + Urls = Controller
Once again, be advised that the following step should be operated under virtual environment.
First Project and Application
Structure
Command : django-admin.py startproject (ProjectName)
(django-venv) ~/django-tutorial$ django-admin.py startproject Opensource
after this command, django will create a folder named by your (ProjectName).
(django-venv) ~/django-tutorial$ cd Opensource
Tree Directory will shows:
1 | Opensource/ (Project Folder) |
manage.py
指令 | 說明 |
---|---|
django-admin.py startproject <project_name> |
建立 Django 專案 |
python manage.py -h <command_name> |
查看 Django commands 的使用方法 |
python manage.py runserver | 啟動開發伺服器 |
python manage.py startapp <app_name> |
新增 Django app |
settings.py
Edit settings.py to host a appropreate django
1 | import os |
this shows content of now_time variable.
**if statement **
1 | {% if (condition) %} |
For loop statement and render
1 | {% for each in staff %} |
Form
Django has a form system to replace traditional HTML’s form
Before making a form for a application, you need to create a forms.py first.
1 | ~$ cd Opensource/opensource_XX |
And put following code into it.
1 | # forms.py |
That’s all!
Example for a Login form.
This is a bunch of HTML code for a basic login form.
1 | <form> |
The form has two field which is TextField and PasswordField.
every time when you design a form, you have to analysis it field structure, and go to django’s official form field document and seek a proper field.
This is the website: https://docs.djangoproject.com/en/1.11/ref/forms/fields/
In this case. We only need CharField.
Which is for reflect HTML’s <input> label.
Also, by using <input> label’s type element, we can control it’s display form like normal text, or a password character (*).
When editing forms.py, you do like this:
1 | from django import forms |
and this is all about creating your first form.
But still, not using it!.
Usage in views.py
Every form is a variable in forms.py
Before use it in views.py, make sure you have already import it.
1 | from opensource_XX import forms |
After this, you can call any form variable in views.py to render in tempalte.
For example:
views.py
1 | from opensource_XX import forms |
login.html
1 | <html> |
After put variable into template html file.
Remember put submit button inside form like:
1 | <input type="submit" value="Submit"/> |
Also, the method you submit a form effect the way how django view function process the form result.
1 | <form method="POST"> |
Verify a form’s data
You can get a request method by calling:
1 | request.method |
It will return a string like ‘GET’ or ‘POST’
Put it into a if statement for a further process, like:
1 | if request.method == 'GET': |
While you want to process a form, you might get some value from a form.
Once you make sure this request contains a form and it’s definition in forms.py.
1 | if request.method == 'POST': |
By using the following method, you can easily get value in specific field in a form.
1 | form.cleaned_data['filed_name'] |
For example:
1 | Getting account field value: |
Finally, you can verify datas
1 | def login(request): |
Other stuff you might interesting.
If you are using some specil filed like GerneralIPAddressField in django.
You can simplly use:
1 | form.is_valid() |
It can help you to check some element like “required=True” or IP format … so on.
Advance template
Why we need base for templates?
By using template, we can make pages with same nature bind to one single file to define it.
Maxinumize the flexibilty of whole system.
But sometime we may want to merge pages with same feature.
And the only way to reach this purpose is to extend another page.
Make your first base.html
Base is also a template file.
In YOUR application folder’s ‘templates’ folder:
1 | ~$ cd Opensource/opensource_XX/templates |
This gives you another new html file called base.html
And put follow code into it.
If you wish to add any code you want, just do so:
1 | <html> |
Structure of this base html allowed pages who extends it can:
(1) Have its own title, and default is ‘YunNet OSC’
(2) Ability to edit meta by different page/
(3) Have its own CSS style or JaveScript.
(4) Also provide a body top/bottom section to put javascript whom need to be load in body.
Extends a base is very easy.
Just add one single line on top.
1 | {% extends 'base.html' %} |
So, just simply open another html file called ‘new_index.html’
1 | {% extends 'base.html' %} |
Of course, remeber to add a new define in views.py and routing in urls.py to make sure this page is correctly loaded.
models
With django default ORM, we can easily control database’s data, without traditional way to access it.
More effective, safty, and easy.
Different application can have it own model define data field.
1 | # System/models.py |
And this is a basic model in database a table named ‘system_test’ with only single field which is ‘name’
also, this field defaultly has a primary key called ‘pk’, the field has only one paremeter which is max_length.
For further model filed to use. you can check the following url:
https://docs.djangoproject.com/en/2.0/ref/models/fields/
Every time you edit a models.py always comes with a pair of command to execute.
1 | # python manage.py makemigrations |
ORM helps developer to construce entire database, before system do the reall change of it.
Developer should use makemigrations to commit to the ORM, let ORM organize every table/field whom may effect by this change.
Once the ORM checked the migrations is valid. Developer can us migrate to confirm the operation,
and let ORM do the migrate job.
Be advice, this doesn’t mean after makemigrations, ORM can 100% migrate changes.
Especially some operation involve with ForiegnKey.