首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

关于服务器——装配配置django

2013-10-14 
关于服务器——安装配置djangodjango是一个基于python语言的服务器框架,能够帮助我们轻易开发一个web应用,下

关于服务器——安装配置django

django是一个基于python语言的服务器框架,能够帮助我们轻易开发一个web应用,下面记录一些简单的安装使用方法。

sudo pip install Django==1.5.1

创建一个新的web服务(mysite,可以进行路由路径配置,数据库配置等)

django-admin.py startproject mysite

创建一个新的web应用(books,具体代码实现)

django-admin.py start app books

?

类似MVC框架,django提供model,view等模块供我们使用(不过django的model更类似于数据库定义,view类似MVC中的controller,template才是MVC中的view)编辑代码如下

books/models.py

from django.db import models# Create your models here.class Publisher(models.Model):    name = models.CharField(max_length=30)    address = models.CharField(max_length=50)    city = models.CharField(max_length=60)    state_province = models.CharField(max_length=30)    country = models.CharField(max_length=50)    website = models.URLField()    def __unicode__(self):        return self.name#class Author(models.Model):#    first_name = models.CharField(max_length=30)#    last_name = models.CharField(max_length=40)#    email = models.EmailField()##    def __unicode__(self):#        return u'%s %s' % (self.first_name, self.last_name)#class Book(models.Model):#    title = models.CharField(max_length=100)#    authors = models.ManyToManyField(Author)#    publisher = models.ForeignKey(Publisher)#    publication_date = models.DateField()##    def __unicode__(self):#        return self.title

?books/views.py

# Create your views here.from django.shortcuts import render_to_responsefrom books.models import Publisherdef list_publishers(request):        publishers = Publisher.objects.all()        return render_to_response('list_publishers.html', {'publishers': publishers})

?templates/list_publishers.html

<table>        {% for p in publishers %}        <tr>        <td>Id #{{ p.id }}</td>        <td>Name #{{ p.name }}</td>        </tr>        {% endfor %}</table>

自定义配置web,设置数据库连接等

mysite/settings.py

INSTALLED_APPS = (    'books',)DATABASES = {    'default': {        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.        'NAME': '/home/ciaos/django.sqlite3',                      # Or path to database file if using sqlite3.        # The following settings are not used with sqlite3:        'USER': '',        'PASSWORD': '',        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.        'PORT': '',                      # Set to empty string for default.    }}TEMPLATE_DIRS = (    '/home/ciaos/mysite/templates',)

mysite/urls.py(利用python强大的正则表达式配置路由)

urlpatterns = patterns('',    # Examples:    # url(r'^$', 'mysite.views.home', name='home'),    # url(r'^mysite/', include('mysite.foo.urls')),    # Uncomment the admin/doc line below to enable admin documentation:    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),    # Uncomment the next line to enable the admin:    url(r'^publishers/',list_publishers))

python manager.py syncdb同步models到数据库

通过python manage.py shell简单操作查询数据库

运行数据库如下(如果要后台运行可以使用nohup)

python manage.py runserver

?用ab测试

ciaos@linux-53dr:~/mysite> sudo /usr/sbin/ab2 -c 10 -n 10000 http://127.0.0.1:8000/publishers/This is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 127.0.0.1 (be patient)Completed 1000 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        WSGIServer/0.1Server Hostname:        127.0.0.1Server Port:            8000Document Path:          /publishers/Document Length:        69 bytesConcurrency Level:      10Time taken for tests:   49.311 secondsComplete requests:      10000Failed requests:        0Write errors:           0Total transferred:      2020000 bytesHTML transferred:       690000 bytesRequests per second:    202.80 [#/sec] (mean)Time per request:       49.311 [ms] (mean)Time per request:       4.931 [ms] (mean, across all concurrent requests)Transfer rate:          40.00 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    2  38.6      0    1001Processing:    22   47  82.8     36    2562Waiting:        6   41  82.7     31    2553Total:         22   49  91.3     36    2562Percentage of the requests served within a certain time (ms)  50%     36  66%     39  75%     41  80%     42  90%     52  95%     63  98%    100  99%    428 100%   2562 (longest request)

并发数据等没有参考意义,因为是在我笔记本的虚拟机上测试的,主要是与tornado服务器做对比。用django实现一个简易的web应用还是非常方便的。

热点排行