django queryset get object from filter - python

for implementation of django-likes i need objects that's gonna use in {% like object %}. if i am using object = Article.objects.get(pk=1) then the like system for particular article working fine. But i am using filter because i need particular filed values for template language. From filter or any other method can i get object so my like system will work ?
views
settings = Article.objects.filter().values('title','content', ..).order_by('-creation_date')
template_var['settings'] = settings
html
{% for x in settings %}
<h2 class="blog-post-title">{{ x.title }}</h2>
<p>{% likes object %} </p>
<p class="blog-post">{{ x.created }} by {{ x.user__username }}</p>
<p>{{ x.content }}.</p>
</div>
{% endfor %}
This is the question how can i get each object out of from settings currently i am getting list because of filter api

If you just use filter(), and don't use values(), then you'll get a queryset of articles
articles = Article.objects.filter().order_by('-creation_date')
If you then loop through articles in your template, you will have article objects that you can use with the template tag. You can still use access attributes like {{ article.created }} as before.
{% for article in articles %}
<h2 class="blog-post-title">{{ article.title }}</h2>
<p>{% likes article %} </p>
<p class="blog-post">{{ article.created }} by {{ article.user.username }}</p>
<p>{{ article.content }}.</p>
</div>
{% endfor %}

Related

How can I show a specific django field in html?

So I want to do what the title says and I don't know how since I'm new at this.
<p>{{ form.description }}</p>
That's the way I show information from different models in a listing using
{% for form in forms %}
But I want to show the description of an specific object.
Thanks in advance.
<form method="post" novalidate>{% csrf_token %}
{{ form.non_field_errors }}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field.errors }}
{{ hidden_field }}
{% endfor %}
<table border="1">
{% for field in form.visible_fields %}
<tr>
<th>{{ field.label_tag }}</th>
<td>
{{ field.errors }}
{{ field }}
{{ field.help_text }}
</td>
</tr>
{% endfor %}
Look this article: [link][1]https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html
Usually django templates will be filled with the data rendered from the django views as dictionary and these will be callable in templates using django template tags "{{tag_name}}"
Here you will be rendering all the forms as a list inside the dictionary so you can show each form by looping it.
{% for form in forms %}
{{form.description}}
{% endfor %}
Like this, you can show each form's data. Instead of this if you only want to show a specific form's data then the proper way is to render the view with the specific form's data and show data in template using the template tag {{form.description}}
Check the django documentation : https://docs.djangoproject.com/en/2.1/topics/templates/

Flask Jinja Route

What is the correct syntax for the href on an html page with Jinja2 code, that allows for navigation between two pages? The first html template has a list of names, while the other has person details.
Here is the code I have for the listOfNames.html page that displays the list of names.
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
Here is the server.py code that gets/puts (correct use of term?) the names on the listOfNames.html.
#app.route('/listNames/<bo>/')
def listNames(bo):
listNames = getListNames(bo)
return render_template('listOfNames.html', listNames=listNames)
This is code for the personInformation.html.
<main role="main" class="col-sm-9 ml-sm-auto col-md-10 pt-3">
<h1>{{ person.firstName }}
{{ person.lastName }}
</h1>
<h2>Office:
{{ person.bo }}
</h2>
<h2>Courses Completed
</h2>
<ul>
{% for rownum, row in personCompleted.iterrows() %}
<li><a href="/courses/{{row.courseId}}">
{{row.courseTitle}}
</a></li>
{% endfor %}
</ul>
</main>
And here is the server.py code.
#app.route('/people/<person_id>')
def person(person_id):
person = getPersonOrganization(person_id)
personCompleted = getPersonCompleted(person_id)
return render_template('personInformation.html', person=person, personCompleted=personCompleted)
The best way to do this is by using the url_for() function from Flask. Here is your new listOfPeople.html template with the link:
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
It's best to not hardcode your URLs in the templates, because if you ever need to reorganize them, then you would need to update URLs all over the place. With url_for() Flask takes care of generating the URLs for you using the information you provided in the app.route decorators.

django template print out by filter id value

I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.

Naming collision in Django Template

I'm having a peculiar problem with a django template I'm setting up: I have a {{ name }} variable that I'm passing to my template, and at the same time, I have a notes list coming from a client-side api that has both a {{ name }} and a {{ body }}.
Whenever I try to print out the name of the note, the other {{ name }} shows up. Which is odd. Here's my code for the notes:
<div class="notes">
{% for note in notes %}
<p><strong>{{ name }}</strong></p>
<p>{{ body }}</p>
{% endfor %}
</div>
Am I doing something wrong? Is there a context operator I can use or something?
<div class="notes">
{% for note in notes %}
<p><strong>{{ note.name }}</strong></p>
<p>{{ note.body }}</p>
{% endfor %}
</div>
This is a common mistake that is made when working with Handlebars alongside Django since Handlebars changes scope automatically for you. All you need to do is refer to the note variable you created with the for loop:
<div class="notes">
{% for note in notes %}
<p><strong>{{ note.name }}</strong></p>
<p>{{ note.body }}</p>
{% endfor %}
</div>
More info here: https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#std:templatetag-for

Accessing DateCreated and DateUpdated in Django/Python for Twilio

Here's my first post! I'm working with a python web app that is recording phone calls. I am able to access client.recordings.uri, but I'm not able to access recordings.datecreated or dateupdated in my code.
{% for recording in recordings %}
<li>{{ recording.datecreated }} {{ recording.duration }} sec.</li>
{% endfor %}
.uri and .duration are valid python attributes, does anyone know how to call the DateCreated attribute? Is it possible with the python module?
{% for recording in recordings %}
<li>
<a href="{{ recording.uri }}.mp3">
{{ recording.date_created }} {{ recording.duration }} sec.
</a>
</li>
{% endfor %}
You'll need to add an underscore between datecreated. You can find a list of all attributes on the recording object here.

Categories

Resources