__init__() takes exactly 1 argument (3 given)
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/post/5144752345317376/ |
Django Version: | 1.6.11 |
Exception Type: | TypeError |
Exception Value: | __init__() takes exactly 1 argument (3 given) |
Exception Location: | ...app/sitepackages/django/core/handlers/base.py in get_response, line 11 |
class PostView(BlogMixin,DetailView):
""" A view for displaying a single post """
template_name = 'post.html'
model = Post
def get_context_data(self, **kwargs):
context = super(PostView, self).get_context_data(**kwargs)
context['form'] = CommentForm()
return context
class PostDetailView(View):
print "HERE ------------ "
def get(self, request, *args, **kwargs):
print "GET ------------ "
view = PostView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
print "POST ------------ "
view = PostComment.as_view()
return view(request, *args, **kwargs)
class PostComment( SingleObjectMixin , FormView):
template_name = 'post.html'
form_class = CommentForm
model = Post
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super(PostComment, self).post(request, *args, **kwargs)
def get_success_url(self):
return reverse('post-detail', kwargs={'pk': self.object.pk})
class BlogMixin(object): """ Basic mixin for all the views. Update the context with additional information that is required across the whole site, typically to render base.html properly """ def get_context_data(self, *args, **kwargs): context = super(BlogMixin, self).get_context_data(*args, **kwargs) blog = Blog.get_unique() context.update({ 'blog': blog, 'active_user': users.get_current_user(), 'is_admin': users.is_current_user_admin() }) return context
urls.py:
url(r'^post/(?P<pk>[\d]+)/$', views.PostDetailView., name="post-detail"),
Hello,
could you show us the relevant part of your urls.py file? I suspect view parameters to cause the trouble but I'd like to be sure.
Best,
Gergely
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/299d4e5e-0b69-4627-a1e6-e7f2d1ff9d8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Forgot to add, the full backtrace would be helpful, too. It should be present on the same error page.
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
exclude = ("parent_post","created_at")
def save():
print "IN VIEW"
if request.method == "POST":
parent_fk = self.object
print "PARRENT"
print parent_fk
form = CommentForm(request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.parent_post = parent_fk
new_comment.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
class PostView(DetailView):
""" A view for displaying a single post """
template_name = 'post.html'
model = Post
def get_context_data(self, **kwargs):
context = super(PostView, self).get_context_data(**kwargs)
context['form'] = CommentForm()
return context
class PostDetailView(View):
def get(self, request, *args, **kwargs):
view = PostView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = PostComment.as_view()
return view(request, *args, **kwargs)
class PostComment( SingleObjectMixin , FormView):
template_name = 'post.html'
form_class = CommentForm
model = Post
def post(self, request, *args, **kwargs):
print "POST ----"
self.object = self.get_object()
return super(PostComment, self).post(request, *args, **kwargs)
def get_success_url(self):
return reverse('post-detail', kwargs={'pk': self.object.pk})
url(r'^post/(?P<pk>[\d]+)/$', views.PostDetailView.as_view(), name="post-detail"),
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
exclude = ("parent_post","created_at")
def create_view(request, **kwargs):
Is there a specific reason you aren't just using CreateView rather than trying to mix in a bunch of classes and trying to roll the post() logic yourself? You can probably cut a very large portion of code out and make this super simple.
https://docs.djangoproject.com/en/1.6/topics/class-based-views/generic-editing/#model-forms
You should also consider upgrading Django immediately to a newer version, as 1.6 is no longer supported.
-James
Are you sure you're using 1.7? Your original error posting begs to differ:
Django Version:1.6.11
The particular portion of docs that you are reading is meant to show how various mixin's can be used together to address specific edge cases, but other generic classes exist that are much better suited for what you are trying to do. Follow the link I posted to see examples.
The only 'tricky' portion of what you are trying to do is to attach the Comment to the Post, which is easily handled by overriding the form_valid() method in a CreateView.
You may not even need to define a Model Form.
- James
I've reviewed your code a bit more and I see what you're trying to do, I think.
With that being said, I'll recant what I said about the CreateView.
Your trouble stems from the fact that FormView by default does not perform a form.save() operation anywhere (it only validates the form). CreateView does this in the form_valid() method.
I'm not sure what you are doing with the create_view() method in the form, but it looks like a pseudo-save method. You probably don't need it, but that chunk off code looks like it actually belongs in form_valid() on the view, not on the form.
Look into overriding form_valid() in your view. The example you were/are looking at is missing the save() functionality, and it states so at the beginning of the section.
"Again, let’s assume we’re not going to store this in a relational database but instead in something more esoteric that we won’t worry about here."
I think that the docs are a bit misleading here, since I would expect a large section like this to at least make reference to something like "#save code here". Maybe it is in there, hard to tell on my phone.
- James
class PostDetailView(BlogMixin,CreateView):
""" A view for displaying a single post """
template_name = 'post.html'
model = Comment
fields = ['body','author_name']
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['post'] = Post.objects.get(pk=self.kwargs['pk'])
return context
def form_valid(self, form):
obj = form.save(commit=False)
obj.parent_post = Post.objects.get(pk=self.kwargs['pk'])
obj.save()
return redirect('post-detail', self.kwargs['pk'])