【问题标题】:How to solve __init__() takes 1 positional argument but 2 were given如何解决 __init__() 需要 1 个位置参数,但给出了 2 个
【发布时间】:2020-01-19 10:48:00
【问题描述】:

views.py

from django.views.generic import ListView
from django.contrib.auth.models import User
class UserListView(ListView):
    model = User
    template_name = 'infinitescroll/articles.html' 
    context_object_name = 'users' 
    paginate_by = 10
    queryset = User.objects.all()  

urls.py

from django.contrib import admin
from django.urls import path
from infinitescroll.views import UserListView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/',UserListView, name='home'),
]

articles.html

<table class="table table-bordered">
  <thead>
    <tr>
      <th>Username</th>
      <th>First name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    {% for user in users %}
      <tr>
        <td>{{ user.username }}</td>
        <td>{{ user.first_name }}</td>
        <td>{{ user.email }}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

我不知道是什么导致了错误,它在异常中给出了错误位置

位置:C:\xampp\htdocs\8moviesdb\infinite\pal\lib\site-packages\django\core\handlers\base.py 在 _get_response,第 113 行

【问题讨论】:

  • 分享错误的完整回溯!

标签: django django-rest-framework django-views


【解决方案1】:

在 urls.py 中,使用 UserListView.as_view() 而不是 UserListView。这就是 Django 基于类的视图的工作方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-30
    • 2017-04-22
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多