如何删除条目对象

时间:2010-12-17 13:56:30

标签: django forms object models

我可以使用具有mysql背景的Django创建一个表单。我想知道是否可以创建一个允许您删除对象的代码。假设我有一个名为“Tony”的客户端,我想创建一些允许我删除Tony的python代码。我该怎么做?

#forms.py
from django import forms
from c2duo.accounts.models import *

class ClientForm(forms.ModelForm):
    client_number = forms.IntegerField()
    name = forms.CharField(max_length=80)
    address = forms.CharField(max_length=250)
    telephone = forms.CharField(max_length=20)
    fax = forms.CharField(max_length=20)
    email = forms.EmailField()
    alternative_name = forms.CharField(max_length=80, required=False)
    alternative_address = forms.CharField(max_length=250, required=False)
    alternative_telephone = forms.CharField(max_length=20, required=False)
    alternative_email = forms.EmailField(required=False)

        class Meta:
        model = Client
        fields = ('client_number','name','address','telephone','fax','email','alternative_name','alternative_address','alternative_telephone','alternative_email'

#views.py 
@login_required
def add_client(request):
    if request.method == 'POST':
        form = ClientForm(request.POST or None)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/index/')
    else:
        form = ClientForm()
    return render_to_response('add_client.html', {'form': form},  context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:0)

def delete_client(request, client_id): 
    client = Client.objects.get(id=client_id)
    client.delete()
    redirect_to = '/index/'
    return HttpResponseRedirect(redirect_to)