I'm trying to display a list of tickets in an HTML table. The table has various headings to display various aspects of the tickets. I would like to present this same table in a bunch of different locations across my project.
I've made a single template of /templates/shared/ticket_list.html in which presents the ticket list and then I {% include %} it where I need to display this listing. Simple enough.
However, there are a couple of pages in my project where I have a Bootstrap tabbed div. I want to display this table of tickets in both tabs, but with a different set of tickets. The HTML for this essentially requires that I {% include %} my table template twice on the same HTML page.
For example:
Tab 1: "Created By User" -- a list of tickets that the current user created
Tab 2: "Assigned To User" -- a list of tickets that are assigned to the current user
In the view, I might have something like:
created_by_ticks = Ticket.objects.filter(created_by = self.request.user)
assigned_to_ticks = Ticket.objects.filter(assigned_to = self.request.user)
The problem is, in my ticket table template, how would I present both querysets since the table itself is likely expecting a single variable name, such as:
{% for t in tickets %}
<tr>...
{% endfor %}
But passing in two querysets of tickets, I now have two ticket variables of created_by_ticks and assigned_to_ticks.
Any ideas how I could use that single ticket table template, but use multiple variables, or some other solution?
You can use the with functionality of the include tag:
{% include 'shared/ticket_list.html' with tickets=created_by_ticks %}
{% include 'shared/ticket_list.html' with tickets=assigned_to_ticks %}
Related
I'm new to django, and i want to show field that related to foreign key in another table. this is the table.
i want to career table got the career_tag_name and hex_code from table color.
i've tried the Career.objects.raw()
this is the query in views.py:
careers = Career.objects.raw('''SELECT website_career_tag.career_tag_name,website_color.hex_code, website_career.*
from website_career INNER JOIN website_career_tag on website_career_tag.id = website_career.career_tag_id_id
LEFT JOIN website_color on website_career_tag.color_id_id = website_color.ID''')
it works perfectly, until i want to use filter() by career_tag_name. when i use query set it's more easy than make it raw to filter.
how do i make those raw query to query set?
It's always better to use django's own ORM rather than raw query and it's quite easy as well. Django querysets always stores all related table informations everywhere, you should just reference to them from the object. For example for career list view you can get all career informations and select the related fields to make loops less expensive in templates:
careers = Career.objects.select_related('career_tag_id', 'career_tag_id__color_id')
And then in a template just refer to related objects as:
{% for career in careers %}
{{ career.career_tag_id.career_tag_name }}
{{ career.career_tag_id.color_id.color_name }}
{% endfor %}
I want to create a for loop in dbt that loops through two columns of a table in my database and created case when statements using the values in each row of the two columns. Just like this:
{% set locations = {'%United-States%':'United States', '%USA%':'United States'} %}
select
case
{% for feeder, correct in locations.items() %}
when lower(locationname) like {{feeder}} then {{correct}}
{% endfor %}
end as city
from table
I was able to create lists for both feeder and correct, but I am not able to merge them as key-value pairs of a dictionary to loop through it. Any ideas on how should I do this?
Sounds like you have two problems:
fetch data from another table, and
use that data to populate a case when statement.
Big thing to remember is that the main thing that dbt-jinja does is create a string of SQL. That said, there is clever functionality that lets you query a database before jinja starts to put the string together.
The idea being:
fetch the values from the database that you want to have included in your SELECT query
populate the previously fetched values into the query while rendering the statement
there's two macros that may be of use in the first step:
run_query() (docs), and
dbt-utils' get_query_results_as_dict() (docs)
Something like this might work (provided you already have dbt-utils installed:
{% set locations_query %}
select feeder, correct from my_other_table
{% endset %}
{% set locations = run_query(locations_query) %}
select
case
-- not sure how this part will work yet....
{% for feeder, correct in locations.items() %}
when lower(locationname) like {{feeder}} then {{correct}}
{% endfor %}
end as city
from table
I've got a model class, House, with several columns. I want to get all the entries to this table, with all the columns, and display these on in a table in a template.
Firstly, how do I pull all the information that I need out of the database (and into a 2d list?), and what tag could I use to access specific data in the table?
it's as easy as stated here
so, practically:
all_houses = Houses.objects.all()
will give you all the entries in your database.
from within a view, pass that variable to the template context then, in the template:
{% for house in all_houses %}
{{ house.<column_name> }}
{% endfor %}
let me explain this bit of code: once you pass all your entries to the template, you can loop them with {% for %}
{{ house. }} means that you can extract the value you need from the column you need ( column_name ) and place it wherever you want (from whitin the for loop, obviously) so you can have (for example) {{ house.price }}, {{ house.bathrooms }} and so on, for each entry you have in your "all_entries"
I can fetch the data like this.
value= mymodel.objects.get(anycondition)
OR
value= mymodel.objects.filter(anycondition)
and can send them to my template with context.
But if I want to select all the data from a table(for all users not only one) as this query does
value= mymodel.objects.all()
and send this value to my template and can see there field by field
e.g.
my table has two fields name and phone no and I use the above query( value= mymodel.objects.all()) now if i want to see all names then i can see that and if i want to see phone no. I can see that too.
I have tried this and it doesn't work and I even I do not know it is possible or not.
If it is possible then please let me know how I can do this ?
I hope you understand my question. !!
Thanks in advance
.all() will return a list of objects that represent the rows in your model. .get() only returns one object. Your template is trying to print the result of all() if it was one object.
This is the same as if you had a list and you wanted to loop through it. In your view you would do:
product = Product_attributes.objects.all()
for i in product:
print i.size
print i.color
The equvalent for the template is:
<ul>
{% for i in product %}
<li>{{ i.size }}</li>
<li>{{ i.color }}</li>
{% endfor %}
</ul>
Although this question isn't clear it seems like you are having a bit of problem with Field Lookups. It is fairly easy to learn Here is a link to get you started
Im trying to using a ordered list in HTML to list the items in my querylist for some reason the ordering is not happening..
The list is diplayed but not ordering.
Here is my Django code,
<o1>
{% for t in teamrel %}
<li> {{t.teamrelation}} </li>
{% endfor %}
</o1>
Here is my output in the HTML code,
Patron Relations Team Volunteer Relations Team Volunteer Relations Team
its not getting ordered.
Let me know your inputs.
The main problem is that your code sample appears to have an <o1> tag (o+ONE) instead of <ol> (o+L).
Probably because <o1> shoudl be <ol>.
Otherwise, the QuerySet needs to be ordered correctly with an queryset.order_by('foo') call. Regardless of invalid tags, the output should be ordered.
You can print print queryset.query to see the actual ordering asked of the database.