简体   繁体   English

如何创建函数是views.py,以便views.py中的所有函数都可以使用它

[英]How to create a function is views.py so that all the functions inside the views.py can use it

Hi I have the following definition 嗨,我有以下定义

if GroupProfile.objects.filter(id=self.request.session['ACL_gid'], permissions__codename='view_page'):
    context['can_view_page'] = 1
else:
    context['can_view_page'] = 0
return context

I am using this part of code in many of my views. 我在许多视图中都使用这部分代码。 How can I define it once so that I dont need to use it every time? 如何定义一次,这样就不必每次都使用它?

You can simply write function in any common file like, 您可以简单地在任何常见文件中编写函数,例如,

def your_func(args):
 #do your work

and simply import it wherever you want to use it like a normal python function. 并像普通的python函数一样简单地将其导入到您想使用它的任何地方。 eg. 例如。 from common import your_func

a) You can create your a method inside your model or in Modelmanager(I think it would be most elegant solution): a)您可以在模型内或在Modelmanager中创建方法(我认为这是最优雅的解决方案):

class MyManager(models.Manager):
    def can_view_page(self, acl_gid, perm_code = 'view_page'):
        return 1 if self.filter(id=acl_gid, permissions__codename=perm_code) else 0


class GroupProfile(models.Model):
    .....
    objects = MyManager()

b) You can create just a function in something like utils.py. b)您可以使用utils.py之类的东西来创建一个函数。 c) You can create a Mixin class and put there this function. c)您可以创建一个Mixin类,并放置此函数。 You can always do there something like: 您可以随时在此处执行以下操作:

class MyMixin(object):
    def get_context_data(self, **kwargs):
        context = super(MyMixin, self).get_context_data(**kwargs)
        context['can_view_page'] = 1 GroupProfile.objects.filter(id=self.request.session['ACL_gid'], permissions__codename='view_page') else 0
        return context

But I'm not a big fan of this approach since it overrides instead of extend - it isn't really transparent in my opinion. 但是我不是这种方法的忠实拥护者,因为它overrides而不是extend -在我看来,它并不是真正透明的。

d) You can create a decorator!) But I don't think it is very right case to use it. d)您可以创建一个装饰器!)但是我认为使用它不是非常合适的情况。

You need to create a custom template context processor , and then add it to your TEMPLATES setting. 您需要创建一个自定义模板上下文处理器 ,然后将其添加到您的TEMPLATES设置中。 The context provider is something very simple: 上下文提供程序非常简单:

from .models import GroupProfile

def view_page(request):
    if GroupProfile.objects.filter(id=self.request.session['ACL_gid'],
                                   permissions__codename='view_page').count():
       return {'can_view_page': 1}
    return {'can_view_page': 0 }

Save this file somewhere; 将此文件保存在某处; ideally inside an app (the same place where you have the models.py file) and then add it to your TEMPLATES setting , make sure you don't override the defaults. 理想情况下,是在一个应用程序内(与您拥有models.py文件的位置相同),然后将其添加到TEMPLATES设置中 ,确保您不覆盖默认值。

Once you set this up, then all your templates will have a {{ can_view_page }} variable; 设置好之后,所有模板都将具有一个{{ can_view_page }}变量; and you don't have to keep repeating code in your views. 而且您不必在视图中重复代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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