1

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.

3
  • You're only outputting a small part of the form, without any form tags. Either you've removed relevant code, or you're missing some substantial stuff. Why are you not just outputting the whole form generated by Django with {{form}}? Commented Mar 25, 2011 at 2:22
  • I've posted just the relevant section of the .html file. There are form tags in the .html file. The reason I want to keep control of each field for css formatting reasons - hence the reason I display each and every field of which email is one example. For guidance I used part of the references here: djangobook.com/en/2.0/chapter07 but I seem unable to see how I can get the formatting I require and the validation. Commented Mar 25, 2011 at 7:19
  • @user673600 You can style your form with CSS just fine with the Django default output - it outputs IDs on each element. Commented Mar 27, 2011 at 5:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.