Install Django in Windows

Source: Internet
Author: User
Tags install django

Install Django and start Web Services in Windows

If you are using Linux or Mac OS X, the system may have pre-installed python. EnterPythonIf the python editing environment appears, it indicates that python has been installed. Even if python is not installed by default in Linux, installing Python and Django on Linux is simple. It is a little complicated in windows, and may involve environment variable configuration and other problems.

Because Django itself is written in pure Python code, the first step to install the Django framework is to install Python first. Django can run in any Python environment or even on a mobile phone! Currently, python2.7 is stable and compatible. I installed python2.7.3. Django of various versions can be downloaded on the official website (http://www.djangoproject.com/download/), installed in Linux or Unix systems is very simple, after the following steps basically on the line:

1). Tar xzvf Django-* .tar.gz(Decompress the Django package)

2) CD Django -*(Go to the decompressed directory)

3) sudo Python setup. py install(Execute setup. py for installation)

1. install it in the command line:

In Windows, you can place Django in the same directory as Python, enter the Django-* directory under DOS, and execute Python setup. PY install, which takes about 1 minute to complete the installation. Django will be installed in the 'site-package' directory of the python installation directory (C: \ python27 \ Lib \ Site-packages or/usr/lib/python2.7/Site-packages). Of course, some dependent files are also attached to the python-related class libraries, this system is automatically installed.

   2. Configure Environment Variables

To configure environment variables, manually add these directories to the system environment variables: C: \ python27; C:/python27/lib/Site-packages/Django; C: /python27/scripts. After adding it, you can use Django's django-admin.py command to create a new project.

3. Check whether the installation is successful.

If the above error occurs, it may be that the Python version is incompatible with the Django version or is not compatible with the computer support (such as 64-bit or 32-bit ). Then, you can check whether Django is successfully installed, go to the Django-* directory under DOS, and check the Django version:

The Django version 1.4.5 is successfully displayed, indicating that Django is successfully installed.

  4. Create the first Django Project

Django environment is configured, you can go to the command prompt to create the first Django application, enter a directory, execute the django-admin.py startproject myproject to create a project:

In this case, there is a folder myproject and a manage. py file under the myproject directory. The myproject directory contains four files (four important files ):

_ Init _. py: indicates that the directory is a python package.

Setting. py: Project setting file

URLs. py: URL ing Management

Manage. py: Command for project operations

Wsgi. py: Python web server gateway interface, which is an interface between a Python application or framework and a web server.

  5. Start the Web Server

Go to the myproject directory and run manage. PY runserver to start Django's built-in web server. (because errors cannot be reproduced, The mytest project creation error is used.) If you are lucky, the following 10013 error may not occur:

You don't need to worry when it appears. This is because port 8000 is occupied by other applications. Run netstat-ano in DOS to check the port usage of the application:

Then open the task manager and check the service in it to see the process corresponding to the service whose PID is 6328. My port 8000 is occupied by codoon music, so I re-run manage after it is disabled. after py runserver is enabled, open a browser and enter http: // 127.0.0.1: 8000 in the address bar. If the following interface appears, the Web service starts successfully:

When the page is refreshed, the HTTP status is returned under DOS: (I refreshed twice)

Port 8000 is the default port number of the djangoweb service. Of course, you can also specify the port number manage. py runserver 0.0.0.0: 8888 when starting the Service (you can also directly write the port number here without adding an IP address)

Haha ~~ In this case, enter http: // 127.0.0.1: 8888 in the address bar of the browser, and the default page is displayed:

Now, you can browse the created website.

  6. Create your own page (view and URL configuration)

Previously, I configured it, so the default "It worked!" is displayed !" Webpage. Now I want to write a webpage and display it on my own.

Create a new view. py file under the myproject directory (the second myproject) created previously. The file content is as follows:

Then, bind the URL and view functions, open the URLs. py file, introduce views, and add ('^ hellodjango/$', hellodjango). In this line of code, the URLs. py file is as follows:

At this time, I want to restart the web service and set the port number to 2222 (manage. py runserver 2222), and then enter localhost: 2222/hellodjango in the address bar of the browser. The following expected results will appear:

 

Summary: (change the relevant path)

1. Install Django

Python setup. py install

2. app generation and startup

Django-admin.py startproject {project_name}

Cd {project_name}

[Manage. py Startapp {app name}]

3. Start the server

Manage. py runserver [[IP:] Port]

4. Open the URL in a browser

Http: // localhost: Port/[function name]

 

 

This is the first lesson I learned about Django. It's very interesting ~~

 

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.