On Fri, Apr 16, 2010 at 6:46 PM, Daxal <daxal.someone...@gmail.com> wrote:
> Thank you for your reply. Helped a lot.
>
> When issuing the code, I get a error.
>
> ...

Yeah, I was in a rush. Your model should really have a CamelCase name
to avoid these sorts of problems..

The basic pattern for using a model form is like this.

# models.py
from django.db import models

class MyObject(models.Model):
  name = models.CharField(max_length=128)


# urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('views',
    url(r'^editmyobject/(?P<object_id>\d+)$', edit_myobj, name='edit_myobj'),
  )


# forms.py
from django import forms
from models import MyObject

class EditMyObjectForm(forms.ModelForm):
  class Meta:
    model = MyObject


# views.py
from models import MyObject
from forms import EditMyObjectForm
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext

def edit_myobj(request, object_id=None):
  instance = get_object_or_404(MyObject, id=object_id)
  if request.method == 'POST':
    form = EditMyObjectForm(instance=instance, data=request.POST)
    if form.valid():
      instance = form.save()
  else:
    form = EditMyObjectForm(instance=instance)
  ctxt = RequestContext(request, { 'form': form, 'instance': instance, })
  return render_to_response('edit_myobject.html', context_instance=ctxt)


I'm purposefully not using your class names, as they are too ambiguous
and confusing for me to grasp properly. Hopefully this will make it
clearer for you.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to