简体   繁体   中英

Python Django 1.6 execute function for every request before getting to view

I'm writing some API functionality for my project using Python 3.4 and Django 1.6.

All functionality works fine, but I want execute one function for all that kind of requests.
For Example: I have following urls.py file in my API application in Django project

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',

    url(r'^getposts', 'Postigs.views.get_posts', name='getPosts'),
    url(r'^addpost', 'Postigs.views.add_post', name='addPost'),
    url(r'^addcomment', 'Postigs.views.add_comment', name='addComment'),
)


And views.py for that URL requests handling.

So is it possible to execute some function for : 执行一些功能:

def pre_execute(request):
    do_something_before_view_function()


I've worked before with many PHP frameworks , there are always some pre_execute() function ... also I've worked with ASP.NET MVC , Node.js Express.js , and all have that function which is firing before request action.

I don't believe that Django didn't have it , but I can't find how implement that functionality.

Thanks.

Like iskorum mentioned above, Middlewares is the answer. Or there is also a chance that you are looking for View Decorators. Here is the link https://docs.djangoproject.com/en/1.6/topics/http/decorators/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM