我想在同一屏幕上创建listview和createview,但是我不理解当前错误“'group_list'对象没有属性'object'”的含义。
我试图在createview的基础上创建它,但是列表很混乱。 我还尝试创建和继承createview和listview类,但是发生了相同的错误。
#error
'group_list' object has no attribute 'object'
#forms
class GroupRequestForm(mixins.BaseModelForm):
class Meta:
model = belong
fields = ('group',)
#view
class group_list(ListView,ModelFormMixin):
model = group
form_class = GroupRequestForm
template_name = 'group/group_list.html'
context_object_name = 'group_list'
queryset = group.objects.all()
def get(self, request, *args, **kwargs):
self.object = None
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = None
self.object_list = self.get_queryset()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
# class group_list(CreateView,):
# form_class = GroupRequestForm
# template_name = 'group/group_list.html'
# success_url = reverse_lazy('group:group_list')
#
# def get_context_data(self, *, object_list=None, **kwargs):
# context_data = super(group_list, self).get_context_data(**kwargs)
# context_data['group_list'] = group.objects.all()
# return context_data
#
# def form_valid(self,form):
# user = self.request.user
# # POST.get('user_id')
# form.instance.user = user
# return super().form_valid(form)
# class group_list(ListView):
# model = group
# context_object_name = 'group_list'
# class group_request(CreateView):
# model = models.belong
# form_class = GroupRequestForm
# emplate_name = 'group/group_list.html'
# success_url = reverse_lazy('group:group_list')
#
# def post(self, request, *args, **kwargs):
# self.object = self.get_object() # assign the object to the view
# form = self.get_form()
# if form.is_valid():
# email = form.cleaned_data.get("email")
# return self.form_valid(form)
# else:
# return self.form_invalid(form)
#
# class GroupListRequest(group_list,group_request):
# def get(self, request, *args, **kwargs):
#
# formView = group_request.get(self, request, *args, **kwargs)
# listView = group_list.get(self, request, *args, **kwargs)
# formData = formView.context_data['form']
# listData = listView.context_data['group_list']
# context = {'form': formData, 'group_list': listData}
# return render(request, 'group/group_list.html', context)
我想在同一屏幕上创建listview和createview。
后记
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/list.py", line 157, in get
context = self.get_context_data()
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/list.py", line 136, in get_context_data
return super().get_context_data(**context)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/edit.py", line 67, in get_context_data
return super().get_context_data(**kwargs)
File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/views/generic/detail.py", line 94, in get_context_data
if self.object:
AttributeError: 'group_list' object has no attribute 'object'
#urls
app_name = 'groups'
urlpatterns = [
path('',views.group_list.as_view(),name='group_list'),
]
答案 0 :(得分:0)
我所指的网站的一部分是错误的,而我更正后该错误消失了。 非常感谢。