search
HomeBackend DevelopmentPython TutorialPython and Django installation steps

Many beginners ask how to install Python and Django. Here we will briefly introduce the installation steps of these two software under Windows 2003.

1. Download and install Python

Python official download address: http://www.python.org/ftp/python/

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

The installation steps are very simple. Double-click the installation package to start the installation. Here we install to D:\Python, as shown in Figure 1,


figure 1

Click the "Next" button to enter the Python installation component selection interface. Here we install all components and select the default settings. As shown in Figure 2.


figure 2

After the installation is completed, you need to set the operating system environment variable Path and add the Python installation path ";D:\Python" as shown in Figure 3


image 3

After the settings are completed, we open the CMD command prompt window, enter "python", and press Enter. You should see a similar screen, as shown in Figure 4.


Figure 4

Ok, at this time, our python installation is complete. You can enter the command print "Hello world" to print the string, and press the Enter key to see if the execution effect of the program is the same.

2. Download and install Django

Download the latest version of Django Django-1.3.1.tar.gz. The Django-1.3.1.tar.gz file we downloaded is a standard Unix compression format file. We can also use software such as WinRAR to decompress it under Windows. After decompression, we get a Django-1.3.1 Directory, assuming we extract it to the D:\Django directory. We open a DOS command prompt window, enter this directory, and then execute the python setup.py install command to start the Django installation. As shown in Figure 5.


Figure 5

After the installation is complete, 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". As shown in Figure 6.


Figure 6

So far we have completed the Django installation, now we need to verify whether we can start working. First, we open a CMD command window to see if Django's regular instructions can be used, and then we see if Django has been integrated with the python language environment. As shown in Figure 7.


Figure 7

As you can see from the picture, we first execute "django-admin.py --version" at the operating system prompt, and the system prints out the Django version number "1.3.1". Then we enter "Python" to enter the python running environment. At the ">>>" prompt, we enter a python module import statement "import django". This statement means that we are introducing "in the current python running environment" django" function module; then we use the "VERSION" method of this function module to view the version number of this module, and we also see the same version number. If you can see this information completely on your computer, then OK, this proves that your computer can start executing python programs based on the Django system.

3. Create a Django project

Our purpose of learning Django is of course to use it to develop Web-based application systems. Let's take a look at how Django displays a Web page. Open a CMD command window and enter the commands in sequence. As shown in Figure 8


Figure 8

Here is an explanation of the commands in the picture above. First, enter the D drive and enter the command django-admin.py startproject mysite to create a website project. The website directory name is mysite and the path is D:\mysite. Then enter the mysite directory and enter manage.py runserver to open the website. You can specify the port, which defaults to 8000. If you want to use port 90, write manage.py runserver 90.

Finally, we open the browser and enter the address http://localhost:8000 in the address bar. Do you see "It worked"? As shown in Figure 9


Figure 9

Next we create a Hello world page:

Using Django, the content of the page 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 HttpResponse
import datetime

def hello(request):
    now = datetime.datetime.now()
    html = "<h3 id="Hello-World">Hello World!</h3>It is now %s " % now
    return HttpResponse(html)

Next, modify the urls.py file in the mysite directory as follows:

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',

    ('^hello/$','mysite.views.hello'),
)

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


Figure 10

4. Create a Mysql database application

1. First install the MySQL database, here we install it to D:\MySQL. Please refer to the documentation for details: MySQL installation diagram

2. Install the python-mysql driver.

Official download address: http://sourceforge.net/projects/mysql-python/files/
Windows version download address: http://www.codegood.com/downloads

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

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

There is a settings.py file in the D:\mysite directory. Open it, find the DATABASES item, and change the database connection parameters. The results are as follows:

DATABASES = {
    'default': {
        'ENGINE': 'mysql',
        'NAME': '你的数据库名称',
        'USER': '你的MYSQL账号',
        'PASSWORD': '你的MYSQL密码',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

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


Figure 11

If there is no prompt message, it means the database connection is successful.

3. Create a new App books

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


Figure 12

4. Custom model file

In the D:\mysite\books directory, modify the contents of the models.py file as follows.

from django.db import models

class 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 model of a book data table

4. Modify the settings.py file and activate the books application

Go to the settings.py file and modify the INSTALLED_APPS item.

INSTALLED_APPS = (
    'mysite.books',
)

5. Create table

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


Figure 13

6. Insert some records into the data table

Open the CMD window and enter some commands in the d:\mysite directory. As shown in Figure 14


Figure 14

7. Modify the contents of the D:\mysite\books\views.py file

from django.shortcuts import render_to_response
from books.models import Book

def 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 subdirectory templates in the D:\mysite directory as the directory to store templates

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 it as follows:

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

Finally, enter the mysite directory and enter manage.py runserver to open the website. Open the browser and access the address http://localhost:8000/books. The result is shown in Figure 15


Figure 15

The above is the detailed content of Python and Django installation steps. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools