Django 2.1 passing a variable to template,












0















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 %}









share|improve this question



























    0















    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 %}









    share|improve this question

























      0












      0








      0








      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 %}









      share|improve this question














      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 %}






      python django






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 4:10









      Clayton HuttonClayton Hutton

      63




      63
























          3 Answers
          3






          active

          oldest

          votes


















          1














          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})





          share|improve this answer































            0














            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






            share|improve this answer
























            • Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

              – Clayton Hutton
              Nov 24 '18 at 4:22



















            0














            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)





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455071%2fdjango-2-1-passing-a-variable-to-template%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              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})





              share|improve this answer




























                1














                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})





                share|improve this answer


























                  1












                  1








                  1







                  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})





                  share|improve this answer













                  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})






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 4:25









                  GahanGahan

                  2,79531232




                  2,79531232

























                      0














                      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






                      share|improve this answer
























                      • Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                        – Clayton Hutton
                        Nov 24 '18 at 4:22
















                      0














                      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






                      share|improve this answer
























                      • Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                        – Clayton Hutton
                        Nov 24 '18 at 4:22














                      0












                      0








                      0







                      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






                      share|improve this answer













                      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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 '18 at 4:12









                      PythonistaPythonista

                      8,73321437




                      8,73321437













                      • Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                        – Clayton Hutton
                        Nov 24 '18 at 4:22



















                      • Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                        – Clayton Hutton
                        Nov 24 '18 at 4:22

















                      Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                      – Clayton Hutton
                      Nov 24 '18 at 4:22





                      Got it, thanks! Can't believe I was doing that. Kid being sick for three days takes its mental toll I guess.

                      – Clayton Hutton
                      Nov 24 '18 at 4:22











                      0














                      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)





                      share|improve this answer




























                        0














                        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)





                        share|improve this answer


























                          0












                          0








                          0







                          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)





                          share|improve this answer













                          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)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 4:28









                          LemayzeurLemayzeur

                          5,1971833




                          5,1971833






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455071%2fdjango-2-1-passing-a-variable-to-template%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks