How to send zipped list in context (Django) - python

I am trying to send a zipped list in context to my HTML template. But I'm only seeing the data of the latest object and not all the objects. Document is my model name.
I am trying to scrape a HTML page and the values I've received are being stored in list (I'm using lxml to parse).
Here is the relevant code from views.py
query_results=Document.objects.order_by('-id')
zipped=Document.objects.all().distinct().values_list('zipped')
context ={
"query_results": query_results, "zipped": zipped,
}
return render(request, "result.html", context)
Here is how I'm displaying the data in my template's table.
{% for item in query_results %}
<tbody>
<tr>
<td>...</td>
.
.
<td colspan="2">
{% for i,j in zipped %}
<b>{{ i }}</b> : {{ j }}<br><br>
{% endfor %}
</td>
.
.
</tr>
</tbody>
{% endfor %}
This is how I zipped the two lists.
Mymodels=Document()
Mymodels.zipped = zip(Mymodels.anchortext,Mymodels.anchorlink)
I'm supposed to get different values for my 3 rows but I'm getting the same value for all the 3 rows. The other values are fine only the zipped values are the same.
Any help would be greatly appreciated.

Related

How to build an HTML table using a for loop in Flask?

I am trying to create a table on a webpage using python and flask. I have two lists, testing_sketches and split_reconstructions.
testing_sketches: It is a list of size 14, containing 14 image addresses.
split_reconstructions: It is a list of lists containing 14 lists of length 20. Basically, there are 20 image addresses corresponding to each of the 14 images in the previous list. This means it includes 20 image addresses for each image address in testing_sketches.
I am trying to iteratively take an image from testing_sketches and display its 20 images in split_reconstructions on its right. Something like the following:
And I have tried to implement it using the following code:
<body>
<h2 style="margin-left: 1.5%;">Iterative Reconstructions</h2>
{% for i in range(len(testing_sketches)) %}
<br>
<br>
<h3 style="margin-left: 10%;">{{ i+1 }}.</h3>
<center><table border="1">
<COLGROUP>
<COL width="100"><COL width="100">
<THEAD>
<tr>
<td><center><b>Image<b></center><img src={{ testing_sketches[i] }} alt="Sorry, No Display!" border="0"/></td>
{% for j in range(20) %}
<td><center><b>Reconstruction-{{ j }}<b></center><img src={{ split_reconstructions[i][j] }} alt="Sorry, No Display!" border="0"/></td>
{% endfor %}
</tr>
</table></center>
{% endfor %}
</body>
But this results in a blank result, no alternate display, no error, just an empty result. I have tried to check the length of the lists that I am passing on, and all of them are non-empty and have correct image addresses of the form: static/test_reconstructions/images/image_04666_img.png (an example of one of the addresses)
I pass the lists and the functions used in the app.py file as follows:
#app.route('/')
def home():
return render_template('iterative_reconst.html', testing_sketchs=testing_sketchs, split_reconstructions=split_reconstructions,
len=len, range=range)
If I understood you correctly, you want a table for each entry in the first list. This table shall have the number of columns corresponding to the entry in the respective second list, headed by the entry in the first list.
For this jinja2 provides the filter length and loop indices. Your code would look something like this.
{% for sketch in testing_sketches %}
<table>
<thead>
<th>Image</th>
{% for i in range(split_reconstructions[loop.index0] | count) %}
<th>Reconstruction-{{i+1}}</th>
{% endfor %}
</thead>
<body>
<tr>
<td>{{ sketch }}</td>
{% for addr in split_reconstructions[loop.index0] %}
<td>{{ addr }}</td>
{% endfor %}
</tr>
</tbody>
</table>
{% endfor %}

Django Render List of Dictionaries to a template

