• python+Django+apache的配置


    1、先安装Python-2.5.4.msi

    2、安装 Django-1.1.1-final.tar.gz 解压开,然后解压到某个目录如:(D:/Dev)

    在命令提示符下进入该目录,输入:cd D:/Dev/Django-1.1.1

    再输入命令:python setup.py install

    先简单的测试一下。

    命令提示符下,输入:python

    然后输入import django

    然后输入django.VERSION

    我看到的是这样的: >>> import django >>> django.VERSION (final 1.1.1) >>>

    3、安装 MySQL-python-1.2.2.win32-py2.5.exe

    这个双击安装过程中应该不会出错。

    4、安装 mod_python-3.3.1.win32-py2.5-Apache2.2.exe

    最后一个选择目录要安装在apache的安装目录下。

    5、新建项目

    命令行进入c:/Python25/,执行“django-admin.py startproject myproj”,新建名为myproj的项目。

    6、新建py文件

    在c:/Python25/myproj目录下新建helloWord.py:

    [python] view plaincopy
    1. from django.http import HttpResponse  
    2.   
    3. def index(request):  
    4.     return HttpResponse('Hello, Django!')  

    配置urls.py文件

    [python] view plaincopy
    1. from django.conf.urls.defaults import *  
    2.   
    3. # Uncomment the next two lines to enable the admin:  
    4. # from django.contrib import admin  
    5. # admin.autodiscover()  
    6.   
    7. urlpatterns = patterns('',  
    8.     # Example:  
    9.     # (r'^myproj/', include('myproj.foo.urls')),  
    10.     (r'^$', 'myproj.helloworld.index'),  
    11.     # Uncomment the admin/doc line below and add 'django.contrib.admindocs'   
    12.     # to INSTALLED_APPS to enable admin documentation:  
    13.     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),  
    14.   
    15.     # Uncomment the next line to enable the admin:  
    16.     # (r'^admin/', include(admin.site.urls)),  
    17. )  

    7、配置Apache的httpd.conf

    添加LoadModule python_module modules/mod_python.so

    编辑httpd-vhosts.conf:

    [c-sharp] view plaincopy
    1. Listen 81  
    2.   
    3. NameVirtualHost 127.0.0.1:81  
    4.   
    5. <VirtualHost 127.0.0.1:81>  
    6.     ServerName localhost:81  
    7.     <Location "/">  
    8.         SetHandler python-program  
    9.         PythonPath "['c:/python25'] + sys.path"  
    10.         PythonHandler django.core.handlers.modpython  
    11.         SetEnv DJANGO_SETTINGS_MODULE myproj.settings  
    12.     PythonInterpreter mysite  
    13.         PythonAutoReload Off  
    14.         PythonDebug On  
    15.     </Location>  
    16. </VirtualHost>  

    注:80为web端口,81为新端口 pythonpath=c:/python25

    配置好后可以在http://localhost:81 访问Django的站点目录。

    8、Django admin设置

    (1) 创建admin.py在项目myproj下

    [c-sharp] view plaincopy
    1. from django.contrib import admin  
    2. from more_with_admin.examples import models  
    3.   
    4. class DocumentAdmin(admin.ModelAdmin):  
    5.     pass  
    6.   
    7. class CommentAdmin(admin.ModelAdmin):  
    8.     pass  
    9.   
    10. admin.site.register(models.Document, DocumentAdmin)  
    11. admin.site.register(models.Comment, CommentAdmin)  

    (2) 在seettings中的INSTALLED_APPS 添加

    'django.contrib.admin', (3) 在urls中添加

    from django.contrib import admin admin.autodiscover() 与

    (r'^admin/(.*)', admin.site.root),

    运行python manage.py sqlall admin

    (4) 运行 python manage.py runserver,将会出现以下信息

    [c-sharp] view plaincopy
    1. Validating models...   
    2. 0 errors found.   
    3.   
    4. Django version 0.96-pre, using settings 'mysite.settings'   
    5. Development server is running at http://127.0.0.1:8000/   
    6. Quit the server with CONTROL-C.   
    7. 现在你可以访问http://127.0.0.1:8000/admin/,登录  

    9、Django 数据库设置

    创建db.py

    [c-sharp] view plaincopy
    1. #coding=utf-8  
    2. #import os    
    3. #os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'   
    4.   
    5. from django.conf import settings  
    6. settings.configure(  
    7.         DATABASE_ENGINE='mysql',   
    8.         DATABASE_NAME='django_demo',   
    9.         DATABASE_USER='root',   
    10.         DATABASE_PASSWORD='',   
    11.         DATABASE_HOST='localhost',   
    12.         DATABASE_PORT='',   
    13.     )  

    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.   
    8. cursor.execute ("SELECT * from django_site")  
    9. row1 = cursor.fetchall ()  
    10.   
    11. print row1  
    12.   
    13. cursor.close ()  
    14. connection.close ()  

    如果出现结果,说明数据库读取成功。

  • 相关阅读:
    gdb高级技巧
    Fira Code字体安装与配置
    回归
    【Luogu】P2292 [HNOI2004]L语言 题解
    浅谈Linux桌面(发行版及桌面环境)
    剑指offer-和为S的连续正数序列-知识迁移能力-python
    剑指offer-数组中只出现一次的数字-数组-python
    剑指offer-数字在排序数组中出现的次数-数组-python
    剑指offer-数组中的逆序对-数组-python
    剑指offer-丑数-穷举-python
  • 原文地址:https://www.cnblogs.com/zhwl/p/4291979.html
Copyright © 2020-2023  润新知