Python network programming----Django's interaction with the database

Source: Internet
Author: User
Tags sqlite

IntroductionDjango provides a unified calling API for multiple database backgrounds, and with Django's help we don't have to write SQL statements directly. Django Converts a relational table (tables) into a class. Each record is an object under the Class (object). We can use object-based methods to manipulate relational databases.


setting up the databaseSetting up the database requires modificationsettings.pyFile if you are using a database that is MySQL: [Python]View Plaincopy
  1. databases = {  
  2.      ' default ' : {  
  3.           :   " Django.db.backends.mysql "   
  4.          ' NAME '  :  " Djangodb "   
  5.          ' HOST '  :  " localhost "   
  6.          ' USER '  :  " root "   
  7.           ' PASSWORD '  :  " 123456 "   #    
  8. }
  9. }
Port if not set, default port 3306 is used if MySQL is not installed, you can also use SQLite. SQLite is well suited for testing and can even be deployed without a lot of concurrent writes. Because SQLite uses the local file system as the storage medium and uses the native file system permission to do the access control, like the host, the port, the user, the password this kind of information does not need only to enter the following information: [Python]View Plaincopy
  1. DATABASES = {
  2.      ' default ' : {  
  3.           ' NAME ' : r ' C:\mysite\db\test.db ' ,                #db目录需要自己创建   
  4.          :  ' django.db.backends.sqlite3 ' ,  
  5. }
  6. }
Design ModelIn a traditional relational database, the data model is a table. Under Django, a table is a class. Each column of the table is a property of the class. In models.py, we create a table with only one column, that is, a class with only one property:
From django.db import Modelsclass Test (models. Model):                         #用于定义数据模型的类    name = models. Charfield (max_length=100)       #name列 with a data type of char and a length of    def __unicode__ (self):        return Self.name
Command the Django synchronization database. Django actually creates the various relational tables in MySQL based on the data model described in models.py: $python manage.py syncdb
Set the ViewInsert two data into the database beforehand
below we take the data out of the database and return it to the HTTP request. In blog/views.py, add a view. For the corresponding request, we will read all the records from the database and then return to the client:

#-*-Coding:utf-8-*-from django.http import httpresponsefrom blog.models import testdef outstr (Request):    blog_list = Test.objects.all ()    blog_str  = map (str, blog_list)    return HttpResponse ("<p>" + ". Join (BLOG_STR) + "</p>")

In order for the HTTP request to find the above program, add the URL link in blog/urls.py:
From Django.conf.urls import patterns, include, Urlurlpatterns = Patterns ("',    # Examples:    # URL (r ' ^$ ', ' Myweb.views.home ', name= ' home '),    url (r ' ^$ ', ' blog.views.outStr '),)

Enter Python manage.py runserver run server
Visit 127.0.0.1:8000/blog


Previous: Python network programming----first knowledge of DjangoNext Talk:


If you have any questions, welcome to my public question ~

Python network programming----Django's interaction with the database

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.