Display all model data inside html page - python

I'm trying to build a website that has products and categories.
When you are on the page of a product, you can click a button to see a list of all the categories it falls under.
You can click another button, that appears on all pages, to see a list of all the categories overall.
In the html page see_all_categories, I wrote a simple block like this:
{% extends 'base.html' %}
{% load staticfiles %}
{% block content%}
{{Category.all}}
{% endblock content %}
I expect to see a messy printout of all the categories but I don't. It doesn't return an error, but it produces nothing, other than the base.html.
What am I doing wrong?

You want to display a list of the categories. I assume your Category model owns an attribute named "title" which is the representation of your Category.
If you're using Django template engine or Jinja2, you can make a for loop inside your template like this :
{% for cat in Category.objects.all %}
{{ cat.title }}
{% endfor %}
As a troubleshooting, I'd suggest you didn't pass your Category model to your template, that is not done automatically. You have to add your model to the context before rendering the template.
As mentionned in the comments, here is doc for template rendering with Django templates.
Django Template Guide
To add your model to the context you can follow this guide.
I don't intend to help you further because I lack of information and that may vary a LOT according to your settings. (Class Based views ? Function based views ? What kind of template are you using... And so on)

I figured out the solution after many long annoying hours of trying everything. I feel dumb but I want to spare the next guy the massive pain in the two-pack.
This is what I did:
In the Views.py, I changed the view function for this page FROM this:
def view_all_categories(request):
context = {'Category' : Category}
return render(request, 'store/see_all_categories.html', context)
TO this
def view_all_categories(request):
all_cats = Category.objects.all().order_by('id')
context = {'all_categories' : all_cats}
return render(request, 'store/see_all_categories.html', context)
and in the page see_all_categories.html itself, I changed it (from the question) TO this:
{% extends 'base.html' %}
{% load staticfiles %}
{% block content%}
{% for cat in all_categories %}
<p>{{ cat.name }}</p>
{% endfor %}
{% endblock content %}
And now it works!!

Related

Creating a custom template tag to replace the for loop - Django

I am trying to simplify my code by creating a custom template tag for a 'for loop' that use frequently on my Django web application. I thought it would be a simple straight process, but something isn't working right... I can use some assistance in catching my error.
Here is my code.
views.py
class ArticleView(DetailView):
model = Articles
def get_context_data(self, **kwargs):
context = super(ArticleView, self).get_context_data(**kwargs)
context['s_terms'] = scientific_terms.objects.all()
return context
template tag
#register.filter(name='term')
def term(value):
{% for term in s_terms %}
{{ term.short_description }}
{% endfor %}
template.html
{% Neurons|term %}
Thank you for your assistance, in advance.
You are mixing Python code with the Django Template Language. The template tags are plain Python code, as they are defined inside a Python module. A working example would be:
#register.filter(name='term')
def term(terms):
output = ''
for term in terms:
output = '{0} {1}'.format(output, term.short_description)
return output
Then you could use it like this:
{{ s_terms|term }}
Maybe what you want is simply to create a reusable Django template.
For example, create a new template named terms.html:
templates/terms.html
{% for term in terms %}
<p>{{ term.short_description }}</p>
{% endfor %}
Then, in another template, you could include this partial template:
templates/index.html (name is just an example)
{% extends 'base.html' %}
{% block content %}
<h1>My application</h1>
{% include 'terms.html' with terms=s_terms %}
{% endblock %}

simple loop in rendered template django cms plugin

I want to loop data taken from database in rendered template. Using cms plugin.
I dont have problem looping data in html template. But if i use CMSPlugin to insert new app in placeholder, nothing shows.
If i run url localhost:port/test.html.I got input what i want. But rendered template doesnt loop data.
{% for post in posts %}
{{ post.firstoption }}
{% endfor %}
if I use code below, nothing shows in my rendered template. Values are passed in rendered template. Because, if i try {{instance.firstoption}} i get value shown in template. Problem is i cant loop data with tag instance.
{% for post in instance.posts %}
{{ post.firstoption }}
{% endfor %}
I also tried {% for post in instance.posts_set.all %}
and {% for post in instance.posts.all %}
cms_plugins.py
class pricing(CMSPluginBase):
model = mymodel
name = _("myplugin")
render_template = "template.html"
cache = False
def render(self, context, instance, placeholder):
context.update({'instance': instance})
return context
models.py
class mymodel(CMSPlugin):
firstoption = models.CharField(max_length=200)
def __str__(self):
return self.firstoption
It is probably because you need to call all on your posts
{% for post in instance.posts.all %}
{{ post.firstoption }}
{% endfor }

