TypeError:get()接受2个位置参数但是3个被赋予

时间:2018-04-14 08:03:20

标签: python django

我正在尝试在我的页面上显示输入,并且一直在关注Max Goodridge的演练ep.45,但似乎已经让自己陷入了这个特定的部分。如果有人能提供帮助,这将是一个巨大的帮助。

我的urls.py:

 url(r'^bandlist/$', polls_views.bandlist, name='bandlist'),
 url(r'^bandlist/(\d+)/$', polls_views.BandView.as_view(), name='bandview'),

我的views.py

def bandlist(request):
    query = Band.objects.order_by('bandname')
    args =  {'query': query}
    return render(request, 'bandlist.html', args)

class BandView(TemplateView):
    template_name = 'bandview.html'

    def get(self, request):
        form = BandForm()
        return render(request, self.template_name, {'form': form})

我的forms.py

class BandForm(forms.Form):
    post = forms.CharField()  

模板(band.html)

{% extends 'bbase.html' %}


{% block content %}
<h1>{{ band.bandname }}</h1>

<h5>Total Ratings: {{ band.totalrating }}</h5>
<h5>How many times have {{band.bandname}} been rated: {{ band.totalrated }}</h5>
<h5>Average rating (Out of 5): </h5>

<form method="post">
    {{ form.as_p }}
    <input type="submit" value="Score" />
</form>
{% endblock %}

在此之后,我将尝试为每个乐队实施评级和评审系统。如果有人能就此提出建议,我将非常感激。

编辑:这是完整的追溯:

Internal Server Error: /bandlist/1/
Traceback (most recent call last):
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
TypeError: get() takes 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:3)

您的get()方法实现缺少参数。

首先检查TemplateView祖先班 https://docs.djangoproject.com/en/2.0/ref/class-based-views/base/#django.views.generic.base.TemplateView

放大以查看确切的get()方法实现 https://docs.djangoproject.com/en/2.0/ref/class-based-views/base/#django.views.generic.base.View

  def get(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')