我正在開發 Django 應用程式,我正在嘗試為用戶的建議和評論創建表單。我想在資料庫(管理界面)中看到建議/評論。評論提交后,不保存在資料庫中。我這里有一些錯誤,但我不知道它在哪里。你能幫我么?
視圖.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 class="main-container1" style=" margin-top: 50px; background-color: lightgray;">
<div class="columns">
<div class="main-par" style="font-size: 21px; margin-bottom: 20px;">Komentari i prijedlozi novih prijevoda:</div>
<div class="column is-half is-offset-one-quarter">
<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" class="btn-sub" value="Submit">
</form>
</div>
</div>
</div>
</section>
</body>
</html>
模型.py:
class CommentsSuggestions(models.Model):
suggestion_word = models.CharField(max_length=100)
suggestion_translation = models.CharField(max_length=100)
comment_suggestion = models.TextField()
表格.py:
class CommentsSuggestionsForm(ModelForm):
class Meta:
model = CommentsSuggestions
fields = '__all__'
網址.py:
path('komentari_i_prijedlozi/', views.komentariPrijedlozi, name="komentariPrijedlozi"),
uj5u.com熱心網友回復:
據我所知,您沒有在模板中使用自定義表單,請將模板中的表單更改為:
<form method="POST" autocomplete="off" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_table }}
<input type="submit" class="btn-sub" value="Submit">
</form>
然后,當到達您的 views.py 時,這些值將被正確處理。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452247.html
