Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialcsj
12,678 Points[Django] Please help explain what it is I'm not getting with query/views/models
I have an app called projects with 3 html templates.
- project_list.html
- project_detail.html
- map.html
the models.py:
from django.db import models
class Project(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
date = models.DateField()
featured_image = models.ImageField(
upload_to='projects/featured', default=False)
def __str__(self):
return self.title
class ProjectImage(models.Model):
project = models.ForeignKey(Project, related_name='images')
image = models.ImageField(upload_to='projects')
class MapData(models.Model):
map_data = models.ForeignKey(Project)
longitude = models.DecimalField(
max_digits=10, decimal_places=8, default=False)
latitude = models.DecimalField(
max_digits=10, decimal_places=8, default=False)
location = models.CharField(max_length=200, blank=True)
with the views.py:
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.views.generic import ListView, DetailView
from .models import Project
from .models import ProjectImage
from .models import MapData
class ProjectListView(ListView):
model = Project
class ProjectDetailView(DetailView):
model = Project
class MapDataView(TemplateView):
model = MapData
template_name = "projects/map.html"
And in my /projects/templates/project_list.html
template I can access the Project
class like so:
{% for project in project_list %}
{{ project.title }}
{% endfor %}
But in my /projects/templates/map.html
I can't do it. I can't access Project
or MapData
class like above. How can that be? Have I made a mistake with the MapDataView
in views.py
?
1 Answer
jacinator
11,936 PointsA TemplateView
does not require a model
attribute to be defined. With what you have I would think that you want to use the DetailView
for MapDataView
. You can still give it the template_name
that you have defined it MapDataView
. A TemplateView
only renders the template with a context of whatever kwargs
are passed into the view.
See these pages for a good summary of TemplateView
s and DetailView
s. I use them all the time.
https://ccbv.co.uk/projects/Django/1.10/django.views.generic.base/TemplateView/ https://ccbv.co.uk/projects/Django/1.10/django.views.generic.detail/DetailView/
The other thing to note is that you can import TemplateView
through django.views.generic
. You don't have to use django.views.generic.base
.
csj
12,678 Pointscsj
12,678 Points@jacinator
Thank you very much for taking the time to give this answer. It was most helpful. I will see if I can make it work.