单击时删除对象

时间:2016-05-30 18:56:32

标签: python django

我正在尝试创建一个待办事项应用程序,我有一个带有表格的表单,用户可以在其中添加" todo"这种形式。

我希望能够删除添加的" todo"从数据库中点击按钮而不必将用户重定向到另一个页面,有没有办法用Django做到这一点?

http://postimg.org/image/8dpe1dcob/

Views.py

from django.shortcuts import render
from django.utils import timezone
from .models import Task
from .forms import TaskForm
from django.shortcuts import redirect
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse

# Create your views here.

def tasks_list(request):
    tasks = Task.objects.filter(created_date__lte=timezone.now()).order_by('created_date')
    form = TaskForm()
    if request.method == "POST":
        form = TaskForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.created_date = timezone.now()
            post.save()
            #return render(request, 'todo/tasks_list.html', {'tasks': tasks, 'form': form})
            return HttpResponseRedirect('/')
    else:
        form = TaskForm()
    return render(request, 'todo/tasks_list.html', {'tasks': tasks, 'form': form})




def post_remove(request, new_id):
    post = get_object_or_404(Task, id=new_id)
    post.delete()
    return redirect('todo.views.tasks_list')

url.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.tasks_list, name='tasks_list'),
    #url(r'^$', views.tasks_list, name='post_remove'),
]

form.py

from django import forms
from .models import Task

class TaskForm(forms.ModelForm):
    title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Add todo'}), label='')
    due_date = forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Add due date', 'class':'datepicker'}), label='')

    class Meta:
        model = Task
        fields = ('title', 'due_date',)

模板/ task_list.html

{% extends 'todo/base.html' %}
{% load bootstrap %}

{% block content %}
<h1>Todos</h1>
<div class="container" id="container">
  <div class="input-group">
    <!--<input type="text" class="form-control" id="textField" placeholder="Add tasks">-->
    <form method="POST" class="post-form" >{% csrf_token %}
      {{ form|bootstrap }}

      <span class="input-group-btn">
        <button class="btn btn-default" type="submit" id="plusSign">Go!</button>
      </span>
      </form>
  </div><!-- /input-group -->
  <table style="width:100%" id='todoTable'>
    <thead id="tableHead">
      <tr>
        <td>Todo</td>
        <td>due date</td>
        <td>delete</td>
      </tr>
    </thead>
    <tbody>
    {% for task in tasks %}
    <tr>
      <td>{{ task.title }}</td>
      <td> {{ task.due_date }} </td>
      <td> <a class="btn btn-default" href="{% url 'tasks_list' %}"><span class="glyphicon glyphicon-remove"></span></a> </td>
    </tr>
  </body>
    {% endfor %}
  </table>
</div>
{% endblock %}

编辑:

我使用Django girls tutorial中的代码找到了解决方案。

我添加了以下代码:

Views.py

def post_remove(request, pk):
    post = get_object_or_404(Task, pk=pk)
    post.delete()
    return HttpResponseRedirect('/')

Url.py

url(r'^task/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),

模板/ task_list.html

<td> <a class="btn btn-default" href="{% url 'post_remove' pk=task.pk %}"><span class="glyphicon glyphicon-remove"></span></a></td>

有人可以检查我对删除的理解是否正确吗?

因此,task_list.html正在将每个待办事项的主键添加为href="/task/49/remove/",这允许views.py中的post_remove功能仅删除&#34; todo&#34;由于post_remove函数中的请求参数,单击按钮时,只有特定的href和删除才有效吗?

1 个答案:

答案 0 :(得分:1)

是。使用ajax,只需调用删除视图,就可以访问数据库并删除该条目。只需使用javascript将其移除到客户端即可。

或者您可以考虑将django-rest-frameworkangularjs一起使用,以便不重新发明轮子并增加一些灵活性来进一步改进您的应用。您可以添加回调以确保一切都在服务器端正确。

如果您想要站在最前沿,可以查看django-channels