Python Django site management configuration MySQL Database

Source: Internet
Author: User

Run command mysql-uroot-p into MySQL

Create a new database MyDatabase

Edit the settings.py file in the/mysite/mysite directory:

The default database is SQLite:

DATABASES = {'      default ': {          ' ENGINE ': ' Django.db.backends.sqlite3 ',          ' NAME ': ' MyDatabase ',      }  }  

MySQL Database configuration:

DATABASES = {'      default ': '          ENGINE ': ' Django.db.backends.mysql ',          ' NAME ': ' MyDatabase ',          ' USER ': ' Root ' ',          ' PASSWORD ': ' 123 ',          ' HOST ': ' 127.0.0.1 ',          ' PORT ': ' 3306 ',      }  }  

Start Django:python manage.py runserver

Running in the new shell: Python manage.py runserver

From django.db Import Connection

cursor = Connection.cursor ()

No error indicates configuration complete

Create a new app to run the command:

Python manage.py startapp MyApp

Go in. MyApp directory edit models.py file

#-*-Coding:utf-8-*-from __future__ import unicode_literalsfrom django.db import models# Create your models Here.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) #default = ' CN ' country = models. Charfield (max_length=50) website = models. Urlfield () def __unicode__ (self): return Self.nameclass Author (models. Model): First_Name = models. Charfield (max_length=30) last_name = models. Charfield (max_length=40) email = models. Emailfield (blank=true) def __unicode__ (self): return Self.last_nameclass book (models. Model): title = models. Charfield (max_length=100) authors = models. Manytomanyfield (Author) publisher = models. ForeignKey (Publisher) publication_date = models.            Datefield () def __unicode__ (self):    Return Self.title 

Edit the settings.py file in the/mysite/mysite directory to add the app

Installed_apps = [    ' django.contrib.admin ',    ' Django.contrib.auth ',    ' django.contrib.contenttypes ',    ' django.contrib.sessions ',    ' django.contrib.messages ',    ' django.contrib.staticfiles ',    ' MyApp ',

Run the following two commands to create the table in turn

Python manage.py makemigrations
Python manage.py Migrate

To create a site user:

Python manage.py Createsuperuser

Start Django:pyhon manage.py runserver 0.0.0.0:9999

This allows you to log on to the site on your browser http://192.168.177.130:9999/admin

Enter the user and password you just created to enter for management:

Editing the admin.py file in the/mysite/myapp directory will appear in the site page:

#-*-Coding:utf-8-*-from __future__ import unicode_literalsfrom django.contrib import adminfrom myapp.models import Pub lisher,author,book# Register Your models here.class bookadmin (admin. Modeladmin):        search_fields = (' title ',) #查询        filter_horizontal = (' authors ',) #横向查询选择        #filter_vertical = (' Authors ',) portrait        List_display = (' title ', ' publisher ', ' Publication_date ') #显示其他信息功能        list_filter = (' title ', ' Publisher ', ' Publication_date ') #右边分类功能        ordering = ('-publication_date ',) #其他信息的排序功能        #fields = (' title ') Sort feature Admin.site.register (Publisher) admin.site.register (Author) admin.site.register (book,bookadmin)

You can add table data on the page

is a variety of features added in book

Python Django site management configuration MySQL 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.