I have been looking at form validation in python using Django as this seems to be the default way to do it, other than checking each field, performing some validation and kicking out a specific message for each field that is badly formed. Ideally I want the benefits of form validation, but I do not know how to couple this Django way with the .css way I am displaying by form.
My form is templated HTML with a css behind to handle the display. The code uses data to send back the form if there is an issue and displays the form which was created previously. So in a nutshell, how do we couple validation to a pre formatted HTML form with css without individually validating.
Here is the code I am using:
Looking at those references etc http://www.djangobook.com/en/2.0/chapter07/, 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开发者_JAVA技巧.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.
Thanks
"Customizing Form Design" section of http://www.djangobook.com/en/2.0/chapter07/ explains how to style forms to go with your HTML and CSS.
Looking at those references etc http://www.djangobook.com/en/2.0/chapter07/, 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.
精彩评论