Printing data from fields that exist within a model onto an HTML page in Django -
i trying print field exists within model onto html page in django.
i want street address field print onto page. username prints onto page within issues. what's best way this?
model:
from django.db import models django.contrib.auth.models import user class address(models.model): user = models.foreignkey(user) street_address = models.charfield(max_length = 200) city = models.charfield(max_length = 100) state = models.charfield(max_length = 100) zipcode = models.integerfield(max_length = 5) updated = models.datetimefield(auto_now = true, auto_now_add = false) timestamp = models.datetimefield(auto_now = false, auto_now_add = true) active = models.booleanfield(default = true) def __str__(self): return self.user.address class job(models.model): user = models.foreignkey(user) position = models.charfield(max_length = 200) employer_address = models.charfield(max_length = 200) city = models.charfield(max_length = 100) state = models.charfield(max_length = 100) zipcode = models.integerfield(max_length = 5) start_date = models.datefield(auto_now = false, auto_now_add = false) end_date = models.datefield(auto_now = false, auto_now_add = false) updated = models.datetimefield(auto_now = true, auto_now_add = false) timestamp = models.datetimefield(auto_now = false, auto_now_add = true) phone = models.charfield(max_length = 20, null=true, blank=true) active = models.booleanfield(default = true) def __str__(self): return self.position
view:
def all(request): users = user.objects.filter(is_active=true) return render_to_response('all.html', locals(), context_instance=requestcontext(request))
html:
{% extends 'home.html' %} {% block content %} <!-- user being called variable declared users in previous page --> {% user in users %} <li>{{ user }}</li> {% endfor %} <!-- {% address in user.street_address %} <li>{{address}}</li> {% endfor %} --> {% endblock %}
unless you're looking have of addresses after of users, put second loop inside first.
you're passing users
, collection of user
objects template rendering. user
doesn't have street_address
field - address
does. you'll need pass address
objects form.
it appears street_address
charfield
, not sort of list.
if user
had street_address
field, do:
{% user in users %} <li>{{ user }}</li> <li>{{ user.street_address }}</li> {% endfor %}
since doesn't, instead this:
def all(request): addresses = address.objects.all() return render_to_response('all.html', locals(), context_instance=requestcontext(request))
and
{% address in addresses %} <li>{{ address.user }}</li> <li>{{ address.street_address }}</li> {% endfor %}
Comments
Post a Comment