2016-12-06 2 views
0

私は以下のような状況に陥っています。ウェブサイト上のユーザーのエリアでは、テーブル内のすべての不動産の投稿を見ることができます。投稿のそれぞれに「ごみボタン」があります。彼がボタンを押すと、彼が選んだ正確なインスタンスをDBから削除したい。Django - テーブル内のユーザーの選択に応じてインスタンスを削除する

user's area これは私が持っているHTMLです。 DBにアクセスしてDBから削除するには、ビューを使用することに注意してください。しかし、私は正確なパラメータをDB上で見つけるために送る方法を知らない。

<div class="container"> 
    <div class="col-xs-12"> 
     <h1>Olá, {{ request.user.first_name }}</h1> 
    </div> 
    <div class="row col-md-12 col-md-offset-0 custyle"> 
    <table class="table table-striped custab"> 
    <thead> 
     <tr> 
      <th>Imagem Principal</th> 
      <th>Data Criação</th> 
      <th>Tipo do Anúncio</th> 
      <th>Tipo do Imóvel</th> 
      <th>Preço Venda</th> 
      <th>Visualizações</th> 
      <th>Expira</th> 
      <th>Status</th> 
      <th class="text-center">Action</th> 
     </tr> 
    </thead> 
      {% for anuncio in anuncios %} 
      <tr > 
       <td> 
        <div class="embed-responsive embed-responsive-16by9"> 
         <img class="embed-responsive-item" src="{{anuncio.imagem_principal.url}}"> 
        </div> 
       </td> 
       <td>Falta</td> 
       <td>{{anuncio.tipo_anuncio}}</td> 
       <td>{{anuncio.tipo_imovel}}</td> 
       <td>R$ {{anuncio.preco_venda}}</td> 
       <td>Falta</td> 
       <td>News Cate</td> 
       <td>News Cate</td> 
       <td><p data-placement="top" data-toggle="tooltip" title="Delete"> 
        <button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete"> 
         <span class="glyphicon glyphicon-trash"></span> 
        </button></p> 
       </td> 


      </tr> 
      {% endfor %} 

    </table> 
    </div> 

</div> 
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true"> 
     <div class="modal-dialog"> 
    <div class="modal-content"> 
      <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button> 
     <h4 class="modal-title custom_align" id="Heading">Edit Your Detail</h4> 
     </div> 
      <div class="modal-body"> 
      <div class="form-group"> 
     <input class="form-control " type="text" placeholder="Mohsin"> 
     </div> 
     <div class="form-group"> 

     <input class="form-control " type="text" placeholder="Irshad"> 
     </div> 
     <div class="form-group"> 
     <textarea rows="2" class="form-control" placeholder="CB 106/107 Street # 11 Wah Cantt Islamabad Pakistan"></textarea> 


     </div> 
     </div> 
      <div class="modal-footer "> 
     <button type="button" class="btn btn-warning btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Update</button> 
     </div> 
     </div> 
    <!-- /.modal-content --> 
    </div> 
     <!-- /.modal-dialog --> 
    </div> 



    <div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true"> 
     <div class="modal-dialog"> 
    <div class="modal-content"> 
      <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button> 
     <h4 class="modal-title custom_align" id="Heading">Delete this entry</h4> 
     </div> 
      <div class="modal-body"> 

     <div class="alert alert-danger"><span class="glyphicon glyphicon-warning-sign"></span> Are you sure you want to delete this Record?</div> 

     </div> 
     <div class="modal-footer form-actions"> 
     <a href="{% url 'dashboard:dashboard_deletar' %}" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span> Yes</a> 
     <button type="submit" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button> 
     </div> 
     </div> 
    <!-- /.modal-content --> 
    </div> 
     <!-- /.modal-dialog --> 
    </div> 

私は本で学んだことを実践しているので、まだAJAXに行きたくありません。

答えて

1

<form>のボタンをPOSTのビューにすると、インスタンスが削除されます。例えば:

HTML

<form action="{% url 'delete_estate %}" method="POST"> 
{% csrf_token %} 
<input type="hidden" name="estate_id" value="{{ estate.id }}"> 
</form> 

def delete_estate(request): 
    if request.method == "POST": 
     estate_id = request.POST['estate_id'] 
     estate = Estate.objects.get(id=estate_id) 
     estate.delete() 

     messages.success(request, "Estate deleted successfully!") 
     return redirect("/") 

のURL

... 
url(r'^delete-estate/$', views.delete_estate.as_view(), name='delete_estate'), 
.... 
+0

申し訳ありませんが、この場合は動作しません。ゴミ箱ボタンをクリックするとモーダルダイアログが表示されます。それが私の問題です。このダイアログにpost_idをどのように取るかわかりません。確認なしでボタンだけだったならば、あなたが提案したことをするのは大丈夫でしょう。 –

1

WELとしてforループの内側モーダルを入れ私はすべての不動産のために別個の様式があるようにします。モーダルのIDをid="delete_{{ anuncio.id }}"に変更し、削除ボタンでdata-target="delete_{{ anuncio.id }}"を使用して同じモーダルを有効にすることを忘れないでください。そのモーダルの中から、Hybridがフォームで言ったことを実行し、{{anuncio.id}}変数にアクセスできるはずです。

まだわからない場合、idはデフォルトで既に作成されています。

+0

ありがとう!出来た。 –

関連する問題