简单的留言簿django:__init __()接受1个位置参数,但给定2个


75

我是Django的新手,正在尝试制作一个简单的留言簿应用程序以适应环境。我收到以下错误,但找不到错误:

异常值:_ init _()接受1个位置参数,但给出了2个。

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

class Bericht(models.Model):
    titel = models.CharField(max_length=50)
    auteur = models.ForeignKey(User, blank=True)
    email = models.EmailField(max_length=75)
    inhoud = models.TextField(max_length=10000, blank=True)
    datum = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.auteur) + " : " + str(self.titel)

    class Meta:
        verbose_name_plural = "berichten"

class BerichtAdmin(admin.ModelAdmin):
    list_display = ["auteur", "datum", "titel"]
    list_filter = ["datum", "auteur"]

admin.site.register(Bericht, BerichtAdmin)

风景

from django.shortcuts import render
from django.views.generic import ListView
from Gastenboek.models import *

class BerichtListView(ListView):
    model = Bericht.objects.all()
    template_name = 'template/bericht_lijst.html'
    paginate_by = 10
    context_object_name = "bericht_lijst"
# Create your views here.

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'Niels.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    (r"^(\d+)/$", 'Gastenboek.views.BerichtListView'),
    (r"", 'Gastenboek.views.BerichtListView'),
)

追溯

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.1
Python Version: 3.3.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Gastenboek')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /
Exception Value: __init__() takes 1 positional argument but 2 were given

1
在此处粘贴完整的错误消息。
Stefano Sanfilippo,2014年

5
我想从模型= Bericht.objects.all()你的问题,它应该是模型= Bericht或网址不添加.as_view()
莫哈末

1
问题不在您在此处显示的任何代码中。您应该始终粘贴完整的追溯。我怀疑问题出在您的urls.py中。
丹尼尔·罗斯曼

看看:stackoverflow.com/questions/16835651/typeerror-for-init / ...问题可能非常相似。
用户

谢谢大家,我根据要求更新了一些信息。
user2583429 2014年

Answers:


214

在您的urls.py中:

您缺少.as_view()

更改为:

(r"^(\d+)/$", Gastenboek.views.BerichtListView.as_view()),
(r"", Gastenboek.views.BerichtListView.as_view()),

现在我收到一条错误消息,说未定义Gastenboek
user2583429

当我这样做时:从Gastenboek import *,它说没有名为site的模块。
user2583429 2014年

1
您不应该使用通配符...尝试导入Gastenboek.views.BerichtListView
Alvaro

4
@Alvaro,您为我省了很多麻烦,谢谢!
GED125

@ GED125很高兴这仍然有用:)
Alvaro
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.