Django - 根据用户在表

时间:2016-12-06 22:36:29

标签: django django-models django-views

我有以下情况:在网站的用户区域,他可以在表格中看到他所有的房地产帖子。每个帖子都有一个“垃圾按钮”。当他按下我想要的按钮时,从DB中删除他选择的确切实例。

user's area 这是我的HTML。请注意,我使用了一个视图来访问数据库,然后从数据库中删除。但我不知道如何发送确切的参数来在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。

2 个答案:

答案 0 :(得分: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(r'^delete-estate/$', views.delete_estate.as_view(), name='delete_estate'),
....

答案 1 :(得分:1)

将模态放在for循环中,以便每个属性都有一个独特的模态。请记住将模态的ID更改为id="delete_{{ anuncio.id }}",并使用删除按钮使用data-target="delete_{{ anuncio.id }}"激活相同的模态。在该模式中,您应该能够执行Hybrid对表单所说的内容并访问{{anuncio.id}}变量。

如果您还不知道,默认情况下已创建了ID。