作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
打开“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'),
'®ion=',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'),
'®ion=',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/
我对 airBNB 搜索页面非常感兴趣,我试图在 AirBNB Search Page 上摆弄 firebug 上的代码,看看它是如何工作的。 在右上角,用户可以在他们希望显示结果的列表、照片和 ma
打开“result_list.html”时出现此错误,请帮助纠正我的代码。逻辑是当用户提交表单时,它将重定向到此结果 html。但现在我只要单独打开这个“result_list.html”,就会弹出这
我是一名优秀的程序员,十分优秀!