Install Python and Django

Source: Internet
Author: User
Tags install django
Python and Django installation steps many beginners have asked how to install Python and Django. here we will briefly introduce the installation steps of these two software in Windows 2003.

1. download and install Python

Python official:

Python 2.7.2 is selected here. Although the latest version is Python 3.2.2, Django currently does not support Python 3.2.2.

The installation procedure is very simple. double-click the installation package to start installation. here we install it to D: \ Python, 1,

Click "Next" to go to the Python installation component selection page. Install all the components and select the default settings. 2.

After installation, you need to set the Path of the operating system environment variable and add the Python installation Path "; D: \ Python" 3

After the settings are complete, open the CMD command prompt window, enter "python", and press enter to see a similar image, 4.

OK. At this time, the python installation is complete. you can enter the print "Hello world" command to print the string and press the Enter key to check whether the program execution effect is the same.

2. download and install Django

Download the latest version of Django Django-1.3.1.tar.gz. We download this django-1.3.1.tar.gz file is a standard Unix compressed format file, we can also use WinRAR and other software in Windows to decompress, decompress, we get a Django-1.3.1 Directory, suppose we decompress the package to the D: \ Django directory. Open the doscommand prompt window, enter this directory, and then execute the python setup. py install command to start Django installation. 5.

After the installation, we found that Django was installed in the directory D: \ Python \ Lib \ site-packages \ django. There is a bin subdirectory in this directory, which stores common Django commands. to facilitate future operations, we need to add this bin Path to the operating system environment variable Path. Add the Django command path "; D: \ Python \ Lib \ site-packages \ django \ bin ". 6.

So far, we have completed the installation of Django. Next we need to verify whether we can start working. First, open a CMD command window to check whether the common commands of Django can be used. then, let's see if Django has been integrated with the python environment. 7.

As you can see, we first execute " -- version" at the operating system prompt, and the system prints the Django version number "1.3.1 ". Enter "Python" to enter the python runtime environment. at the ">>>" prompt, enter a python module import statement "import django ", this statement indicates that the "django" function module is introduced in the current python runtime environment. then, we use the "VERSION" method of this function module to view the VERSION number of this module, we also see the same version number. If you see the complete information on your computer, then OK, which proves that your computer can start to execute python programs based on the Django system.

3. create a Django project

To learn about Django, we certainly aim to develop a Web-based application system. let's take a look at how Django displays a Web page. Open a CMD command window and enter commands in sequence. 8

Figure 8

Here to explain the command, first enter the D disk, enter the command startproject mysite to create a website project, the site directory name is mysite, Path is D: \ mysite. Go to the mysite directory and enter manage. py runserver to open the website. You can specify a port. the default value is 8000. to use port 90, write it as manage. py runserver 90.

Finally, open the browser and enter the address http: // localhost: 8000 in the address bar. do you see "It worked? 9

Figure 9

Next we will create a Hello world page:

With Django, the page content is generated by View functions. we create a view file views. py in the D: \ mysite directory and enter the following content:

from django.http import HttpResponseimport datetimedef hello(request):    now =    html = "Hello World!It is now %s " % now    return HttpResponse(html)

Next, modify the URL. py file under the mysite Directory. the content is as follows:

from django.conf.urls.defaults import patterns, include, urlurlpatterns = patterns('',    ('^hello/$','mysite.views.hello'),)

Finally, open the browser and enter the address http: // localhost: 8000/hello/in the address bar. The result is 10.


4. create a Mysql database application

1. first install the MySQL database. here we install it to D: \ MySQL. For more information, see the MySQL installation diagram.

2. install the python-mysql driver.


We use windows MySQL-python-1.2.3.win32-py2.7.exe here

2. modify the database items of the settings. py configuration file.

There is a setttings. py file under the D: \ mysite Directory, open it, find DATABASES, and change the database connection parameters. The result is as follows:

DATABASES = {'default': {'engine ': 'mysql', 'name': 'Your database name', 'user': 'Your mysql account ', 'password': 'Your MYSQL password', 'host': '2017. 0.0.1 ', 'port': '123 ',}}

Open the CMD window and enter the following command in the D: \ mysite directory to test whether the data connection is successful. 11


If no prompt is displayed, the database connection is successful.

3. create an App, books

Open the CMD window and enter the command 12 in the D: \ mysite directory.


4. customize the model file

Under the D: \ mysite \ books Directory, modify the content of the models. py file as follows.

from django.db import modelsclass Book(models.Model):    title = models.CharField(max_length=100)    authors = models.CharField(max_length=100)    publisher = models.CharField(max_length=100)    publication_date = models.DateField()    def __unicode__(self):        return u'%s %s' % (self.title,self.authors)

Create a data table model for a book

4. modify the setttings. py file to activate the books application.

Go to the settings. py file and modify INSTALLED_APPS.

INSTALLED_APPS = (    'mysite.books',)

5. create a table

Open the CMD window and enter the following command in the d: \ mysite directory to synchronize your model to the database. 13


6. Insert some records into the data table

Open the CMD window and enter some commands under the d: \ mysite directory. 14


7. modify the content of the D: \ mysite \ books \ views. py file.

from django.shortcuts import render_to_responsefrom books.models import Bookdef booklist(request):    list = Book.objects.all()    return render_to_response('booklist.html',{'books':list})

8. modify the content of d: \ mysite \ url. py. The result is:

urlpatterns = patterns('',    ('^hello/$','mysite.views.hello'),    ('^books/$','mysite.books.views.booklist'),)

9. create a new sub-directory templates under the D: \ mysite directory as the Directory for storing the template.

Create a new template file booklist.html with the following content

    {% for book in books %}
  • {{book.title}}
  • {% endfor %}

10. modify the d: \ mysite \ settings. py file.

Find the TEMPLATE_DIRS item and modify the content as follows:

TEMPLATE_DIRS = (      'd:/mysite/templates')

Finally, enter the mysite directory and enter manage. py runserver to open the website. Open the browser access address http: // localhost: 8000/books. Result 15:


The above describes the installation steps of Python and Django. For more information, see other related articles in the first PHP community!

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: 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.