django 1.8 -'ResultView'对象没有属性'方法'

时间:2015-10-26 09:45:58

标签: python django

打开“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>

  .......

** result_list.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()

1 个答案:

答案 0 :(得分:3)

get_queryset方法只接受self参数。如果您想获得请求,您必须编写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()