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 %}
python django
add a comment |
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
add a comment |
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
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
python django
asked Nov 24 '18 at 4:10
Clayton HuttonClayton Hutton
63
63
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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})
add a comment |
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
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
add a comment |
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)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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})
add a comment |
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})
add a comment |
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})
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})
answered Nov 24 '18 at 4:25
GahanGahan
2,79531232
2,79531232
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 24 '18 at 4:28
LemayzeurLemayzeur
5,1971833
5,1971833
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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