What is a good way to get the dynamic title of a page in Django?

I'm trying to customize a share on Twitter button and I'm using to this to share:
http://twitter.com/intent/tweet?status=[TITLE]+[URL]
I'm able to get the URL with {{ request.get_host }} and {{ request.path }}, but I can't seem to get the title working. Are there any request objects I can use to get the title of my current page title? Thanks.
The easiest way to do this is:
Insert the following code in your base.html
#base.html
{% block title %} {% endblock %}
and following in your index or any Html file with your title
#index.html
{% extends 'base.html'%}
{% block title %} My Page Title {% endblock %}
Place your context object key in double paranthesis.
Lets take am making a dynamic teacher's page in a school management system
#urls.py
path("teacher/<str:pk>/",views.teacher,name="teacher"),
The vew
#view
def teacher(request,pk):
teacher = Teacher.objects.get(id=pk)
context= {"teacher":teacher}
return render(request,'main/teacher.html',context)
The template
<title>{{teacher.fname}} Page</title>

Include tag in Django template language -- what can I pass in to it?

OK, so again there is likely a "simple" solution to this, but I am a beginner and nothing seems simple to me.
I have a view and a template that shows the attributes of an instance of a Car class that I have modeled. This Car class has a ManyToMany relationship with my custom User class. The template that show the attributes of a given instance of Car has many variables. The view for each Car works fine. Here is what I can't get to work:
I have a user profile page for each instance of User. From that page, I want to show the attributes of each Car that a particular User has "favorited." I am unable to figure out how to do this.
I have tried the {% include %} tag to include a snippet of the Car template and then use a for statement to iterate through the favorite set of the User. In theory, this would populate the User page with each Car that they have "favorited" and show its attributes. However, I do not know how to pass the {% include %} tag the proper context so the attributes are populated correctly for each instance of Car. Is this possible?
Is there a simpler way to do it that I am just overlooking?
Any help is appreciated. Thanks!
Use the {% include ... with ... %} syntax:
{% for car in user.favorite_cars.all %}
{% include "car.html" with name=car.name year=car.year %}
{% endfor %}
Another alternative is the {% with %} tag:
{% for car in user.favorite_cars.all %}
{% with name=car.name year=car.year %}
{% with color=car.color %}
{% include "car.html" %}
{% endwith %}
{% endwith %}
{% endfor %}
UPDATE: If data for the template can't be obtained from the Car model then you have to use the custom inclusion tag:
from django import template
register = template.Library()
#register.inclusion_tag('car.html')
def show_car(car):
history = get_history_for_car(car)
return {'name': car.name, 'history': history}
And the in the template:
{% load my_car_tags %}
{% for car in user.favorite_cars.all %}
{% show_car car %}
{% endfor %}

Render Externally Defined Block In Django Template

I'm writing a simple blog-like application for Django and am trying to get the effect of having a front page with posts limited to 5, with a comprehensive archive that lists something like 100 posts at a time. (100 is not realistic, just throwing a number out there)
Since the blog post blocks will look exactly the same between the two pages minus the number being shown, I'd like to put the corresponding HTML in a separate template that I can include or link to from the actual templates being rendered. I've looked over the documentation, and the include tag looked promising, but it apparently renders outside of the current context, which is not helpful to my cause, since it wouldn't get the objects to loop through. Outside of that, I can't see any other way to do what I want. Is this possible or am I just out of luck and going to have to violate DRY? Code is below to give you an idea of what I want.
Thanks
#######################
# news/frontpage.html #
#######################
{% extends "news/base.html" %}
{% block site_title %} - Front Page{% endblock %}
{% block center_col %}
{{ block.super }}
View Older Blog Posts
{% endblock %}
{% block blog_rows %}
{% for object in object_list %}
# Blog post content would go here, however it is to be included.
{% endfor %}
{% endblock %}
You're looking for an inclusion tag.
Why don't you filter for the blog posts you want to show in your view? That way you can keep the template the same:
{% for object in blogposts %}
# ...
{% endfor %}
You define blogposts in your view, which either includes 5 or 100 posts.
Ignacio is right that you want an inclusion tag, but you should know that the include tag does not render outside the current context - it very definitely uses the same context as the block it's in.
Your problem is probably that you're trying to call blogpost_set on the object_list - but the relationship is not with the list of objects, it's with each individual object in the list. You'd need to iterate through object_list and then through blogpost_set.all on each one.

Categories