Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango newbie question regarding defining an object with subobjects from models for use in templates
    primarykey
    data
    text
    <p>I am somewhat new to Django and have searched for some simple examples of creating objects with subobjects in views so that in templates I can have nested for loops.</p> <p>Here is my models.py for this application...</p> <pre><code>from django.db import models from django import forms class Market(models.Model): name = models.CharField('Market name', max_length=150) state = models.CharField('State', max_length=2) def __unicode__(self): return self.name class Location(models.Model): name = models.CharField('Location name', max_length=150) address1 = models.CharField('Address 1', max_length=200) address2 = models.CharField('Address 2', max_length=200,blank=True) city = models.CharField('City', max_length=100) state = models.CharField('State', max_length=2) zip_code = models.CharField('ZIP', max_length=10) phone = models.CharField('Phone', max_length=20) hours = models.TextField('Hours of operation', max_length=255) quote_text = models.TextField('Customer quote', max_length=500) quote_by = models.CharField('Customer name', max_length=30) yelp_url = models.URLField('Yelp URL', max_length=300,blank=True) image_store = models.ImageField('Storefront image', upload_to='images/locations', max_length=300,blank=True) image_staff = models.ImageField('Staff image', upload_to='images/locations', max_length=300,blank=True) market = models.ForeignKey(Market, verbose_name='Regional market', null=True) def __unicode__(self): return self.name </code></pre> <p>Markets data may look as follows...</p> <pre><code>id = 1 state = 'MO' name = 'St. Louis - Central' id = 2 state = 'MO' name = 'St. Louis - West' id = 3 state = 'IL' name = 'Chicago - South' id = 4 state = 'IL' name = 'Chicago - North' </code></pre> <p>In my views.py I'd like to create an object with a list/array of grouped Market states (distinct) in descending order, each with a subarray of the individual Market names in that State in order to complete a nested forloop in the template.</p> <p>The templating language in Django is really cool in how it prevents a ton of logic from residing betwixt the html, which I like. But I am still wrapping my head around both Python syntax and the need to create all objects exactly the way they need to iterate in the template. </p> <p>Here's what views.py looks like ...</p> <pre><code>def locations_landing(request): marketList = Market.objects.values('state').order_by('-state').distinct() return render_to_response('locations.html', locals()) </code></pre> <p>How to return an object so that my template can perform the following nested looping...</p> <pre><code>{% for st in stateList.all %} &lt;h4&gt;{{ st.state }}&lt;/h4&gt; {% for mkt in stateList.marketList.all %} &lt;p&gt;* &lt;a href="#"&gt;{{ mkt.name }}&lt;/a&gt;&lt;/p&gt; {% endfor %} {% endfor %} </code></pre> <p>This would result in the following rendered in html based on my data samples above...</p> <pre><code>&lt;h4&gt;MO&lt;/h4&gt; &lt;p&gt;* St. Louis - Central&lt;/p&gt; &lt;p&gt;* St. Louis - West&lt;/p&gt; &lt;h4&gt;IL&lt;/h4&gt; &lt;p&gt;* Chicago - South&lt;/p&gt; &lt;p&gt;* Chicago - North&lt;/p&gt; </code></pre> <p>BTW, there are no errors in any of the .PY code samples above, all is well, I just need some guidance on creating the object correctly in the view so the template does it's thing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload