当我尝试使用django 1.9时,我遇到了 django-comments-xtd 的问题。我正在关注this教程。当我使用Django 1.8时,一切都很好。但是,如果我使用Django 1.9,我会在发送消息时收到错误消息。
TypeError at /comments/post/
get_comment_create_data() got an unexpected keyword argument 'site_id'
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/comments/post/
Django Version: 1.9.4
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'pagedown',
'crispy_forms',
'markdown_deux',
'django_comments',
'django_comments_xtd',
'blog']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django/views/decorators/http.py" in inner
42. return func(request, *args, **kwargs)
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django_comments/views/comments.py" in post_comment
108. comment = form.get_comment_object(site_id=get_current_site(request).id)
File "/home/olga/.virtualenvs/my_site/lib/python3.4/site-packages/django_comments/forms.py" in get_comment_object
121. new = CommentModel(**self.get_comment_create_data(site_id=site_id))
Exception Type: TypeError at /comments/post/
Exception Value: get_comment_create_data() got an unexpected keyword argument 'site_id'
我在settings.py中设置了SITE_ID
理论上,django-comments-xtd与django 1.9兼容
当我使用自己的项目(dj 1.9)时,我遇到了同样的问题。甚至当我运行example(dj 1.9)作为教程时。一切都很好,直到我发送消息。
以下是我自己的应用的 forms.py 。
from django import forms
from pagedown.widgets import PagedownWidget
from .models import Post
class PostForm(forms.ModelForm):
content = forms.CharField(widget=PagedownWidget(show_preview=False))
publish = forms.DateField(widget=forms.SelectDateWidget())
class Meta:
model = Post
fields = ['title', 'content', 'image']
from django.utils.translation import ugettext_lazy as _
from django_comments_xtd.forms import XtdCommentForm
class MyCommentForm(XtdCommentForm):
def __init__(self, *args, **kwargs):
if 'comment' in kwargs:
followup_suffix = ('_%d' % kwargs['comment'].pk)
else:
followup_suffix = ''
super(MyCommentForm, self).__init__(*args, **kwargs)
for field_name, field_obj in self.fields.items():
if field_name == 'followup':
field_obj.widget.attrs['id'] = 'id_followup%s' % followup_suffix
continue
field_obj.widget.attrs.update({'class': 'form-control'})
if field_name == 'comment':
field_obj.widget.attrs.pop('cols')
field_obj.widget.attrs.pop('rows')
field_obj.widget.attrs['placeholder'] = _('Your comment')
field_obj.widget.attrs['style'] = "font-size: 1.1em"
if field_name == 'url':
field_obj.help_text = _('Optional')
self.fields.move_to_end('comment', last=False)
我的应用urls.py
from django.conf.urls import url
from django.contrib import admin
from .views import (
post_create,
post_detail,
post_list,
post_update,
post_delete
)
urlpatterns = [
url(r'^create/$', post_create, name='create'),
url(r'^$', post_list, name='list'),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', post_delete),
]
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^comments/', include('django_comments_xtd.urls')),
url(r'^', include("blog.urls", namespace ="blog")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:0)
我猜that link可以帮到你。我有同样的错误,当我将** kwargs添加到自定义get_comment_create_data()方法时它就消失了,正如claudep所说。