Question

Trying to create a menu with "City/Location" having sub-menu for "Neighborhoods"

  • New York
  • -- Neighborhood
  • -- Neighborhood
  • -- Neighborhood
  • Chicago
  • -- Neighborhood
  • -- Neighborhood
  • -- Neighborhood
  • Miami
  • -- Neighborhood
  • -- Neighborhood
  • -- Neighborhood

View.py

def location(request):
args['getlocs'] = Location.objects.filter(switch=1, restaurants__city_id=8).order_by('name')
args['gethoods'] = Hood.objects.filter(switch=1, restaurants__city_id=8).distinct().order_by('name')
return render_to_response('location.html', args)

Template.html

<ul>
   {% for location in getlocs %}
      <li>{{ location.name }}</li>
        <ul>
           {% for hood in gethoods %}
              <li>{{ hood.name }}</li>
           {% endfor %}
        </ul>
    {% endfor %}
</ul>

Obviously when I run this, every neighborhood in db is displayed and I only need neighborhood that belongs in each city/location to be displayed. Is there a way just to add some condition to the html template or do I need to change the view? Thanks much!

EDIT:

class Location(models.Model):
name = models.CharField(max_length=50, db_column='location', blank=False, null=False)
city = models.ForeignKey('City', related_name="locations")
...

class Hood(models.Model):
name = models.CharField(max_length=50, db_column='hood')
city = models.ForeignKey('City', related_name='hoods')
location = models.ForeignKey('Location', related_name='hoods')
... 

class Restaurant(models.Model):
name = models.CharField(max_length=50, db_column='name', blank=True)
location = models.ForeignKey('Location', related_name="restaurants")
hood = models.ForeignKey('Hood', null=True, blank=True, related_name="restaurants")
...

No correct solution

OTHER TIPS

If I am under standing the relationship correctly, a City has many Neighborhoods. So this is a many-to-many relationship. In the models.py this would look like

from django.db import models

class Neighborhood(models.Model):
  name = models.CharField(max_length=100)

class City(models.Model):
  name = models.CharField(max_length=100)
  neighborhoods = models.ManyToManyField(Neighborhood)

You can then set up your page to accept the city name as an argument in the href, and then call a page view for only neighborhoods in that city. Your href link in the template to go to the city view with only it's neighborhoods may look like this:

<a href="{% url 'city_view' city.name %}">Link</a>

That is linked to this url in your urls.py:

url(r'city/(?P<city_name>.+)/$', views.city_view, name='city_view')

Then your view in views.py that will filter for only neighborhoods in that city may look like this:

def city_view(request, name):
  city = City.objects.get(name=name)
  neighborhoods = city.neighborhoods.all()
  return render(request, 'template.html', {'neighborhoods': neighborhoods})

Then call only the neighborhoods for that city in the template.html:

{% for neighborhood in neighborhoods %}
  neighborhood.name
{% endfor %}

This should hopefully show the database calls, views.py progression, and how it routes the view based on what is requested in the urls.py. If you need anything else, lmk.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top