Django connects to MySQL database

Source: Internet
Author: User

1. Modifying settings.py Configuration Data Properties

DATABASES = {'default': {'ENGINE':'Django.db.backends.mysql','NAME':'Fruitdb','USER':'Root','PASSWORD':'Hellosandy',}}

After the modification, enter the terminal into the project directory to execute the Python manage.py shell command to start the interactive interface enter the code to verify that the database configuration is successful. No error is successful!

 from Import connection>>> cursor = connection.cursor ()

2. Create a Django app

The terminal enters the project directory to execute the Python manage.py startapp Register generated directory file as follows:

products/    __init__. py    models.py    tests.py    views.py

3. Writing Models

 from  django.db import   models  #   Create your models here.  class   Register (models. Model): Nickname  = models. Charfield (Max_length=30 = models. Charfield (Max_length=50 = models. Charfield (Max_length=30)  def  __unicode__   return  Span style= "color: #800000;" > " %s,%s,%s  " % (Self.nickname, Self.email, Self.password) 

4. model Installation (modify settings.py)

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Register',)

Use Python manage.py validate to check that the model's syntax and logic are correct.

Without errors, execute the Python manage.py syncdb to create the data table.

sandymatomacbook-pro:fruitproject sandy$ python manage.py validate0errors Foundsandymatomacbook-Pro: Fruitproject sandy$ 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 Register_register

Now you can see that your database has created several other tables in addition to Register_register, which are the tables that the Django management background requires.

Django connects to MySQL database

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.