python framework django basic guide
- 2020-05-10 18:23:58
- OfStack
Django introduction:
Django is an open source Web application framework written by Python. The framework mode of MVC is adopted, namely model M, view V and controller C. However, in the actual use of Django, Django is more concerned with models (Model), templates (Template), and views (Views), known as MTV mode. The main purpose of Django is to make it easy and fast to develop a database-driven web site. It emphasizes code reuse, and multiple components can easily serve the entire framework in the form of "plug-ins". Django has many powerful 3rd party plug-ins.
django is object-relational mapping (ORM, object-relational mapping) : define your data model in the form of the Python class. ORM connects the model to a relational database. You can manipulate the database through simple API, and you can use the original SQL statement in Django. Django can run on Apache or on servers that support WSGI, FastCGI. Support multiple databases, Postgresql,MySql, Sqlite3,Oracle are already supported.
django installation
pip install Django
Verify the installation of django
import django
django.get_version()
Create an django project
django-admin.py startproject mysite
At this point, some directories and files will be automatically generated. manage.py, the outermost layer, is like a running entry. It can complete some common functions through the command line call, such as:
Run django's native web server:
python manage.py runserver http://127.0.0.1:8080
Commonly used to synchronize or create database tables:
python manage.py syncdb
Create a subproject inside django project
python manage.py startapp polls
Create super administrator:
python manage.py createsuperuser
There is also the setttings.py file, which is the configuration file for django.
The urls.py file, which is the django file used to match url, is defined here for which url to execute which background code (view).
Django MTV model --modle model
Django adopts the orm schema (object relational mapping). The django model defines one python class according to the contents of the database table. The members of this class correspond to the fields 11 in each database table.
The member type in the class is also corresponding to the field type in the database table, and the name can also be a kind of 1, it looks more intuitive. So each instance of the class represents one piece of data in the database.
Model examples (as defined in models.py) :
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)d
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
django setup database:
database in the settings.py file can define the type of database you want to use, such as:
Define the database as sqlite
'ENGINE': 'django.db.backends.sqlite3'
Define the database as mysql
'ENGINE': 'django.db.backends.mysql'
django-admin.py startproject mysite, and then python manage.py runserver http://127.0.0.1:8080 even if the simplest django web server is set up, http://127.0.0.1:8080 can be accessed through http://127.0.0.1:8080. In addition, activate the application in install_apps, execute pyhton manage.py syncdb, and then create the corresponding database according to the defined model.
Example of database operation :(suppose File is the model class defined)
Get all data:
all_filelist = File.objects.all()
Get all the data and sort by a certain field:
all_filelist = File.objects.all().order_by('-id')
Execute sql statement:
cursor = connection . cursor()
cursor.extcute("select * from info_path")
chaannels = cursor.fetchall()
Filter data:
list=File.objects.all().filter(xxx=xxx)
Query by primary key
list=File.objects.all().get(id=1)
Time filtering:
results = File.objects.all().filter(time__range=(dayfrom, dayto))
Create new data:
file = File(time=time,path=path,result=result)
file.save()
Data acquisition:
file.time
file.path