disruptive
disruptive

Reputation: 5946

App engine validation with `django` and `css`

Looking at those references etc, I have been unable to come up with a good way to couple everything together. The problem relates to send back the form and contents if the form is not valid. So what I am doing is pulling out each generated form item by item and displaying in the .html file.

So my question is. How do I get this working. Now I can display the form with css style sheet, but I cannot seem to get validation working on the field and I'm always generating an error.

class Quote(db.Model):
    email = db.StringProperty(required=True)

class QuoteForm(djangoforms.ModelForm):
class Meta:
        model = Quote
        exclude = ['entry_time']

class MainPage(webapp.RequestHandler):
def get(self):
    form = QuoteForm();
    template_values = {}        
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, {'form': form}))

def post(self):
        data = QuoteForm(data=self.request.POST)
        if data.is_valid():
                # save here
                self.redirect('/Confirm.html')
        else:
            template_values = {}        
            path = os.path.join(os.path.dirname(__file__), 'index.html')
            self.response.out.write(template.render(path, {'form': data}))

and the part of the .html file is here

<div>
    {{ form.email.errors }}
    <label for="id_email">Your e-mail address:</label>
    {{ form.email }}
</div>

It would nothing that I put into the email field validates correctly. I'm not sure why!? I'm losing the information I have already put into the form. How do I retain this information and actually do proper validation. The model suggests that only a non blank string is required, but nothing ever satisfies the validation.

Upvotes: 1

Views: 240

Answers (1)

Xion
Xion

Reputation: 22770

You can add your own attributes to widgets (i.e. controls) that Django uses to render your form elements. This includes a class attribute for CSS styling:

class QuoteForm(djangoforms.ModelForm):
    email = EmailField(widget = TextInput(attrs={'class':'textfield'}))
    class Meta:
        model = Quote
        exclude = ['entry_time']

The downside is that you need to explicitly specify the fields from your model in the form class.

Upvotes: 1

Related Questions