Django connects to MySQL database, framework maps database model, saves SQL statements OH

Source: Internet
Author: User
Tags django server

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

Related Article

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.