Write the first Django app note (installation configuration)

Source: Internet
Author: User
Tags install django

Writing your first Django app
Write the first Django app note

This note was written a long time ago. I have referenced many Django documents and official documents and hope it will be helpful to viewers!

I. installation and configuration
1. Download and install Django from the official website.
2. Decompress Django and go to the Django folder under shell (CMD ).
3. Run Python setup. py install. Before running, make sure that python is in the environment variable.
4. After installation, set the site-packages/Django/bin directory to the environment variable.
At this point, Django has been configured

2. Create a project and an app
1. Go to a directory and run the django-admin.py startproject mysite under shell
The django-admin.py is under the environment variable (Django/bin directory) set above, mysite is
The name of the project you created. At this time, Django will create the mysite folder for you.
There are the following files:
_ Init _. py manage. py settings. py URLs. py
These four files are used to represent projects, manage projects, configure Django run, and parse URLs respectively.

2. Go to the mysite directory under shell and run manage. py runserver. Some prompts are displayed
CTRL-C (in Windows) to end server running

3. Open the browser and enter http: // 127.0.0.1: 8000. Some Django prompts will appear:
It worked!
Congratulations on your first Django-powered page.
If no error occurs, the default port number is 8000. You can modify the port number as follows:
Manage. py runserver Port
 
This tutorial provides an important warning: do not publish this manage. py as a server !!

4. Run the manage. py Startapp appname command to create an app.
A folder that contains the following files:
_ Init _. py models. py, views. py
_ Init _. py is an empty file, indicating that the file in this directory is a whole module.
The models. py file defines various data table classes.
Views. py is not clear yet

5. the classes defined in the models file are tables in the database (the table name consists of the app names with lower-case characters.
Class Name That is named + _ + lowercase characters), this database needs to be set in settings. py,
Starting with an uppercase database, set the engine, database name, port number, host address, and data
The classes defined in the username and password models. py file for database login are processed using models. model as the parent class.
.
Each column in the table must be defined by * field, specifically:
Charfield (maxlength = 50) textfield ()
Integerfield datetimefield ()
Foreignkey () (used to associate two tables)

6. Compile the models Code The class structure cannot enter the database table, in settings. py
Set installed_apps, add the installed app, and then run the following method:
Manage. py syncdb
In this case, syncdb performs a Class search based on the configurations in installed_apps.
Class to create a database table. After creating a table, you will be prompted to create a superuser. This superuser is
Django superuser, non-system (root permission in Linux, Administrator permission in Windows)
Then you can use admin to log on to the webpage.
Note: syncdb may use update to update database tables or insert to insert tables.
Confirm code

7. Of course there are still some problems with logging in directly with Admin. Three things need to be done:
A. Modify the settings. py file and add 'django. contrib. admin' to installed_app ',
B. Run manage. py syncdb to create a database file and add the django_admin_log database table.
C. Remove the # uncomment this for admin in URLs. py: Comment symbol of the following line.
Enter htpp: // 127.0.0.1: 8000 (the port number is based on the running status) in the browser.
Current logon page

8. To control and modify the admin subclass defined in the class in the models. py file, superuser can
After logging on to the system, you can add or delete instances of this class.

9. log on to Python using manage. py shell in the console.
Add the django_setting_modules environment variable and add project. app (use the specific project name and
In the environment variable, use the from... Import Statement to load the related module class.
Operation.

Get all objects: poll. Objects. All ()
Object filtering (query): poll. Objects. Filter (question _ startswith ('wh '))
# Question indicates the data member in the class
Get objects based on parameters: poll. Objects. Get (pub_date _ year = 2005)
Poll. Objects. Get (ID = 1) <==> poll. Objects. Get (PK = 1)
After the returned object is stored, you can use the method --> P = poll. Objects. Get (ID = 1) of the class where the object is located)
C = P. choice_set.create (choice = 'not much', votes = 0) # note the Writing Method
Enter the associated object: C. Poll
P. choice_set.all () # Enter join to get all objects of choice
P. choice_set.count () # Enter join to obtain the number of all choice objects
Delete one of the choice: c = P. choice_set.filter (choice _ startswith = 'just hacking ')
C. Delete ()


10. Console module management. Define an instance using this class. The returned class instance can directly access data elements or directly
Modify: Save () to save the modified content to the database. The database table has an automatically added
Id variable (controlled by the database), Django get through ID or PK (primary-key)

Note: When modifying a class in models, you need to re-run syncdb to update the database, and then run
Runserver: refresh the browser page to get the correct results. Otherwise, the classes and databases in Models
Errors may occur.


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.