Clayton Hutton
Clayton Hutton

Reputation: 11

Django 2.1 passing a variable to template,

Have a question here about passing a variable into a Django template. The goal is to filter a set of photos based off the type of photography. I initially wanted to do it from S3 and the folder that it was in, but that's a little beyond my skill at the moment. I went with just creating different url's that account for that. The issue I'm having is that I'd like to pass the variable into the template that extends the base_layout.html, but it won't render anything for that variable. Am I just miss-understanding how to do it?

Model.py

from django.db import models


# Create your models here.
class Gallery(models.Model):
    title = models.CharField(max_length = 50)
    body = models.TextField(max_length = 500)
    created = models.DateTimeField(auto_now_add = True)
    thumb = models.ImageField(default = 'default.jpg', blank = True)
    slug = models.SlugField(blank = True)
    order = models.CharField(max_length = 2, blank = True)

    def __str__(self):
        return self.title

    def body_preview(self):
        return self.body[:50]

class photoUrl(models.Model):
    url = models.CharField(max_length = 128)
    uploaded_on = models.DateTimeField(auto_now_add = True)

class Photos(models.Model):
    title = models.CharField(max_length = 50)
    picture = models.ImageField(blank = True)
    created = models.DateTimeField(auto_now_add = True)
    catagory = models.CharField(max_length=256, choices=[('wedding', 'wedding'), ('portrait', 'portrait'), ('landscape', 'landscape'), ('boudoir', 'boudoir'),], blank = True)

    def __str__(self):
        return self.title

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from . models import Photos

# Create your views here.


def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
    return render(request, 'gallery/gallery.html', {'photo_list' : photo_list}, {'photoCat' : photoCat})

urls.py

from django.contrib import admin
from django.urls import path
from . import views

app_name='gallery'

urlpatterns = [
    path('wedding/', views.photo_wedding, name='wedding'),
    path('portrait/', views.photo_portrait, name='portrait'),
    path('landscape/', views.photo_landscape, name='landscape'),
    path('boudoir/', views.photo_boudoir, name='boudoir'),
]

gallery.html

{% extends 'gallery/base_layout.html' %}

{% load static %}

{% block gallery %}

    <div class="gallery" id="gallery">
        <div class="container">
            <div class="w3l-heading">

                <h3>{{photoCat}}</h3>

                <div class="w3ls-border"> </div>
            </div>
        </div>
{% endblock gallery %}

Upvotes: 0

Views: 232

Answers (3)

Lemayzeur
Lemayzeur

Reputation: 8525

With the render() function, the third argument is the context. The context is a dictionary used to send variable to templates. No need to pass two dicts

def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
    context = {'photo_list' : photo_list,'photoCat' : photoCat}
    return render(request, 'gallery/gallery.html', context)

Upvotes: 0

Gahan
Gahan

Reputation: 4213

From the definition of render:

render(request, template_name, context=None, content_type=None, status=None, using=None) Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

the render method takes the first parameter as a request, the second parameter as template_name and the third parameter is a context which is of type dictionary you choose to pass to the template, you can access all the values of dictionary with the key.

So your method should look like below:

def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
    return render(request, 'gallery/gallery.html', {'photo_list' : photo_list, 'photoCat' : photoCat})

Upvotes: 1

Pythonista
Pythonista

Reputation: 11645

Why are you passing two dictionaries. Just add a key. That is the context data.

In class based views you can also overload the method get_context_data

Upvotes: 0

Related Questions