Home  >  Article  >  Backend Development  >  Python and Django installation steps

Python and Django installation steps

巴扎黑
巴扎黑Original
2017-04-05 13:08:413511browse

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 = "<html><body><h3>Hello World!</h3>It is now %s </body></html>" % 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

<ul>
{% for book in books %}
 
  <li> {{book.title}} </li>

{% endfor %}
</ul>

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