Prisijungti

django neina editinti form iraso

  • 26 Geg '10

Sveik,
pagal pavyzdi meginu padaryt add/edit/delete funkcionaluma, add'inti sekasi puikiai, o editinti neina vis saugo kaip nauja irasa. Ka darau blogai. Kodas:

TIK SAVINIMUI

from django.forms import ModelForm

class Client(models.Model):
    title   = models.CharField(max_length=250)
    description   = models.CharField(max_length=2000)

def clients_add(request):
    if request.method == 'POST':
        form = ClientForm(request.POST)
        if form.is_valid():
            form.save()
            saved = 1
    else:
        form = ClientForm()

    return render_to_response('add.html', locals())

JEI NORIU EDITINT

class Client(models.Model):
    title   = models.CharField(max_length=250)
    description   = models.CharField(max_length=2000)

class ClientForm(forms.Form):
    title   = forms.CharField(max_length=250)
    description   = forms.CharField(max_length=2000)

def clients_edit(request, id=None):
    if request.method == 'POST':
        form = ClientForm(request.POST, instance=Client.objects.get(id=id))
        render(form)
        if form.is_valid():
            form.save()
    else:
        form = ClientForm(instance=Client.objects.get(id=id))
    return render_to_response('edit.html', locals())

meginau ivairiausiais budais su siuo kodu: 'ClientForm' object has no attribute 'save'

  • 26 Geg '10

Kaip matau sukuri ClientForm instancą ir jį saugai, nors kaip suprantu saugoti reikėtų modelį Client?

Galiu ir meluoti, reikėtų pasibandyti...

  • 26 Geg '10

Jei nori „keisti“ reikia:

class Client(models.Model):
    title   = models.CharField(max_length=250)
    description   = models.TextField(max_length=2000)

class ClientForm(forms.Form):
    class Meta:
        model = Client

def clients_edit(request, id=None):
    client = Client.objects.get(pk=id)
    if request.method == 'POST':
        form = ClientForm(request.POST, instance=client)
        if form.is_valid():
            form.save()
    else:
        form = ClientForm(instance=client)

    return render_to_response('edit.html', locals())

Bent jau man veikia normaliai.

  • 11 Lap

big fish free slots games <a href=" http://onlinecasinogameslots.com/# ">free slot play no download </a> slot machines for home entertainment http://onlinecasinogameslots.com/#

Atsakyti