gpt4 book ai didi

python - django 1.8 -'ResultView' 对象没有属性 'method'

转载 作者:行者123 更新时间:2023-11-30 23:05:07 25 4
gpt4 key购买 nike

打开“result_list.html”时出现此错误,请帮助纠正我的代码。逻辑是当用户提交表单时,它将重定向到此结果 html。但现在我只要单独打开这个“result_list.html”,就会弹出这个错误。

网址

urlpatterns = patterns('',
url(r'^result_list/$',ResultView.as_view(),name='result'),
url(r'^input/$',InputFormView.as_view(),name='input'),
)

views.py

class InputFormView(FormView):
template_name = 'inputform.html'
form_class = InputForm

#redirect to result page with submitted form information

def get_success_url(self):
return ''.join(
[
reverse('result'), ----redirect to 'result' page
'?company=',self.request.POST.get('company'),
'&region=',self.request.POST.get('region')
]
)

class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result

def get_context_data(self,**kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))

return context

def get_queryset(request):
if request.method == 'POST':
form = InputForm(request.POST)
if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']

queryset=Result.objects.filter(region=region)
return get_context_data(queryset,request)

else:
print form.errors()
return Result.objects.all()

inputform.HTML 片段

<form method="post" action="{% url 'result' %}">  ----if here correct?
{% csrf_token %}

<!--enter company-->
<div class="field">
{{ form.company.errors }}
<label for="{{ form.company.id_for_label }}">Company:</label>
{{ form.company }}
</div>

<!--select region-->
<div class="field" >
<label> Select the Region:
{{ form.region }}
{% for region in form.region.choices %}
<option value="region" name= "region" id="id_region">{{region}} </option>
{% endfor %}
</label>
</div>

.......

**结果列表.html

<div class="basicinfo">         <!--form information-->
<table border="1" cellpadding="1">
<tr>
<td align="left">Company</td>
<td>{{company }}</td>
</tr>
<tr>
<td align="left">Region</td>
<td>{{region}}</td>
</tr>
</table>
</div>

<div class="result">
<table style border="0" cellpadding="0">
<tr> {{sales}}</tr>
</table>
.....
</div>

回溯:

File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\list.py" in get
159. self.object_list = self.get_queryset()
File "C:\Users\user\Desktop\ss\result\views.py" in get_queryset
91. if request.method == 'POST':

Exception Type: AttributeError at /result_list/
Exception Value: 'ResultView' object has no attribute 'method'

新的回溯

File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\list.py" in get
174. context = self.get_context_data()

Exception Type: TypeError at /dupont_list/
Exception Value: get_context_data() takes exactly 2 arguments (1 given)

基于建议的新views.py

class InputFormView(FormView):
template_name = 'inputform.html'
form_class = InputForm

#redirect to result page with submitted form information

def get_success_url(self):
return ''.join(
[
reverse('result'), ----redirect to 'result' page
'?company=',self.request.POST.get('company'),
'&region=',self.request.POST.get('region')
]
)

class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result

def get_context_data(self,**kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))

return context

def get_queryset(self):
if self.request.method == 'POST':
form = InputForm(self.request.POST)
if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']

queryset=Result.objects.filter(region=region)

else:
print form.errors()
return Result.objects.all()

最佳答案

get_queryset 方法仅接受 self 参数。如果你想访问request,你必须写self.request。

def get_queryset(self):
if self.request.method == 'POST':
form = InputForm(self.request.POST)
if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']

queryset=Result.objects.filter(region=region)
return queryset

else:
print form.errors()
return Result.objects.all()

关于python - django 1.8 -'ResultView' 对象没有属性 'method',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33342639/

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