English 中文(简体)
什么是检验?
原标题:What does test_func and view_func do in Python / Django?

我试图冲破Django的以下法典,以说明其做些什么,并在必要时加以ed弄,但我可以清楚地说明其中几个职能正在做些什么,或者它们从哪里恢复。

是否测试——func,并且认为——func Django是特定的,还是这些建筑职能?

Conclusion: I m not sure how/why I overlooked the fact that these were simply being defined as arguments to the functions. I need to start paying better attention to details.

这里的Django功能I m试图打破/数字:

def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.
    """

    def decorator(view_func):
        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            print test_func
            if test_func(request.user):
                return view_func(request, *args, **kwargs)
            path = request.build_absolute_uri()
            # If the login url is the same scheme and net location then just
            # use the path as the "next" url.
            login_scheme, login_netloc = urlparse.urlparse(login_url or
                                                        settings.LOGIN_URL)[:2]
            current_scheme, current_netloc = urlparse.urlparse(path)[:2]
            if ((not login_scheme or login_scheme == current_scheme) and
                (not login_netloc or login_netloc == current_netloc)):
                path = request.get_full_path()
            from django.contrib.auth.views import redirect_to_login
            return redirect_to_login(path, login_url, redirect_field_name)
        return _wrapped_view
    return decorator
最佳回答

Django view.test_func is a function “theeck that theuserpass the given test”。

因此,你写了要求用户提供某种物品的职能,如果通过,请上。 然后,你将这一功能通过到<条码>用户_passes_test上,并创设了一个指导器,在用户看到其意见之前,可以首先测试用户。

@user_passes_test
def test_intelligence(user):
    if is_intelligent:
        return True
    else:
        return False

@test_intelligence
def my_view(request):
    #this is the view you only want intelligent people to see
    pass

http://docs.python.org/fer/compound_stmts.html#Function"rel=“nofollow” <代码>wraps 是通过评分程序保存所包装功能(名称、动力等)的标记。 位于in functools

问题回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签