[Django] introduces a python framework: Django installation and server Construction

Source: Internet
Author: User
Tags gz file django website install django

Django is a web development framework that can make web development work pleasant and efficient.

Using Django enables you to build and maintain high-quality web applications at a low cost.

It is somewhat similar to PhP codeigniter and uses the MVC design pattern.


The basic installation steps are as follows:


1. Install Python (note version)

Django is written in Python only, so the first step to installing the framework is to ensure that you have installed python. It is best to select the Django installation file and then find the corresponding version. I installed python33 before and later installed python27 due to version issues. Otherwise, Django will not be installed successfully. The latest version requires Python version 2.6.5 or later. However, things are not better and better than new things. python2.x should be more popular.


2. Install the official release

The officially released version has a version number, such as 1.0.3 or 1.1,

The new version can always be found at http://www.djangoproject.com/download.

On Windows systems, we recommend that you use uncompressed software to uncompress the .tar.gz file. After decompression, start a DOS shell (command prompt) with the administrator privilege ), then execute the following command in the directory whose name starts with Django (you can press the Shift key and right-click to directly call the command prompt at the current position ):

Python setup. py install


3. test whether the installation is successful.

The final task is to test whether Django is successfully installed and works well.

Open the python interpreter and enter the following code. If the installation is successful, you can import the Django module:
>>> Import Django

>>> Django. Version

(1, 1, 0, final', 1)


Next we will create a simple helloworld project to familiarize ourselves with the basic environment of Django.

Create a directory helloworld, and I put it on disk D. Path: D: \ helloworld
If you have a background in PHP programming, you may be used to placing code in the root directory of the web server's documents (such as/var/www ). In Django, it is not a good idea to put any Python code together with the root of the web server document. This makes it possible for people to see your original code on the Internet.


We can place the code in some directories outside the root directory of the document.
Go to the directory you created and run the command Django-Admin. py startproject mysite.

This will create a directory named mysite under your current directory:


Note:
If you use the setup. py tool to install Django, Django-Admin. py should have been added to the system path.

If you are prompted that Django-Admin. py is unavailable, you need to add both Python and Django-Admin. py to the system path.

The method is as follows:

My computer-> properties-> advanced system settings-> environment variables-> System Variables

Add your own Python path and scripts PATH at the beginning of the PATH variable. The specific content varies from person to person. Be careful.

Taking me as an example, I directly installed python2.7 on drive C, so I added the following content directly before the PATH variable:

C: \ python27; C: \ python27 \ scripts;


In this way, this simple mysite is complete.

Click here to see the site created by Django for us. We can find a folder named mysite and a file named manage. py. Click the folder to view the mysite folder. Several Python files are found in the folder, including several important files:

_ Init _. py: Let Python treat this directory as a development kit (a group of modules ). This is an empty file. You do not need to modify it.
Settings. py: Set or configure the Django project. View and understand the available setting types and their default values in this file.
URLs. py: Configure the Django project URL. It can be viewed as the directory of your Django website. Currently, it is empty. Similar to the Controller in the CI framework, it is used to process URL redirection.

Although these files are small, they already constitute a runable Django application.


The next step is to run the server.

Unlike JSP and PHP, developing Django basically does not require professional servers such as Apache or tomcat.

The Django Development Service is a built-in, lightweight Web service that is available during development.

This server is used to develop sites quickly. That is to say, you do not need to configure a product-level Web Server (such as APACHE) before preparing to release the product. Unlike Apache, this development server monitors your code and automatically loads it, so that you can easily modify the code without restarting the service.


Switch to your project directory (the directory with the manage. py file) and run the following command:
Python manage. py runserver


You will see something like this


This will start a local server on port 8000 and can only be connected and accessed from your PC. Now that the server is running, use a web browser to access http: // 127.0.0.1: 8000 /. You should be able to see a pleasing pale blue Django welcome page. It started to work.


Although this web server provided by Django is very convenient for development, do not use it in the formal application deployment environment. At the same time, the server can only process a single request reliably without any type of security audit.

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.