gpt4 book ai didi

python - 子类化基本 Django 通用 View 时缺少 META 属性?

转载 作者:太空宇宙 更新时间:2023-11-03 19:05:47 24 4
gpt4 key购买 nike

我正在尝试对 Django 提供的基本通用 View 对象进行子类化,以便我可以完全控制 View 的渲染,但仍然使用更清晰的基于类的 View 方法,而不是映射到函数。

这是我到目前为止的观点:

from django.views.generic.base import View
from django.shortcuts import render
from account.forms import UserForm, UserProfileForm

class RegisterView(View):
def get(request, *args, **kwargs):

user_form = UserForm()
profile_form = UserProfileForm()

return render(request, 'account/register.html', {'user_form': user_form, 'profile_form': profile_form})

def post(request, *args, **kwargs):
pass

当我尝试导航到此 View 的 URL 时,我从 Django 收到此错误:

AttributeError at /account/register/

'RegisterView' object has no attribute 'META'

Request Method: GET
Request URL: http://localhost:8000/account/register/
Django Version: 1.4.3
Exception Type: AttributeError
Exception Value: 'RegisterView' object has no attribute 'META'

Exception Location: C:\Python27\lib\site-packages\django\core\context_processors.py in debug, line 35
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3

Environment:


Request Method: GET
Request URL: http://localhost:8000/account/register/

Django Version: 1.4.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'account')

Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
48. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
69. return handler(request, *args, **kwargs)
File "C:\project\account\views.py" in get
49. return render(request, 'account/register.html', {'user_form': user_form, 'profile_form': profile_form})
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in render
40. context_instance = RequestContext(request, current_app=current_app)
File "C:\Python27\lib\site-packages\django\template\context.py" in __init__
176. self.update(processor(request))
File "C:\Python27\lib\site-packages\django\core\context_processors.py" in debug
35. if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:

Exception Type: AttributeError at /account/register/
Exception Value: 'RegisterView' object has no attribute 'META'

在对通用 View 进行子类化时,文档没有指定任何有关任何“META 属性”的信息,因此我不确定我做错了什么,或者这是否是基本通用 View 的允许使用。

我对(严肃的)Python 编程和 Django 有点陌生,所以如果我遗漏了一些明显的内容,请原谅我。

最佳答案

记住这是一个类:您错过了 getpost 定义中的 self 参数:

def get(self, request, *args, **kwargs):

关于python - 子类化基本 Django 通用 View 时缺少 META 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com