Python + Django + apache, pythondjango

Source: Internet
Author: User
Tags install django

Python + Django + apache, pythondjango

This article describes how to configure python + Django + apache. We will share this with you for your reference. The details are as follows:

Download and install xampp
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. Install Django-1.1.1-final.tar.gzDecompress the package and decompress it to a directory such as: (D:/Dev)

Enter the directory at the command prompt and enter: cd D:/Dev/Django-1.1.1
Run python setup. py install.
First, perform a simple test.
At the command prompt, enter: python
Enter import django
Enter django. VERSION
What I see is: >>> import django >>> django. VERSION (final 1.1.1) >>>

3. Install MySQL-python-1.2.2.win32-py2.5.exe

This double-click installation process should not cause errors.

4. Install mod_python-3.3.1.win32-py2.5-Apache2.2.exe

The last selection directory should be installed under the apache installation directory.

5. Create a project

Run the command line to enter c:/Python25/and run the django-admin.py startproject myproj to create a project named myproj.

6. Create a py file

Create helloWord. py in the c:/Python25/myproj directory:

from django.http import HttpResponsedef index(request):  return HttpResponse('Hello, Django!')

Configure the urls. py file

from django.conf.urls.defaults import *# Uncomment the next two 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. Configure httpd. conf of Apache

Add LoadModule python_module modules/mod_python.so

Edit httpd-vhosts.conf:

Listen 81NameVirtualHost 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: Port 80 is the web port and port 81 is the new port pythonpath = c:/python25

After configuration, you can access the Django site directory at http: // localhost: 81.

8. Django admin settings

(1) Create admin. py under the project myproj.

from django.contrib import adminfrom more_with_admin.examples import modelsclass DocumentAdmin(admin.ModelAdmin):  passclass CommentAdmin(admin.ModelAdmin):  passadmin.site.register(models.Document, DocumentAdmin)admin.site.register(models.Comment, CommentAdmin)

(2) Add INSTALLED_APPS in seettings

'Django. contrib. admin'

(3) add in urls

From django. contrib import admin. autodiscover () and
(R '^ admin/(. *)', admin. site. root ),

Run python manage. py sqlall admin

(4) Run python manage. py runserver and the following information will appear:

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/and log on

9. Django database settings

Create db. py

#coding=utf-8#import os#os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'from django.conf import settingssettings.configure(    DATABASE_ENGINE='mysql',    DATABASE_NAME='django_demo',    DATABASE_USER='root',    DATABASE_PASSWORD='',    DATABASE_HOST='localhost',    DATABASE_PORT='',  )

Load_db_py

import dbfrom django.db import connectioncursor = connection.cursor ()cursor.execute ("SELECT VERSION()")row = cursor.fetchone ()print "server version:", row[0]cursor.execute ("SELECT * from django_site")row1 = cursor.fetchall ()print row1cursor.close ()connection.close ()

If the result is displayed, the database is read successfully.

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.