2017-04-09 57 views
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>
這裏是views.py

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) 
+0

'後= get_object_or_404(郵政,PK = PK)'什麼是PK?這是從哪裏來的? – karthikr

回答

0

是否得到表單提交?如果沒有,也許嘗試加入一些JavaScript來提交表單,當按鍵被按下

$("form").submit(); 
+0

添加此代碼時,這會變得更好。數據正在輸入數據庫中。但是,由於數據不顯示,我仍然在做錯事。它顯示爲短劃線。感謝您的推動。 – Arielis