Tuesday, July 28, 2015

Re: Using FormMixin with DetailView

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

On Jul 28, 2015 1:39 AM, "Ioannis Latousakis" <latusaki@gmail.com> wrote:
Hello James,

The reason I am following this approach is because this is what was suggested in the documentation, for the scenario where you need to have a form within a DetailsView. Although even when I got my code to be identical to the documentation example, I still dont get an object created. Still, on form submit, the method get_success_url() of PostComment formview is called.  I am currently using django 1.7. 

On Tuesday, 28 July 2015 11:32:13 UTC+3, James Schneider wrote:

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

The override is this, I was experimenting with save..


class CommentForm(forms.ModelForm):
   
class Meta:
        model
= Comment
        exclude
= ("parent_post","created_at")


    def create_view(request, **kwargs):
        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'))


--
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/6825b838-584c-4e89-bc4e-4a6d0bf81c5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciWMvDuH8M4tYFe5HA4h8mefvP7SBSfHDF9GGO952rF3jw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment