django心得:url可以做的更多

辛辛苦苦,完善了一个url_decorate的django app,支持include,支持多个decorate的复合

代码在http://groups.google.com/group/python-cn/browse_thread/thread/6980d49316499e96

可以方便的在url中使用decorate,这里我演示了一个内建的登录,一个自定义的页面访问量的统计
如自定义的页面访问量的统计代码很简单,如下
models.py
from django.db import models
class PageAnalytic(models.Model):
    url = models.URLField(max_length=2048,primary_key=True)
    visit = models.IntegerField(default=0)

    class Admin:
        list_display= ('url','visit')

    class Meta:
        ordering = ['url']

views.py:

from models import PageAnalytic
from url_decorate import make_decorate

@make_decorate
def page_analytic(request):
    page,is_create=PageAnalytic.objects.get_or_create(url=request.path)
    page.visit+=1
    page.save()

shortcuts.py:

from url_decorate import url_decorate,patterns_decorate

from views import page_analytic

page_analytic_url=url_decorate(page_analytic)
page_analytic_patterns=patterns_decorate(page_analytic)

使用示例:

.................
from utils.url_decorate.shortcuts import
page_analytic_patterns,page_analytic_url

urlpatterns = patterns('',
    #统计页面访问量
    page_analytic_url(r'^admin/', include('django.contrib.admin.urls')),
    (r'^media/(?P .*)', 'django.views.static.serve',
{'document_root' : settings.MEDIA_ROOT, 'show_indexes':True}),
)

#更方便的形式
urlpatterns+=page_analytic_patterns('',
    (r'^$',direct_to_template, {'template': 'mytube.html'}),
)



Url decorator

Some time we want to account the number of visitor by url or some url
only the logined in user can visit .

Yes , we can use url decorator do this thing .

I write a reuseable app for simplify those job . It can make decorator
more easy , can work with "include(xxx.urls)",
support multi-decorator .

Ok , we give a simplest example:

from django.conf.urls.defaults import *
from django.contrib.admin.views.decorators import staff_member_required
from django.views.generic.simple import direct_to_template

from url_decorate.url_decorate import url_decorate

staff_member_url=url_decorate(staff_member_required)

urlpatterns = patterns('',
    staff_member_required(r'^$',direct_to_template, {'template': 'xxx.html'}),
)

Ok,You will find the / of this website require login in .
But , sometime we have a lot of url should be logined in ,
patterns_decorate will help you to do this more easy .
Just use as below:
staff_member_patterns=patterns_decorate(staff_member_required)
urlpatterns = staff_member_patterns('',
    (r'xxx',direct_to_template, {'template': 'xxx.html'}),
    (r'xxx',direct_to_template, {'template': 'xxx.html'}),
    (r'xxx',direct_to_template, {'template': 'xxx.html'}),
)

For lazy people as me , I write staff_member_patterns,staff_member_url
and login_url,login_patterns in shortcuts.py , just import it to use
:)

Then , let's write a decorator by ourself .This decorator can account
the number of visitor for each pages .

First , write a model

models.py:
#--------------------------------------------------------------------------<wbr></wbr>--
from django.db import models
class PageAnalytic(models.Model):
   url = models.URLField(max_length=2048,primary_key=True)
   visit = models.IntegerField(default=0)

   class Admin:
       list_display= ('url','visit')

   class Meta:
       ordering = ['url']
#--------------------------------------------------------------------------<wbr></wbr>--

Second , write the view . You just need to write a function can
process the request .
The other thing "make_decorate" can take it over .

views.py:
#--------------------------------------------------------------------------<wbr></wbr>--
from models import PageAnalytic
from url_decorate import make_decorate

@make_decorate
def page_analytic(request):
   page,is_create=PageAnalytic.objects.get_or_create(url=request.path)
   page.visit+=1
   page.save()
#--------------------------------------------------------------------------<wbr></wbr>--

At last , write two shortcuts

shortcuts.py:
#--------------------------------------------------------------------------<wbr></wbr>--
from url_decorate import url_decorate,patterns_decorate
from views import page_analytic

page_analytic_url=url_decorate(page_analytic)
page_analytic_patterns=patterns_decorate(page_analytic)
#--------------------------------------------------------------------------<wbr></wbr>--

Now , It's time to use this , we just accout the admin .

#--------------------------------------------------------------------------<wbr></wbr>--
from url_decorate.shortcuts import page_analytic_url

urlpatterns = patterns('',
   page_analytic_url(r'^admin/', include('django.contrib.admin.urls')),
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值