I have a list of dictionaries with same key information. How can I pass this list of dictionaries to a Django template ?
listdict = [{'product':'specs','price':'12'}, {'product':'shoes','price':'30'}]
When trying to send this list via views file to template it fails with an error indicating that only dictionaries are allowed.
Here is the code from views file
return render(request, 'recordings/extensionrecording.html',listDict)
Here is the html block-
<tbody>
{%for list in listDict%}
{%for elements in list%}
<tr>
<td class="body-item fonts-style display-4">{{elements.product}}</td>
<td class="body-item fonts-style display-4">{{elements.price}}</td>
</tr>
Just pass these as an entry of the dictionary:
return render(request, 'recordings/extensionrecording.html',{'listDict': listDict})
Then in the template, you can render these with:
{%for item in listDict %}
{{ item.product }}
{{ item.price }}
{% endfor %}

Django - how to make a link in an html template open an app and pass a objects value as a parameter

Hello I have an app that displays some database information in a table. Inside of the html template I am making an edit link that I want to open another app(page viewLit) while passing a value to it's view. I have added my code below. My question is I am unsure of how to make this links url and pass the object data located inside circuit.circuitid along with it. I haven't been able to find the right way to code this yet and this is just how I thought that this should be done. If anyone has a better idea I am open to suggestions.
search_custom.html(code for link)
{% for circuit in filter.qs %}
<tr>
<td class="actions">
View
</td>
<td>{{ circuit.circuitid }}</td>
</tr>
{% endfor %}
myapp/myapp/urls.py
urlpatterns = [
path('viewLit/', include('viewLit.urls')),
]
myapp/viewLit/urls.py
urlpatterns=[
path('viewLit/circuitid.id', views.viewLit, name='viewLit'),
]
myapp/viewLit/views.py
def viewLit(request, circuitid):
#display records fields here
return HttpResponse("You are at the viewLit page!")
Have a look at the documentation:
Django documentation
myapp/viewLit/urls.py
urlpatterns=[
path('viewLit/(?P<circuit_id>\w+)', views.viewLit, name='viewLit'),
]
html- template:
search_custom.html(code for link)
{% for circuit in filter.qs %}
<tr>
<td class="actions">
View
</td>
<td>{{ circuit.circuitid }}</td>
</tr>
{% endfor %}

Using the slice filter with context data from a Django QuerySet

I am trying to split a list from my model across two columns, using this html code in the template:
< div class ="col-md-6" >
{%for value in object_list %}
<ul>< ahref="/sites/{{value.url}}/">{{value.Site}}</a></ul>
{% endfor %}
I was planning to achieve this with the slice tag to filter the list, e.g.:
{%for value in object_list|slice:"10:20" %}
It does not work however, and I think it might be because I have context data i.e. {{value.Site}}, instead of just {{Site}} for example. This is the corresponding view:
class homeview(ListView):
template_name = 'annual_means/home.html'
def get_queryset(self):
return AnnualMean.objects.values("Site", "url").distinct()
What do I need to do to get the slice to work?
I think, what you need is this:
<table>
<tr>
<th>URL</th>
<th>SITE</th>
</tr>
{% for value in object_list %}
<tr>
<td>{{value.url}}</td>
<td>{{value.Site}}</td>
</tr>
{% endfor %}
</table>
URLs and Sites will be displayed as a table.

Indexing a QuerySet array in django templates

I'm trying to access a queryset array that I passed from the views in the templates. I want to index each entry using a numeric iterator. I'm using a django snippet to get the range of customers. Here is what I have done so far:
{% for cust in customer_comments %}
{% for i in cust|length|get_range %}
<tr>
<td>{{cust.i.customer_id}}</td>
<td>{{cust.i.feedback_detail}}</td>
</tr>
{% endfor %}
{% endfor %}
When I iterate using cust.i.customer_id it displays nothing. But when I use cust.0.customer_id or cust.1.customer_id, it displays what I want it to. Kindly help why i is not working.
Btw this is how I initialized the customer_comments object in views.
customer_comments = []
for i in all_features:
if OpenFeedback.objects.filter(feature_id = i.feature_id).exists():
feedback_obj = OpenFeedback.objects.filter(feature_id = i.feature_id)
customer_comments.append(feedback_obj)
You don't iterate like that in Python or in Django templates: you iterate through the list itself.
{% for customer in cust %}
<tr>
<td>{{customer.customer_id}}</td>
<td>{{customer.feedback_detail}}</td>
</tr>
{% endfor %}

Categories

Resources