Home > database >  Django Model Form not saving data
Django Model Form not saving data

Time:03-30

I am working on Django application and I am trying to create form for users' suggestions and comments. I want to see that suggestions/comments in database (admin interface). After the comment is submitted, it is not saved in database. I have some mistake here but I don't know where it is. Can you please help me?

views.py:

def komentariPrijedlozi(request):
    if request.method == 'POST':
        form = CommentsSuggestionsForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'rjecnik/successCommentsSuggestions.html')
    form = CommentsSuggestionsForm()
    context = {'form' : form}
    return render(request, 'rjecnik/komentariPrijedlozi.html', context)

komentariPrijedlozi.html:

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Comments and suggestions</title>
</head>

<body>
  <section  style=" margin-top: 50px; background-color: lightgray;">
    
      <div >
      <div  style="font-size: 21px; margin-bottom: 20px;">Komentari i prijedlozi novih prijevoda:</div>
        <div >

          
          <form method="POST" autocomplete="off" enctype="multipart/form-data">
    <tr>
        <th>
          <label for="suggestion1">Prijedlog novog pojma:</label>
        </th>
        <td >
           <input type="text" name="suggestion1" maxlength="100" autocomplete="off" style="width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block;">
        </td>
    </tr>
    <tr>
        <th>
          <label for="suggestion2">Prijedlog prijevoda:</label>
        </th>
        <td>
          <input type="text" name="suggestion2" maxlength="100" autocomplete="off" style="width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block;">
            
        </td>
    </tr>
    <tr>
        <th>
            <label for="comment">Komentar:</label>
        </th>
        <td>
            <textarea name="comment" cols="40" rows="10" style="width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block;"></textarea>
        </td>
    </tr>

          {% csrf_token %}
          <input type="submit"  value="Submit">
       
         </form>
        </div>
      </div>
    </div>
  </section>
</body>

</html>

models.py:

 class CommentsSuggestions(models.Model):
    suggestion_word = models.CharField(max_length=100)
    suggestion_translation = models.CharField(max_length=100)
    comment_suggestion = models.TextField()

forms.py:

class CommentsSuggestionsForm(ModelForm):
class Meta:
    model = CommentsSuggestions
    fields = '__all__'

urls.py:

path('komentari_i_prijedlozi/', views.komentariPrijedlozi, name="komentariPrijedlozi"),

CodePudding user response:

For as far as I can see you are not using your custom form in your template, change the form in your template to this:

<form method="POST" autocomplete="off" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_table }}
    <input type="submit"  value="Submit">
</form>

Then the values will be processed correctly when arriving in your views.py.

  • Related