开发者

Model Forms in Django

I am doing a web interface using HTML and django. My problem is that I want to show a form in the website so that the user can fill the fields of one of the models. Although I've read some examples and documentation about how to do it, I cannot see the form in my website.

In my django app, I have the following model:

class Signature(models.Model):    
    sig = models.ForeignKey(Device)    
    STATE = models.CharField(m开发者_开发知识库ax_length=3, choices=STATE_CHOICES)    
    interval = models.DecimalField(max_digits=4, decimal_places=2)

As the form is related with that model, I have created this ModelForm class:

class SignatureForm(ModelForm):
    class Meta:
        model = Signature

After that, I have define the following view:

def SigEditor(request):

   if request.method == 'POST':

       form = SignatureForm(request.POST)

       if signature_form.is_valid():

           # Create a new Signature object.

           signature_form.save()

        return HttpResponseRedirect('eQL/training/form.html')

   else:

       form = SignatureForm()

   return render_to_response('eQL/training/showImage.html',
                             {'signature_form' : SignatureForm })

Finally, I can represent the form in my website by adding:

  <fieldset><legend>Device Information</legend>
      <form action="" method="post">

    {{ signature_form }} < br>
      <input type="submit" value="Submit">
      </form>

  </fieldset>

If I open the website, there are no errors but I don't see the form. However, before the submit button, appears:

< class 'eQL.models.SignatureForm' > 

Can anyone help me? I am quite new in this web framework. Thanks!!


Update:

you have 2 problems here:

1st mistake: you name the form instance with 2 names depending on form method (but this would have raised an exception if it's not for the 2nd error you made)

2nd error: you should give the form instance to the template,

not the class ('signature_form' : SignatureForm):

return render_to_response('eQL/training/showImage.html',
                          {'signature_form' : form})


The template tag {{signature_form}} is expanded as < class 'eQL.models.SignatureForm' > because it is a class not an object. You write a reference to the class not an object instance You have to write :

return render_to_response('eQL/training/showImage.html', {'signature_form' : form})

Instead of :

return render_to_response('eQL/training/showImage.html', {'signature_form' : SignatureForm})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