Create a new Django project:
Such as
Add Project directory File: test_db, (if the file does not exist, a new folder will be created in that directory)
Set app Name: TestDB, open the project in a new window,
wherein, in the MVC pattern, models.py is used to process m,views.py for processing v,urls.py for mapping views.
1. Download Pymysql:pip Install Pymysql
2. Add (not) in Project __init__.py:
Import Pymysql
Pymysql.install_as_mysqldb ()
:
Fields that define database tables in models.py:
From django.db import Models
Class Publisher (models. Model):
Name = models. Charfield (max_length=30)
Address = models. Charfield (MAX_LENGTH=50)
City = models. Charfield (max_length=60)
State_province = models. Charfield (max_length=30)
Country = models. Charfield (MAX_LENGTH=50)
Website = models. Urlfield ()
def __unicode__ (self):
Return Self.name
Create a database in MySQL beforehand, such as: book, and then modify setting.py in the following:
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' Book ',
' USER ': ' Root ',
' PASSWORD ': ' lyf123 ',
' HOST ': ' 127.0.0.1 ',
' POST ': ' 3306 ',
# ' ENGINE ': ' Django.db.backends.sqlite3 ',
# ' NAME ': Os.path.join (Base_dir, ' db.sqlite3 '),
}
}
After the above two steps, and then through the command to view the models.py corresponding SQL statement, the command is as follows: Python manage.py sqlall TestDB (project name) (if it appears: App ' TestDB ' has migrations error, Just delete the migrations file) as shown in:
Then, by ordering Python manage.py syncdb to synchronize the database, create the table with the results such as:
Start MySQL and enter the corresponding command to see if the table and the corresponding fields are established.
To add view processing in views.py:
From. Models Import Publisher
From django.shortcuts import Render_to_response
def book_list (Request):
names = Publisher.objects.all ()
Return Render_to_response (' book_list.html ', {' names ': Names})
To add a URL mapping in urls.py:
URL (r ' ^book/', views.book_list),
Add two HTML files to the templates:
#base. html
<! DOCTYPE html>
<meta charset= "UTF-8";
<title>
{% block title%} {% Endblock%}
</title>
<body>
{ % block content%}{% Endblock%}
{% block footer%}
<p>thanks for visiting my site.</p>
{ % endblock%}
</body>
#book_list. html
{% extends ' base.html '%}
{% block title%}title:book_lib{% Endblock%}
{% block content%}
<tr><th>name</th><th>address</th><th>city</th<th>country</th ><th>website</th></tr>
{% for M in names%}
<tr>
<td > {m.name}}
<td > {m.address}} </td>
<td > {m.city}} </td>
<td>{{m.country}}</td>
<td>{{M.website}}</td>
</tr>
{% ENDFOR%}
{% Endblock%}
Run the Django server with the following results:
PS: The data in the table is randomly written in the MySQL command window for testing
Summary: This example is a straightforward, simple record of the Django framework principle, which does not involve adding, deleting, changing, or checking the database.
Django connects to MySQL database, framework maps database model, saves SQL statements OH