嗨,这应该是一个简单的我确定。只是失去了对基于类的观点的耐心。不确定他们如何简化事情。基于函数的视图似乎更具可读性并且更有意义。无论如何我试图将我的一个功能视图转换为一个类,并在我请求视图时收到此错误
'tuple'对象没有属性'get'
PS我正在使用django 1.8.1,我导入View的方式是通过django.views.generic.View而不是django.views.View,如文档中所述。可能是相关的。
这是urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^upload_photo/$', views.upload_photo, name='upload_photo'),
url(r'^upload_photo_class/$', views.UploadPhotoView.as_view()),
]
以下是基于类的视图
class UploadPhotoView(View):
form = PhotoModelForm
formset = formset_factory(PhotoExtendedModelForm, max_num=1, validate_min=True)
template_name = 'upload_photo.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get(self, request):
form = self.form
formset = self.formset
return (request, self.template_name, {'form': form,
'formset': formset})
def post(self, request, *args, **kwargs):
form = self.form(request.POST, request.FILES)
formset = self.formset(request.POST)
if all([form.is_valid(), formset.is_valid()]):
photo = form.save()
photo_instance = photo
user_id = request.user
for inline_form in formset:
data = inline_form.save(commit=False)
data.Photo = photo_instance
data.user = user_id
data.save()
return render(request, 'upload_photo_done.html')
else:
return (request, self.template_name, {'form': form,
'formset': formset})
这是基于功能的原始视图
@login_required
def upload_photo(request):
form = PhotoModelForm
formset = formset_factory(PhotoExtendedModelForm, max_num=1, validate_min=True)
if request.method == 'POST':
form = form(request.POST, request.FILES)
formset = formset(request.POST)
if all([form.is_valid(), formset.is_valid()]):
photo = form.save()
photo_instance = photo
user_id = request.user
for inline_form in formset:
data = inline_form.save(commit=False)
data.Photo = photo_instance
data.user = user_id
data.save()
return render(request, 'upload_photo_done.html')
else:
return render(request, 'upload_photo.html', {'form': form, 'formset': formset})
else:
return render(request, 'upload_photo.html', {'form': form, 'formset': formset})
继承人追溯:
Request Method: GET
Request URL: http://127.0.0.1:8000/otologue/upload_photo_class/
Django Version: 1.8.1
Python Version: 3.4.4
Installed Applications:
('account',
'photologue_custom',
'otologue',
'photologue',
'sortedm2m',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "../venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
223. response = middleware_method(request, response)
File "../venv/lib/python3.4/site-packages/django/middleware/clickjacking.py" in process_response
31. if response.get('X-Frame-Options', None) is not None:
Exception Type: AttributeError at /otologue/upload_photo_class/
Exception Value: 'tuple' object has no attribute 'get'
答案 0 :(得分:0)
在def get()中,你应该像这样返回一个 HttpResponse 对象
HttpResponseRedirect('/success/')
或使用render()重定向到模板。您正在尝试使用渲染函数调用参数返回一个普通元组。
答案 1 :(得分:0)
您希望继承having min(status = 'Y' then 1 else 0 end) = 1
而非FormView
。因此,您的第一行将是view
它将简化大量的表单处理和样板,这是基于类的视图的很多好处。像往常一样,Django documentation on this is很棒。
在您的情况下,可能就像:
一样简单class UploadPhotoView(FormView):