The first example of DJANGO: A simple Content Management System (CMS)

Source: Internet
Author: User
Tags table definition

Diango is a web development framework of Python. The purpose of the framework is to provide convenience for development, although it may be difficult at the beginning of learning. The book being read is practical Django projects.

The
One example is a simple Content Management System CMS (Contents Management
System ). Django was originally developed on a news website to facilitate Content Management (News editing, publishing, and so on) of the website ). To put it simply, CMS should include permission management and pages
Edit and publish. To accomplish these tasks, you cannot do without the support of the database. The first example uses the SQLite database for simplicity.

Assume that Django is installed in C:/python26/lib/Site-packages/Django. First, you need to create a project for CMS, it may be named CMS, with Django script django-admin.py to complete this task. To the directory of the selected project, run the following command

Python C:/python26/lib/Site-packages/Django/bin/django-admin.py
Startproject
CMS

A subdirectory named CMS will be created for the project, which contains some basic files required by the project, such as _ init _. py, manage. py, settings. py, and URLs. py.

Run the manage. py script (Python manage. py
Runserver
),
You can start a simple web server and enter "http: // 127.0.0.1: 8000/" in the browser to connect to this Web
Server. (In fact, manage. py only calls the execute_manager () function of Django. Core. Management.
The settings of the project are passed to the function as parameters)

Settings. py saves the configuration information of the Project. URLs. py defines the uring between URLs and implementation code.

The database connection information is saved in settings. py (databases =
). Django engineering is a group of applications supported by Django. Some common application scenarios have been customized in Django. Which applications need to be installed in this release must also be installed in
Installed_apps = is defined in settings. py. By default, a project includes the following applications:
'Django. contrib. auth', # perform authentication
'Django. contrib. contenttypes ',
'Django. contrib. session ',
'Django. contrib. Sites ', # manage multiple web sites. The relationship between release items and sites
'Django. contrib. Messages ',

For CMS projects, two additional applications are required:
'Django. contrib. admin', # provides the Site Management Interface
'Django. contrib. flatpage ',

The flatpages application is used for simple pages.
Provides a data model, including the title, content, URL corresponding to the page, and so on. The corresponding database table is django_flagpage.

Python manage. py
Syncdb

The databases required by the project will be created. Specific implementation: Find the list of installed applications, find the data model (model. py) of each application, the data model has the corresponding database table definition; database creation
After that, Django finds and executes the initialization code for each application. For example, the initialization code of Django. contrib. Auth prompts you to create a user account, and inserts it into the database table.
Necessary initial data.

URLs. PY defines the URL matching mode, view (Python function, response to HTTP requests) corresponding to each mode, or an include, and reuse other URL configuration definitions to process the mode. The matching mode is defined by a regular expression. For example:
(R' ^ admin/', include (Admin. Site. URLs )),
(R'', include ('django. contrib. flatpages. urls'), # All RegEx that does not match the above match this pattern

The first mode is defined as http: // 127.0.0.1: 8000/admin/
When you connect to the Web server, a logon page appears. When you log on with a user account created during syncdb execution, the site management page is displayed. You can add users/groups, add sites, and add simple
Page: Django. contrib. Auth provides a data model for user and group operations. Django. contrib. sites provides a data model for site management.
Flatpages provides a data model for simple page management.

Click "add simple page" here, but the definition of the simple page is saved in the database (this is the data model
Type, or URLs. py), but what is the layout of such a simple page displayed in the browser? You also need to define a display template
(Template), 'django. contrib. flatpage' searches for the template file flatpages/default.html by default.

Repeat it. To display a page normally, you need to do two tasks: 1. define the URL matching mode (and the corresponding data model) in Py. 2. Define the corresponding display template.

1. (r'', include ('django. contrib. flatpages. URLs ') defines the data model for processing simple pages (the RegEx with the wildcard is used here)
2. Create the default.html file in the flatpagessubdirectory as the page display template.

Django
The template system tries to achieve two goals: 1) the template allows the minimum amount of code to avoid Program Logic mixing into the template; 2) it does not use a special markup language such as XML. Two machines are provided in the template file.
System, variables, and
Tags. Variable is a placeholder. The actual content is queried by the data model of the application from the database table and then filled in. Tag can do almost anything, and its actual effect depends on the specific
Tag. developers can define their own tags to complete specific tasks.

The final output of the template file is a common HTML file, which is provided to the browser. The template file can be defined.
Save in: 1) the directory specified by template_dirs in settings. py; 2) in the installed application, if any one contains the subdirectory templates/
. It is best to save the template file in a separate place from the Code to reflect the separation of Program Logic and page display.

The template file for an example is as follows (the variables in the template are in curly brackets ):

<HTML>
<Head>
<Title >{{ flatpage. Title }}</title>
</Head>

<Body>
<H1 >{{ flatpage. Title }}{Flatpage. Content }}
</Body>
</Html>

It is slightly different from MVC (Model-View-Controller). The Django mode is generally called MTV (model-template-view ).

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.