- Download and install the XAMPP kit
- Download Mod_python-3.3.1.win32-py2.5-apache2.2.exe
- Download Python-2.5.4.msi
- Download Django
- Download Mysql-python-1.2.2.win32-py2.5.exe
1, first install Python-2.5.4.msi
2, installation django-1.1.1-final.tar.gz unpack, then extract to a directory such as: (D:/dev)
Enter the directory at the command prompt, enter: CD d:/dev/django-1.1.1
Re-enter command: Python setup.py install
Let's test it briefly.
At the command prompt, enter: python
Then input the import Django
then enter Django. VERSION
What I see is this: >>> import Django >>> Django. VERSION (Final 1.1.1) >>>
3, installation Mysql-python-1.2.2.win32-py2.5.exe
This double-click should not be an error during the installation process.
4, Installation Mod_python-3.3.1.win32-py2.5-apache2.2.exe
The last selection directory will be installed in the Apache installation directory.
5. New Project
The command line enters c:/python25/, executes "django-admin.py startproject MyProj" and creates a new project named MyProj.
6. New PY File
Create a new helloword.py under the C:/python25/myproj directory:
[Python]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- From django.http import HttpResponse
- def index (Request):
- return HttpResponse (' Hello, django! ')
Configuring the urls.py File
[Python]View Plaincopy
- From Django.conf.urls.defaults Import *
- # Uncomment the next lines to enable the admin:
- # from Django.contrib Import admin
- # Admin.autodiscover ()
- Urlpatterns = Patterns ("',
- # Example:
- # (R ' ^myproj/', include (' Myproj.foo.urls ')),
- (R' ^$ ', ' Myproj.helloworld.index '),
- # Uncomment the Admin/doc line below and add ' Django.contrib.admindocs '
- # to Installed_apps to enable admin documentation:
- # (R ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')),
- # Uncomment the next line to enable the admin:
- # (R ' ^admin/', include (Admin.site.urls)),
- )
7. Configuring Apache's Httpd.conf
Add LoadModule python_module modules/mod_python.so
Edit httpd-vhosts.conf:
[C-sharp]View Plaincopy
- Listen 81
- Namevirtualhost 127.0.0.1:81
- <virtualhost 127.0.0.1:81>
- ServerName localhost:81
- <location "/" >
- SetHandler Python-program
- PythonPath "[' c:/python25 '] + sys.path"
- Pythonhandler Django.core.handlers.modpython
- SETENV Django_settings_module myproj.settings
- Pythoninterpreter MySite
- Pythonautoreload OFF
- Pythondebug on
- </Location>
- </VirtualHost>
Note: 80 is a web port and 81 is a new port Pythonpath=c:/python25
Once configured, you can access the Django Site directory in http://localhost:81.
8. Django Admin Settings
(1) Create admin.py under Project MyProj
[C-sharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- From Django.contrib Import admin
- From More_with_admin.examples import Models
- Class Documentadmin (admin. Modeladmin):
- Pass
- Class Commentadmin (admin. Modeladmin):
- Pass
- Admin.site.register (models. Document, Documentadmin)
- Admin.site.register (Models.comment, Commentadmin)
(2) Installed_apps added in Seettings
' Django.contrib.admin ', (3) Add in URLs
From django.contrib Import admin admin.autodiscover () and
(R ' ^admin/(. *) ', admin.site.root),
Run python manage.py sqlall admin
(4) Run Python manage.py runserver, the following message will appear
[C-sharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- Validating models ...
- 0 errors found.
- Django version 0.96-pre, using Settings ' mysite.settings '
- Development server is running at http://127.0.0.1:8000/
- Quit the server with Control-c.
- Now you can access http://127.0.0.1:8000/admin/, Login
9. Django Database Settings
Create db.py
[C-sharp]View Plaincopy
- #coding =utf-8
- #import OS
- #os. environ[' django_settings_module '] = ' myproj.settings '
- From django.conf Import settings
- Settings.configure (
- database_engine=' MySQL ',
- Database_name=' Django_demo ',
- database_user=' root ',
- Database_password=',
- database_host=' localhost ',
- Database_port=',
- )
Load_db_py
[Python]View Plaincopy
- Import db
- From django.db Import connection
- cursor = Connection.cursor ()
- Cursor.execute ("Select VERSION ()")
- row = Cursor.fetchone ()
- Print "server version:", row[0]
- Cursor.execute ("select * from Django_site")
- Row1 = Cursor.fetchall ()
- Print Row1
- Cursor.close ()
- Connection.close ()
If the result appears, the database reads successfully.
Configuration of Python+django+apache