Many beginners ask how Python and Django are installed, so here's a quick introduction to the two software installation steps under Windows 2003.
First, download and install Python
Python official: http://www.python.org/ftp/python/
We have chosen Python 2.7.2 here. Although the latest version is Python 3.2.2, Django does not support Python 3.2.2 at this time.
The installation step is simple, double-click the installation package to start the installation, here we install to d:\python,1,
Figure 1
Click the "Next" button to enter the Python Installation Component Selection screen. Here we install all the components and choose the default settings. 2.
Figure 2
After the installation is complete, you need to set the operating system environment variable path, add the Python installation path ";D: \python" 3
Figure 3
After the setup is complete, we open the cmd Command Prompt window, enter "Python", and then enter, should be able to see a similar screen, 4.
Figure 4
Ok, this time, our Python installation is complete, you can enter the command print "Hello World" Print the string, press ENTER to see if the program performs the same.
Second, download and install Django
Download the current version of Django django-1.3.1.tar.gz. We downloaded this django-1.3.1.tar.gz file is a standard UNIX compressed format of the file, we can also use under Windows WinRAR and other software to extract, extract after we get a Django-1.3.1 directory, suppose we extract to D : \django directory. We open the DOS command Prompt window, enter this directory, and execute the Python setup.py install command to start the Django installation. 5.
Figure 5
After the installation was completed, we found that Django was installed in the D:\Python\Lib\site-packages\django directory. In this directory there is a bin subdirectory, which holds the common Django commands, to facilitate later operation, 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.
Figure 6
So far we've done the Django installation, and we need to verify that we're ready to start working. First we open a CMD command window to see if Django's general instructions will work, and then we'll see if Django has been integrated with the Python language environment. 7.
Figure 7
See, we first execute "django-admin.py--version" at the operating system prompt, the system prints out the Django version number "1.3.1". Then we enter "Python" into the Python runtime environment, at the ">>>" prompt, we enter a Python module import statement "Import Django", this statement that we introduced in the current Python runtime environment " Django "This function module, then we use this function module" version "method to see the version number of this module, also we see the same version number. If you see this information intact on your computer, then OK, which proves that your computer is ready to start executing a Django-based Python program.
Third, create a Django project
To learn Django, our goal is, of course, to develop web-based applications, so let's look at how Django displays a Web page. Open a CMD command window and enter instructions in turn. 8
Figure 8
Here to explain the command, first enter the D disk, enter the command django-admin.py startproject MySite used to create a Web site project, the site directory name is MySite, the path is D:\mysite. Then enter the MySite directory and enter manage.py runserver to open the website. You can specify a port, which defaults to 8000, and if you want to use port 90, write manage.py Runserver 90.
Finally we open the browser in the Address bar to enter the address http://localhost:8000, see "It worked" it? 9
Figure 9
Next we build a Hello World page:
With Django, the content of a page is generated by a view function, and we create a view file in the D:\mysite directory views.py enter the following:
From django.http import httpresponseimport datetimedef Hello (Request): Now = Datetime.datetime.now () html = "
Next, modify the urls.py file in the MySite directory with the following contents:
From django.conf.urls.defaults import patterns, include, Urlurlpatterns = Patterns ("", (' ^hello/$ ', ' Mysite.views.hello '),)
Finally we open the browser in the Address bar to enter the address http://localhost:8000/hello/, the results show 10
Figure 10
Iv. building a MySQL database application1, first install MySQL database, we install here to D:\MySQL. Specific references open document: MySQL installation diagram
2, install Python-mysql drive.
Official: http://sourceforge.net/projects/mysql-python/files/
Windows version: Http://www.codegood.com/downloads
We use Windows version Mysql-python-1.2.3.win32-py2.7.exe here
2. Modify the database entry for the settings.py configuration file
In the D:\mysite directory, there is a setttings.py file, open it, find the databases, change the database connection parameters. The results are as follows:
DATABASES = {' default ': ' ENGINE ': ' MySQL ', ' name ': ' Your database name ', ' USER ': ' Your MySQL account ', ' PASSWORD ' : ' Your MySQL password ', ' HOST ': ' 127.0.0.1 ', ' PORT ': ' 3306 ', }}
Open the cmd window and enter the following instruction in the D:\mysite directory to test the success of the data connection. 11
Figure 11
If there are no prompt messages, the connection to the database is successful.
3. Create a new app app books
Open the cmd window and enter the command in the D:\mysite directory: 12
Figure 12
4. Custom model File
Under the D:\mysite\books directory, modify the contents 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 '% (self.title,self.authors)
Create a model of a book's data table
4. Modify the setttings.py file to activate the books application
Then to the settings.py file, modify the Installed_apps.
Installed_apps = ( ' mysite.books ',)
5. Build a table
Open the cmd window and enter the following command in the D:\mysite directory to synchronize your model to the database. 13
Figure 13
6. Insert some records into the data table
Open the Cmd window, and in the D:\mysite directory, enter some instructions. 14
Figure 14
7. Modify the contents 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 subdirectory under the D:\mysite directory templates as a directory for storing templates
Create a new template file booklist.html content below
<ul>{% in books%} <li> {{book.title}} </li>{% endfor%}</ul>
10. Modify the d:\mysite\settings.py file
Locate the Template_dirs entry and modify the following:
Template_dirs = ( ' d:/mysite/templates ')
Finally enter MySite This directory, enter manage.py runserver to open the website. Open browser Access address Http://localhost:8000/books results 15
Original link: http://www.admin10000.com/document/94.html
Python, Django, and MySQL installation steps