Reputation: 1120
I am developing a simple web application with Google App Engine and I'd like to understand how to use the verbose_name option on Properties correctly.
I have a simple model:
from google.appengine.ext import ndb
class Person(ndb.Model):
name = ndb.StringProperty(required=True, verbose_name='my_name')
surname = ndb.StringProperty(required=True, verbose_name='my_surname')
Then I have a very simple HTML page that shows the user a form. This page is called and sent back as response using the Jinja2 template engine.
<html>
<body>
<form action="/test" method="POST">
<label>Insert name:</label>
<input name="my_surname" type="text"/>
<label>Insert surname:</label>
<input name="my_surname" type="text"/>
<button type="submit">Insert!</button>
</form>
</body>
</html>
I would like to be able to use, inside the html page, the property verbose_name
in order to be able to change property names very easily in future. As the Google App Engine docs state, verbose_name
is:
Optional HTML label to use in web form frameworks like jinja2.
What I would expect is then something like this:
<input name="{{ my_name }}" type="text"/>
<input name="{{ my_surname }}" type="text"/>
Any help will be very appreciated.
Upvotes: 0
Views: 135
Reputation: 7067
For starters, the purpose of verbose_name
is to use it in the label, not as the name of the input. It's better if the name matches the model (for clarity and future automation), and I don't think it'll change often (if at all), because that requires a lot of work (like re-writing all your entities).
The label is what the user reads in the form, and that might change, because of meaning or to make it clearer. This lets you change the output from code instead of directly on the template, which is specially useful when generating forms automatically (which is the actual purpose of the property).
So, as for your actual question, it's as easy as:
<input name="{{ models.Person.name._verbose_name }}" type="text"/>
This means you must get the models (or at least a model, Person) to the template, as an added value in the context.
Upvotes: 2