0
當我試圖將數據添加到數據庫時,我看不到我做錯了什麼。當我按下提交按鈕時,數據庫中不會輸入任何內容。按Enter鍵時會發生同樣的情況。用django通過按回車鍵將數據添加到數據庫中
這是我的html文件。
<script>
$(document).keypress(function(event) {
if (event.keyCode == 13 || event.which == 13) {
alert('enter key is pressed');
event.preventDefault();
}
});
</script>
<div class="col-md-6 col-md-offset-3">
<form method="POST" action="">
<p>
{% csrf_token %}
<input type="hidden" value="{{post.id}}" />
<div class="col-xs-16" style="margin: 0; 0;padding: 3%;">
<label for="inputsm">Oracle</label>
<input class="form-control input-md" type="text" value="{{ post }}">
<input type="submit" value="Submit" style="display: none" /> {{ form.body }}
<button type="submit" value="Submit" class="btn btn-success">Submit</button>
</p>
</div>
</form>
</div>
def post_list(request):
posts = Post.objects.all()
category = Category.objects.all()
context = {
'posts':posts,
'cat':category,
}
return render(request, 'journal/post_list.html', context)
def add_post(request):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST or None)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('post_details', pk=post.pk)
return render(request, 'journal/post_list.html', {'post': post})
else:
form = PostForm()
context = {
"form": form,
}
return render_to_response('journal/post_list.html', context)
'後= get_object_or_404(郵政,PK = PK)'什麼是PK?這是從哪裏來的? – karthikr