Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:04:01+00:00 2026-06-01T14:04:01+00:00

[edit] So it seems my code works fine, another piece of code + tiredness

  • 0

[edit]
So it seems my code works fine, another piece of code + tiredness is the issue
[/edit].

I have a decorator which does a simple check for a couple of required request keys.

def fields_required(*fields):
assert isinstance(fields, tuple), "Fields must be of type tuple."

def wrap_func(fn):

    def wrapper(cls, request, *args, **kwargs):
        print 'oh hi'
        missing_fields = []
        for field in fields:
            if not request.REQUEST.has_key(field):
                missing_fields.append(field)

        if len(missing_fields) > 0:
            #maybe do smth here
            return HttpResponseBadRequest()          

        return fn(cls, request, *args, **kwargs)
    return wrapper
return wrap_func

I expect a HTTP 403 Bad Request status code if one of the fields is missing, however the decorator never executes that code.

A basic representation of my view file:

class ViewA(View):

    @fields_required('name','api_key')
    def get(self, request, *args, **kwargs):
        # some logic

class ViewB(View):

    @fields_required('SHOULD_NEVER_SEE','THIS_STUFF')
    def get(self, request, *args, **kwargs):
        # some logic

When opening ViewA in the browser, the console output is as following:

('name', 'api_key')
('SHOULD_NEVER_SEE','THIS_STUFF')

I cannot understand why the decorator for ViewB is executed, and why there is no ‘oh hi’ in my console. Any insights?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:04:02+00:00Added an answer on June 1, 2026 at 2:04 pm

    The decorator for ViewB is “executed” but not because you’re viewing ViewA. It’s because Python decorates the method when it executes the file itself. For example, the following prints b even though func is not called:

    def deco(f):
        print 'b'
        def g():
            print 'c'
        return g
    
    @deco
    def func():
        print 'a'
    

    Regarding the issue that ‘oh hi’ is not printed; could you try adding the decorator to dispatch instead of get (i.e., add the following to your views):

    @method_decorator(fields_required('SHOULD_NEVER_SEE','THIS_STUFF'))
    def dispatch(self, *args, **kwargs):
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like such a simple question. I have several Edit boxes on my
Ok, so this piece of code works fine in the Debug but not in
Which algorithms or data structures are used in auto-suggest features? It seems that edit-distance
The following block of code works fine (no errors) $query = select * from
EDIT: This seems to be a problem with IE exclusively. Very simple to re-create,
I've stress tested the code below and it seems to work fine - what
Seems like everyone have issue accessing local machine or internet etc from emulator. All
I am wondering why this code works fine and comiler doesnt generate any errors
The below code works fine in both Chrome and IE $.get(../ajax/BTBookAppointment.aspx?dsl= + telNumberBox.value +
EDIT: It seems to be something with having the two queues in the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.