Interaction between Django and databases

Source: Internet
Author: User
1. Generate Django framework: cmd-> CD into the file containing the django-admin.py execute the django-admin.py startproject myprj. This will generate four files in the folder: __init _. py, manage. py, settings. py, urls. py
2. Use python to generate a sqlite3 Database
Import sqlite3
Sqlite3.connect ('database/webblog. db') // Note: if the database file exists (in the python installation directory), The webblog is automatically generated if it does not exist.
3. Configure the database. Modify settings. py,
DATABASE_ENGINE = 'sqlite3 '# 'postgresql _ psycopg2', 'postgresql ', 'mysql', 'sqlite3 'or 'oracle '.
DATABASE_NAME = r "C: \ Python25 \ database \ webblog. db" # Or path to database file if using sqlite3.
DATABASE_USER = ''# Not used with sqlite3.
DATABASE_PASSWORD = ''# Not used with sqlite3.
DATABASE_HOST = ''# Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''# Set to empty string for default. Not used with sqlite3.
I am using sqlite3 as the background database, so here DATABASE_ENGINE should be sqlite3, DATABASE_NAME should be the absolute path of the database (relative path I have not tried, I do not know whether it can be ). Also, here, you need to set the database folder to everyone to write (in my previous blog post, we will show you how to modify this permission ).
4. run cmd and cd to include manage. py folder, for example: C :/.. (Omitted)...> cd c: \ python25 \ scripts \ webblog (Press ENTER), and then input python manage. py shell. Console programming >>>. Enter from django. db import connection. Continue entering cur = connection. cursor (). If no error message is displayed, your database is successfully configured.
5. Exit shell.> ctrl + z (^ z)
6. Create a new application. Enter python manage. py startapp webblogapp in the console. The webblogapp folder is generated in the current folder. Contains four files: _ init _. py, models. py, tests. py, and views. py.
7. Use models. py to define the model. Modify models. py and add the following code:
From django. db import models
From django. contrib import admin

# Create your models here.

_ List_per_page = 50
  
# In fact, the class defined here is equivalent to defining two tables. At last, we can see that it generates two tables: webblogapp_User and webblogapp_person.
# On how to define the model, it is recommended to see the http://docs.djangoproject.com/en/dev/topics/db/models/
Class User (models. Model ):
Username = models. CharField (max_length = 20)
Password = models. CharField (max_length = 20)
Nickname = models. CharField (max_length = 30)

Class person (models. Model ):
GENDER_CHOICES = (
(U 'M', u 'male '),
(U 'F', u 'female '),
)
Name = models. CharField (max_length = 60)
Gender = models. CharField (max_length = 2, choices = GENDER_CHOICES)
8. After defining the model (the table in the database), you need to install the model. In settings. py, modify the settings as follows:
INSTALLED_APPS = (
# 'Django. contrib. auth ',
# 'Django. contrib. contenttypes ',
# 'Django. contrib. session ',
# 'Django. contrib. sites ',
'Webblog. webblogapp', # webblog is the folder name containing webblogapp, and webblogapp is a file containing models. py
)
9. Run python manage. py sqlall webblogapp. Generate a table. We can see that two tables are generated: webblogapp_user and webblogapp_person.

10. Submit the SQL statement to the database. Run python manage. py syncdb. It can be seen that multiple tables are generated (I don't know what other tables are used ...)

11. Next, you will be asked to enter the user name, email address, password, and so on. Press the button and you will be OK.
12. Go to shell. Python manage. py shell.
>>> P = Person (name = "Fred Flinstone", gender = "M ")
>>> P. save () // insert a record in webblogapp_person
>>> P. gender
U'm'
>>> P. get_gender_display ()
U'male'

---- David Cai08-08-19 at Kingsoft

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.