About Django:
Django is an Open-source Web application framework written by Python. The MVC framework pattern was adopted, i.e. model m, View V and Controller C. But in Django's actual use, Django is more concerned with models, templates, and views, called MTV Mode. The main purpose of Django is to easily and quickly develop a Database-driven website that emphasizes code reuse and that multiple components can easily serve the entire framework in a "plug-in" form, and Django has many powerful third-party Plugins.
Django is an object-relational mapping (orm,object-relational mapping): Define your data model in Python class form, ORM connects the model to the relational database, you can manipulate the database through a simple api, You can also use the original SQL statements in Django. Django can run on apache, or it can run on a server that supports WSGI,FASTCGI. Support a variety of databases, has supported postgresql,mysql, Sqlite3,oracle.
Django Installation
Pip Install Django
Verifying the Django Installation
Import djangodjango.get_version ()
Create a Django Project
django-admin.py Startproject MySite
In this case, some directories and files are generated automatically, and the outermost manage.py, like a running portal, can be used to accomplish some common functions through command line invocation, such as:
Run Django's own web Server:
Python manage.py runserver http://127.0.0.1:8080
Common synchronization or creation of database Tables:
Python manage.py syncdb
Create a subproject within a Django project
Python manage.py Startapp Polls
To create a super administrator:
Python manage.py Createsuperuser
There is also the setttings.py file, which is a Django configuration File.
The urls.py file is the file that Django uses to match the url, and which URL executes which background code (view) is defined Here.
----modle model of Django MTV mode
Django uses ORM mode (object relational mapping), and the Django model defines a Python class based on the contents of the database table, and the members of this class correspond to field one by one in each of the database tables;
The type of the member in the class also corresponds to the field type in the database table, and the name can actually be the same as the one that looks more Intuitive. Thus each class instance represents a single piece of data in the Database.
Model Examples (defined in models.py):
from Import Models class Poll (models. Model): = models. Charfield (max_length=200) = Models. Datetimefield ('date published')class Choice (models. Model): = models. ForeignKey (Poll) d = Models. Charfield (max_length=200) = Models. Integerfield (default=0)
Django Settings database:
The database in the settings.py file can define the types of databases 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 '
Execute 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 built, The test can be accessed through http://127.0.0.1:8080. In addition, the Install_apps activates the application, executes the Pyhton manage.py syncdb, and the corresponding database is created according to the defined Model.
Database operation Example: (assuming file is a defined model Class)
Get all Data:
All_filelist = File.objects.all ()
Get all the data and sort by a field:
All_filelist = File.objects.all (). order_by ('-id')
Execute SQL Statement:
cursor = Connection. Cursor () cursor.extcute ("select * from Info_path"= Cursor.fetchall ()
Filter Data:
List=file.objects.all (). Filter (xxx=xxx)
Query based on PRIMARY key
List=file.objects.all (). Get (id=1)
Time Filter:
Results = File.objects.all (). filter (time__range= (dayfrom, dayto))
To create new Data:
File = File (time=time,path=path,result=result) file.save ()
Get Data:
File.timefile.path
Python Django Basics (i)