Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango ModelForm help
    text
    copied!<p>I've been working with a simple model and was having some trouble saving the ModelForm data. I want to allow users to create and edit existing "Group" objects in the database. If the user chooses to "Edit" an existing group, i want the resulting form to be pre-populated with that objects existing data. Then, if they click on "Save", it should update any changed data. The model and view I'm using is below. The problem I'm having is that form.is_valid() never returns True. What am i doing wrong here?</p> <p><strong>MODEL</strong></p> <pre><code>class Analyst(models.Model): def __unicode__(self): return unicode("%s, %s"%(self.last, self.first)) id = models.AutoField(primary_key=True) first = models.CharField(max_length=32) last = models.CharField(max_length=32) class Alias(models.Model): def __unicode__(self): return unicode(self.alias) alias = models.CharField(max_length=32) class Octet(models.Model): def __unicode__(self): return unicode(self.num) num = models.IntegerField(max_length=3) class Group(models.Model): def __unicode__(self): return unicode(self.name) name = models.CharField(max_length=32, unique=True) #name of the group id = models.AutoField(primary_key=True) #primary key octets = models.ManyToManyField(Octet, blank=True) #not required aliases = models.ManyToManyField(Alias, blank=True) #not required analyst = models.ForeignKey(Analyst) #analyst assigned to group, required </code></pre> <p><strong>VIEW</strong></p> <pre><code>class GroupEditForm(ModelForm): class Meta: model = Group def index(request): if request.method == 'GET': groups = Group.objects.all().order_by('name') return render_to_response('groups.html', { 'groups': groups, }, context_instance = RequestContext(request), ) def edit(request): if request.method == "POST": #a group was selected and the submit button clicked on the index page form = GroupEditForm(instance = Group.objects.get(name=request.POST['name'])) #create a form and pre-populate existing data for that object elif request.method == "GET": #the "add new" button was clicked from the index page form = GroupEditForm() #create a new form with no data pre-populated return render_to_response('group_edit.html', { 'form': form, }, context_instance = RequestContext(request), ) def save(request): if request.method == "POST": form = GroupEditForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/groups/') </code></pre> <p>and the relevant template</p> <p><strong>Template</strong></p> <pre><code>&lt;h1&gt;Edit Group Information&lt;/h1&gt; &lt;form method="post" action="/groups/edit/save/"&gt;{% csrf_token %} &lt;div class="edit"&gt; &lt;br&gt;&lt;/br&gt; {{form}} &lt;/div&gt; &lt;br&gt;&lt;/br&gt; &lt;input type="submit" value="Save"&gt; &lt;a href="/groups/"&gt;&lt;input type="button" name="cancel" value="Cancel" /&gt;&lt;/a&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload