Configuration of the Python+django+apache

Source: Internet
Author: User
Tags win32

    • 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" >
    1. From django.http import HttpResponse
    2. def index (Request):
    3. return HttpResponse (' Hello, django! ')

Configuring the urls.py File

[Python]View Plaincopy
  1. From Django.conf.urls.defaults Import *
  2. # Uncomment the next lines to enable the admin:
  3. # from Django.contrib Import admin
  4. # Admin.autodiscover ()
  5. Urlpatterns = Patterns ("',
  6. # Example:
  7. # (R ' ^myproj/', include (' Myproj.foo.urls ')),
  8. (R' ^$ ', ' Myproj.helloworld.index '),
  9. # Uncomment the Admin/doc line below and add ' Django.contrib.admindocs '
  10. # to Installed_apps to enable admin documentation:
  11. # (R ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')),
  12. # Uncomment the next line to enable the admin:
  13. # (R ' ^admin/', include (Admin.site.urls)),
  14. )

7. Configuring Apache's Httpd.conf

Add LoadModule python_module modules/mod_python.so

Edit httpd-vhosts.conf:

[C-sharp]View Plaincopy
  1. Listen 81
  2. Namevirtualhost 127.0.0.1:81
  3. <virtualhost 127.0.0.1:81>
  4. ServerName localhost:81
  5. <location "/" >
  6. SetHandler Python-program
  7. PythonPath "[' c:/python25 '] + sys.path"
  8. Pythonhandler Django.core.handlers.modpython
  9. SETENV Django_settings_module myproj.settings
  10. Pythoninterpreter MySite
  11. Pythonautoreload OFF
  12. Pythondebug on
  13. </Location>
  14. </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" >
    1. From Django.contrib Import admin
    2. From More_with_admin.examples import Models
    3. Class Documentadmin (admin. Modeladmin):
    4. Pass
    5. Class Commentadmin (admin. Modeladmin):
    6. Pass
    7. Admin.site.register (models. Document, Documentadmin)
    8. 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" >
    1. Validating models ...
    2. 0 errors found.
    3. Django version 0.96-pre, using Settings ' mysite.settings '
    4. Development server is running at http://127.0.0.1:8000/
    5. Quit the server with Control-c.
    6. Now you can access http://127.0.0.1:8000/admin/, Login

9. Django Database Settings

Create db.py

[C-sharp]View Plaincopy
  1. #coding =utf-8
  2. #import OS
  3. #os. environ[' django_settings_module '] = ' myproj.settings '
  4. From django.conf Import settings
  5. Settings.configure (
  6. database_engine=' MySQL ',
  7. Database_name=' Django_demo ',
  8. database_user=' root ',
  9. Database_password=',
  10. database_host=' localhost ',
  11. Database_port=',
  12. )

Load_db_py

[Python]View Plaincopy
  1. Import db
  2. From django.db Import connection
  3. cursor = Connection.cursor ()
  4. Cursor.execute ("Select VERSION ()")
  5. row = Cursor.fetchone ()
  6. Print "server version:", row[0]
  7. Cursor.execute ("select * from Django_site")
  8. Row1 = Cursor.fetchall ()
  9. Print Row1
  10. Cursor.close ()
  11. Connection.close ()

If the result appears, the database reads successfully.

Configuration of Python+django+apache

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: info-contact@alibabacloud.com 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.