Suppose you have a model
class User(models.Model):
TYPES = (
('Admin', 'Admin'),
('Staff', 'Staff'),
)
You can access this in the view using the following code
In your view file
from app.core.models import User
def save(request):
user_types = User.TYPES
render('user/form.hrml', {user_types:user_types})
If you want to use this variable in the template, Assign that value user_types as a dictionary variable then use the below code
{% for value, text in user_types %}
{{ value }} {{ text }}
{% endfor %}
class User(models.Model):
TYPES = (
('Admin', 'Admin'),
('Staff', 'Staff'),
)
You can access this in the view using the following code
In your view file
from app.core.models import User
def save(request):
user_types = User.TYPES
render('user/form.hrml', {user_types:user_types})
If you want to use this variable in the template, Assign that value user_types as a dictionary variable then use the below code
{% for value, text in user_types %}
{{ value }} {{ text }}
{% endfor %}
0 Responses to Get python django model choice in View
Something to say?