简体   繁体   中英

Django 2.1.7 How to use a decorator on a package view

I would like to know how to use a decorator on a function view.py which do not find it directly in my application, but in a pakage (venv / lib / ...)

For more details, I'm going to build an application that uses django-allauth. I create a decorator that asks the user to confirm their password before accessing certain page.

All goes well but I would like to use this constructor on the function of aullauth that can manage emails.

I do not know how to proceed...

enter image description here

Sorry for my english.

If it is a fucntion view you can do this

import package_view...

@decorator
def custom_view(request, *args, **kwargs):
    return package_view(request, *args, **kwargs)

If it is a CBV you can inherit from it and add a decorator to the dispatch method:

class CustomView(PackageView):
    @decorator
    def dispatch(request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

Hi @Pedro So here I have tried the solution for Class Base Views, but it did not work. As my knowledge is limited I probably did not target the right class ...

Nevertheless, I solved the problem by targeting the url and using the decorator on the targeted url

Here's what it looks like if it can help other people:

...
from allauth.account import views as allauth_views
from .decorators import confirm_password

urlpatterns = [
    re_path(r'^accounts/', include('allauth.urls')),
    re_path(r"^email/$", confirm_password(allauth_views.email), name="account_email"), 
    ...
]

Thank you very much, I am grateful for your help and time.

It is very likely that your solutions will help me in the future. :)

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