Django AttributeError: 'function' object has no attribute 'get'

Asked 2 years ago, Updated 2 years ago, 73 views

In django routing, I would like to specify a unique function in path(), where I receive a request, add processing, and then invoke class view.
If you access url from the following source, you will get an error.
What I want to do is to do class view.as_view() in the function while receiving a request from another function.
Please help me.

 from django.urls import path
from django.contrib.auth.decorators import login_required
from.import views

app_name = 'test'

defaaa(request):
  if request.user.i == 0:
        return 'url'
    else:
        return views.TestListView.as_view() ← "This is where the error happen."

urlpatterns = [
    path(', aaa, name='test'),
]
Internal Server Error:/alcool/
Traceback (most recent call last):
  File"/Library/Frameworks/Python.frameworks/Version/3.8/lib/python 3.8/site-packages/django/core/handlers/exception.py", line 47, inner
    response=get_response(request)
  File"/Library/Frameworks/Python.frameworks/Version/3.8/lib/python 3.8/site-packages/django/utils/deprecation.py", line 116, in__call__
    response=self.process_response(request, response)
  File"/Library/Frameworks/Python.framework/Version/3.8/lib/python 3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'function' object has no attribute 'get'

python django

2022-09-30 17:28

1 Answers

The views.TestListView.as_view() return value is a function.You must call it with a request as an argument.In other words,

return views.TestListView.as_view()(request)

It would be good to say that


2022-09-30 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.