可以方便的在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}),
)
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