Home > Software engineering >  Django how to add a form to a DetailView with FormMixin
Django how to add a form to a DetailView with FormMixin

Time:01-24

I am attempting to add a form for comments to a DetailView. The DetailView displays notes for specific projects. So the comments have a foreign key that is the specific note and the note has a foreign key for the specific project.

I am attempting to use FormMixin with DetailView. So far I have not bee successful. Currently I can get the form to display but it does not save and in the terminal I see the following error Method Not Allowed (POST): /projects/project/1/note/1/

I can get these to work separately but not with the form in the DetailView.

Here are my models:

class ProjectNotes(models.Model):
    title = models.CharField(max_length=200)
    body = tinymce_models.HTMLField()
    date = models.DateField(auto_now_add=True)
    project = models.ForeignKey(Project, default=0, blank=True, on_delete=models.CASCADE, related_name='notes')

    def __str__(self):
        return self.title
class ProjectNoteComments(models.Model):
    body = tinymce_models.HTMLField()
    date = models.DateField(auto_now_add=True)
    projectnote = models.ForeignKey(ProjectNotes, default=0, blank=True, on_delete=models.CASCADE, related_name='comments')

The View:

class ProjectNotesDetailView(DetailView, FormMixin):
    model = ProjectNotes
    id = ProjectNotes.objects.only('id')
    template_name = 'company_accounts/project_note_detail.html'
    comments = ProjectNotes.comments
    form_class = NoteCommentForm

    def form_valid(self, form):
        projectnote = get_object_or_404(ProjectNotes, id=self.kwargs.get('pk'))
        comment = form.save(commit=False)
        comment.projectnote = projectnote
        comment.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('project_detail', args=[self.kwargs.get('pk')])

The form:

class NoteCommentForm(forms.ModelForm):
    class Meta:
        model = ProjectNoteComments
        fields =['body',]

        widgets = {
            'body': forms.TextInput(attrs={'class': 'form-control'})
        }

The template:

% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block content %}
<div >
  <div >
    <h2>{{ projectnotes.title }}</h2>
    <p>{{ projectnotes.body | safe }}</p>
  </div>
  <div><b>Comments on {{projectnotes.title}}</b></div>
  {% if projectnotes.comments.all %}
    {% for comment in projectnotes.comments.all %}
        <div  style="padding: 10px;">
          
            {{ comment.body | safe }}
         
            
        </div>
      {% endfor %}
    {% else %}
    <p>No comments have been have been added yet.</p>
    {% endif %}
  
  <h2><a href="">add note</a></h2>
  
  <h1>Add Comment</h1>
  
   <form action="" method="post">
    {% csrf_token %}
    {{ form.media }}
    {{ form|crispy }}
    <input type="submit" value="save">
   </form>
  
{% endblock content %}

CodePudding user response:

Try to change the order between DetailView and FormMixin in ProjectNotesDetailView then implement the post method (enabled by the `FormMixin):

class ProjectNotesDetailView(FormMixin, DetailView):
    model = ProjectNotes
    id = ProjectNotes.objects.only('id')
    template_name = 'company_accounts/project_note_detail.html'
    comments = ProjectNotes.comments
    form_class = NoteCommentForm

    def form_valid(self, form):
        projectnote = get_object_or_404(ProjectNotes, id=self.kwargs.get('pk'))
        comment = form.save(commit=False)
        comment.projectnote = projectnote
        comment.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('project_detail', args=[self.kwargs.get('pk')])



    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return HttpResponseForbidden()
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

Check how to use formmixin with detailview (documentation).

  •  Tags:  
  • Related