Search code examples
djangocreate-view

Restrict access to Generic views


i've a creteView (a generic view then) that creates a form where user can put data and create new object.

now, i would like to limit the access to this page only when a function of mine returns true. This function does not takes into consideration the user, so decorator on user permission would not work. the function

is_allowed()

does some logic on the day and time of the day.

how can i restrict the generic view on this function? where should i put the control?

for a normal view it's easy:

if is_allowed():

so what i thought was to do something like this (where View1 is the creteView)

def Home(request):
    if is_allowed():
        return View1.as_view()

doing so i receive this message

'function' object has no attribute 'has_header'

what can i do?


Solution

  • There's documentation on decorating generic views.

    You can also override various functions from the View base classes to add your checks. For example, you might override dispatch.