Deployment of Django Module

Source: Internet
Author: User
Tags django web server

In Django, settings related to the project Global must be added in the configuration file settings. py. I use MySQL as the background database and have created a database named django_news in MySQL. You need to set DATABASE_ENGINE = "mysql" and DATABASE_NAME = "django_news" in the corresponding location in the settings. py file ".

Note that if you use the SQLite database, Django can automatically create a new database in SQLite based on the database name, while in MySQL, PostgreSQL, or other databases, you must first create a database with the specified name. When using a MySQL database, you need to install an additional Python Link Library MySQLdb-1.2.1 for MySQL, which can be found at the site.

The following two items are DATABASE_USER and DATABASE_PASSWORD. You need to enter the user name and password for accessing the database based on the local settings. If the database is installed on another machine or the listening port of the database is changed, you must set the DATABASE_HOST address and DATABASE_PORT. The Mysql database used by the author is set:

 
 
  1. DATABASE_USER = 'django'   
  2. DATABASE_PASSWORD = 'django_password'  

To enable Django to identify the application modules added by developers, You need to define the list of applications loaded by the Django project in the INSTALLED_APPS section of the settings. py file. By default, some built-in modules required for running the Django project have been added to the list. We also need to compile the application module news. add article le and add Django that comes with django. contrib. admin application module. The modified code is as follows:

Add required modules

 
 
  1. INSTALLED_APPS = (   
  2.  'django.contrib.auth',   
  3.  'django.contrib.contenttypes',   
  4.  'django.contrib.sessions',   
  5.  'django.contrib.sites',   
  6.  'django.contrib.admin',   
  7.  'news.article',   
  8. )  

After the admin module is added, you cannot use the admin background management interface of Django immediately. You need to open urls under the root directory of the News project. in The py file, remove the # comment next to "# Uncomment this for admin:", so that Django redirects the URL for the management interface to "(r & apos; ^ admin/& apos ;, include (& apos; django. contrib. admin. urls & apos;), "available, so that Django can smoothly parse the access address and switch to the background management interface when accessing the admin module.

After the configuration file is changed, run the manage. py syncdb command at the command prompt of the News project. Django automatically maps the ORM database based on the definition of the model, blocking the preparation of the underlying database details and SQL queries.

The time has come to show the charm of Django. The Django framework will give developers a magical experience. With the rolling prompt after command execution, Django has automatically created corresponding tables and fields in the database based on the modeling file we just defined in models. When executing the command, you will be prompted to create a "superuser" account to log on to the background management page automatically created by Django to manage the model. Command Prompt 2 for synchronously updating database tables during command execution is shown below:

 
Synchronously update the database table during Django Command Execution

The best way to maintain learning motivation is to find a little sense of accomplishment at any time. Let's take a look at the work completed by these steps. Run manage again. py runserver to start the built-in Django Web server, access the address http: // 127.0.0.1: 8000/admin/in the browser, and log on with the account and password of the superuser user you just created, 3. The beautiful Django background management interface is displayed.

 
Django background management interface automatically generated based on model

In the admin management interface, the models modules defined in the application are displayed. When you click View, the list of database objects in models is displayed. The background management interface provided by Django allows you to directly change or Add database fields. Next, click "Add" next to "Lists" to Add a new news category. In the title field, type "Sports News" or another category you like and save it. Click "Add" in the "Items" Item, and fill in the first Item of the news. Each Item corresponds to a category Item in the List, as shown in interface 4 for adding Items, because association between tables is set, the Django Item management interface automatically generates content for the added List category drop-down option.


Page for adding news entries

The convenient backend management interface of Django saves a lot of time for Web developers. Currently, only the default backend management method of Django is used. For more information about Django module deployment, developers can refer to the user manual provided by Django to further customize and personalize the background.

Edit recommendations]

  1. Django overview and model definition overview
  2. Install and configure the Django Development Environment Eclipse + Pydev)
  3. Python framework Django 1.0 will be released in September 2
  4. Has Django reached its peak?
  5. Solve the problem that Eclipse cannot debug the configuration of the agent program

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.