Python framework django Basic Guide, python framework django

Source: Internet
Author: User
Tags django web server install django pip install django

Python framework django Basic Guide, python framework django

Django introduction:

Django is an open-source Web application framework written in Python. It adopts the MVC framework model, namely Model M, view V and controller C. However, in actual use of Django, Django focuses more on models, templates, and Views, which are called the MTV mode. The main purpose of Django is to develop a database-driven website easily and quickly. It emphasizes code reuse. Multiple components can easily serve the entire framework in the form of plug-ins, django has many powerful third-party plug-ins.

Django is an object relational ing (ORM, object-relational mapping): defines your data model in the form of Python classes. ORM connects the model with the relational database, you can use simple APIs to operate databases, and you can also use the original SQL statements in Django. Django can run on Apache or on servers that support WSGI and FastCGI. Supports multiple databases, including Postgresql, MySql, Sqlite3, and Oracle.

Django Installation

pip install Django

Verify django Installation

import djangodjango.get_version()

Create a django Project

django-admin.py startproject mysite

At this time, some directories and files are automatically generated, and the manage. py at the outermost layer is like a running entry. Through the command line call, you can complete some common functions, such:

Running django's built-in web server:

python manage.py runserver http://127.0.0.1:8080

Common synchronization or database table creation:

python manage.py syncdb

Create a sub-project in the django project

python manage.py startapp polls

Create a super administrator:

python manage.py createsuperuser

There is also the setttings. py file, which is the django configuration file.

The urls. py file is a file that django uses to match urls, and the background code (view) executed by which url is defined here.

Django MTV model ---- modle Model

Django adopts the orm mode (Object relationship ing). The django model defines a python class based on the content of the database table. The members in this class correspond to the fields in each database table one by one;

The member type in the class also corresponds to the field type in the database table, and the name can also be the same, it looks more intuitive. In this way, each class instance represents a piece of data in the database.

Model example (defined in models. py ):

from django.db import modelsclass 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 settings database:

The database in the settings. py file can define the database type you want to use, for example:

Define the database as sqlite

'ENGINE': 'django.db.backends.sqlite3'

Define the database as mysql

'ENGINE': 'django.db.backends.mysql'

Execute the django-admin.py startproject mysite and then python manage. py runserver http: // 127.0.0.1: 8080 even if you build the simplest django web server, you can access and test it through http: // 127.0.0.1: 8080. In addition, activate the application in install_apps and execute pyhton manage. py syncdb. Then, the corresponding database will be created based on the defined model.

Database Operation example: (assume File is the defined model class)

Get all data:

all_filelist = File.objects.all()

Retrieve all data and sort by a field:

all_filelist = File.objects.all().order_by('-id')

Execute the 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 Filter:

results = File.objects.all().filter(time__range=(dayfrom, dayto))

Create new data:

file = File(time=time,path=path,result=result)file.save()

Get data:

file.timefile.path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.