Django
An open-source Web application framework, written by Python. The MVC Software Design pattern is used, i.e. model M, view V and Controller C. It was originally developed to manage some of the news content-based websites of the Lawrence Publishing Group. and was released in July 2005 under the BSD license.
1, the installation of Python Django (This step, the installation process self-completion, the installation of a lot of steps), the installation of different versions of the resulting file directory is also different, here is 1.6.5 version of Django;
[Email protected] ~]# django-admin.py--version1.6.5
2. Create a Django Project
[[email protected] python]# django-admin.py startproject mysite2[[email protected] python]# CD Mysite2/[[email protected ] mysite2]# lltotal 8-rwxr-xr-x. 1 root root 9 June 22:20 Manage.pydrwxr-xr-x. 2 root root 4096 June 9 22:20 Mysite2
The above is 1.6.5 builds a manage.py and a directory mysite2, in the case of 1.3.7 version, the following format is generated:
[Email protected] python]# CD Mysite2/[[email protected] mysite2]# lltotal 16-rw-r--r--. 1 root root 0 June 10:19 __init__.py-rw-r--r--. 1 root root 503 June 10:19 Manage.py-rw-r--r--. 1 root root 5203 June 10:19 Settings.py-rw-r--r--. 1 root root 568 June 10:19 urls.py
3, here we still use 1.6.5 version as an example to illustrate the problem, if we need to connect to the database, then we need to modify the Mysite2 folder settings.py file;
DATABASES = {' default ': {' ENGINE ': ' Django.db.backends.mysql ', # need to connect to what database ' NAME ': ' Mysite2 ', # required Database name to connect ' USERNAME ': ' Root ', # Connection database username ' PASSWORD ': ' Linux ', # Connect database Password ' port ': ' 3306 ', # port }} We can also change the time zone and the language used Language_code = ' en-zh ' # chinese time_zone = ' Asia/shanghai ' # Asia, Shanghai
4, after you can start Django, switch to manage.py directory files, such as not write 0.0.0.0:8000 (listen to all addresses), which is the default is to listen to the 127.0.0.1:8000 socket;
[email protected] mysite2]# python manage.py runserver 0.0.0.0:8000
5, then can be accessed on the page, with the local address plus port can be accessed:
Http://10.17.1.151:8000/
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/44/wKioL1V37bySBlcSAAFK4H3r4GY139.jpg "title=" Qq20150610113219.jpg "alt=" Wkiol1v37bysblcsaafk4h3r4gy139.jpg "/>
################ below is a simple setup step for creating a new app ############
1, use manage.py to create an app;
[email protected] mysite2]# python manage.py startapp myapp[[email protected] mysite2]# lltotal 12-rwxr-xr-x. 1 root root 9 June 22:20 Manage.pydrwxr-xr-x. 2 root root 4096 June 01:47 Myappdrwxr-xr-x. 2 root root 4096 June 9 23:29 Mysite2
2. To make Django aware of the existence of a new application, you also need to add an entry to the Installed_apps in the settings.py file.
Installed_apps = (' Django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.c Ontrib.sessions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' MyApp ', add new app)
3, switch to MyApp, create a model, that is, a new class in the moduls.py file, do a class to represent a table in the corresponding database;
[[Email protected] myapp]# vim models.pyfrom django.db import modelsclass teacher (models. Model): name = models. Charfield (max_length=30) gender = models. Charfield (max_length=5) age = models. Charfield (max_length=5) job = models. Charfield (MAX_LENGTH=50) def __unicode__ (self): return "%s, %s, %s, %s" % (self.name, self.gender, Self.age, self.job) Class student (models. Model): name = models. Charfield (max_length=30) gender = models. Charfield (max_length=5) age = models. Charfield (max_length=5) course = models. Charfield (max_length=50) def __unicode__ (self): return "%s, %s, %s, %s" % (Self.name, self.gender, self.age, self.course)
# # After creating the required tables, you can synchronize the database with the following command:
[[email protected] mysite2]# python manage.py syncdbcreating tables Creating table django_admin_logcreating table auth_permissioncreating table auth _group_permissionscreating table auth_groupcreating table auth_user_groupscreating Table auth_user_user_permissionscreating table auth_usercreating table django_content _typecreating table django_sessioncreating table myapp_teacheryou just installed django ' S auth system, which means you don ' t have any superusers defined. would you like to create one now? (yes/no): yesUsername (leave blank to use ' root ': # admin user account email address: [email protected]password: # This setting is login admin Yes login password password (again): superuser created Successfully.INSTALLING CUSTOM SQL&NBSP, ..... INSTALLING INDEXES&NBSP, ..... Installed 0 object (s) from 0 fixture (s)
# # Log in to the MySQL database to see the resulting table:
mariadb [(None)]> use mysite2;database changedmariadb [mysite2]> show tables;+----------------------------+| tables_in_mysite2 |+----------------------------+| auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_session | | myapp_student |# The following two are the tables we need to create student and teacher| myapp_teacher |+----------------------------+11 rows in set (0.00 sec ) mariadb [mysite2]> desc myapp_student;+--------+-------------+------+-----+---------+------ ----------+| field | type | null | Key | Default | Extra |+--------+-------------+------+-----+---------+----------------+| id | int (one) | NO | PRI | NULL | auto_increment | | name | varchar ( | NO | | NULL ) | | | gender | varchar (5) | no | | NULL | | | age | varchar (5) | NO | | NULL | | | course | varchar ( | NO | | ) null | |+--------+-------------+------+-----+---------+----------------+5 rows in set (0.10 sec) mariadb [mysite2]> desc myapp_teacher;+---- ----+-------------+------+-----+---------+----------------+| field | type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------- --------+| id | int (one) | NO | pri | null | auto_increment | | name | varchar () | NO | | null | | | gender | varchar (5) | no | | null | | | age | varchar (5) | NO | | NULL | | | job | varchar ( | NO | ) | NULL | |+--------+-------------+------+-----+---------+----------------+5 rows in set (0.00 SEC)
4, here we can add, delete, modify the information in the table through the Django Admin admin interface, that is, add a class to the admin.py in the MyApp directory:
[[email protected] myapp]# vim admin.pyfrom Myapp.models import student, teacherclass teacheradmin (admin. Modeladmin): list_display = ( ' Name ', ' gender ', ' age ', ' job ', ) class Studentadmin (admin. Modeladmin): list_display = ( ' Name ', ' gender ', ' age ', ' Course ', ) Admin.site.register (student, studentadmin) admin.site.register (teacher, teacheradmin)
After this save, you can enter http://10.17.1.151:8000/admin in the address bar to visit, you can directly manipulate the database table here.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/48/wKiom1V37R_DvAdBAAEkpLVSCdw452.jpg "title=" Qq20150610153845.jpg "alt=" wkiom1v37r_dvadbaaekplvscdw452.jpg "/>650" this.width=650; "src=" http://s3.51cto.com /wyfs02/m01/6e/44/wkiol1v37xmc_tggaajcpreueh4566.jpg "title=" qq20150610155418.jpg "alt=" wKioL1V37xmC_ Tggaajcpreueh4566.jpg "/>650) this.width=650;" Src= "http://s3.51cto.com/wyfs02/M02/6E/44/ Wkiol1v37yzrew6xaafzuwqtdx4261.jpg "title=" qq20150610155441.jpg "alt=" Wkiol1v37yzrew6xaafzuwqtdx4261.jpg "/>
This article is from the "boiled frog with boiling water" blog, please make sure to keep this source http://tanxw.blog.51cto.com/4309543/1660453
Steps to build a Python Django Web project (i)