2013-02-07 49 views
1

我想要繼承Django提供的基本通用視圖對象,這樣我就可以完全控制視圖的呈現,但仍然使用基於清潔器的基於類的視圖方法而不是映射到函數。子類化基本Django泛型視圖時缺少META屬性?

這是迄今爲止我的觀點:

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 

當我嘗試導航到的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' 

文檔沒有指定任何關於任何「META屬性」的子類化通用視圖時,所以我不知道我在做什麼錯誤,或者如果這是基礎通用視圖的允許使用。

我對(嚴重的)Python編程和Django有點新,所以請原諒我,如果我失去了明顯的東西。

回答

1

記住,這是一類:你已經錯過了self論點的getpost的定義:

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

人,這是明顯的....謝謝! – Alex