Accessing DateCreated and DateUpdated in Django/Python for Twilio - python

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.

Related

How to add an attribute to the form tag itself and not just a field in Django?

I am having trouble stopping bots filling in spam while letting through legit users. I have a honeypot field with autocomplete="off" attribute but it doesn't seem to be working. From what i've read, the best cross browser solution is to add autocomplete="false" to the main form tag itself, e.g. <form autocomplete="false">...</form>. What is the best way to do this in Django?
Just do that in your template where the form is added.
In a template you'd typically do something like;
<form autocomplete="false">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.label }}
{{ field }}
{{ field.help_text }}
{% endfor %}
So just add whatever you want to the form tag.
You should probably also have a look at including recaptcha if you've got spam problems.
And remember that v3 doesn't require any selecting street lights etc
https://pypi.org/project/django-recaptcha/

Django/Python HTML recommend friends

So I'm trying to recommend users for a social network based on this condition: If A follows B and B follows C then, we should recommend that A follows C.
I have this code so far which displays mutual friends, all users and those that follow you
<ul>
{% for member in members %}
<li> {{ member.username }}
{% if member in following %}
{% if member in followers %}
↔ is a mutual friend [drop] </li>
{% else %}
← you are following [drop] </li>
{% endif %}
{% else %}
{% if member in followers %}
→ is following you [recip] </li>
{% else %}
[follow] </li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
But I'm stuck on doing this condition so any help would be great, thanks!
Django templates should not encapsulate overly complex logic, by design. They are mainly a design tool, and the main logic should run in python. This is why there are clear limits what you can do in a template without too much hoops.
Here is an simple option, how to implement your requirements
def get_status(user,member):
... calculate follower status here ...
return msg,href,link_text
In the view, where you run your queryset:
members = Members.objects.all()
for member in members:
member.msg,member.href,member.link_text = get_status(member,request.user)
Finally, in the template:
<ul>
{% for member in members %}
<li> {{ member.username }}
↔ {{ member.msg }} [{{ member.link_text }}] </li>
{% endfor %}
</ul>
Note: this is a simple answer. You should really think through this logic, where you want to put it (in the model maybe, as Member method), use reverse for urls etc. But the idea is not to put basic app rules in the template

django queryset get object from filter

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 %}

Django-CMS node tags in breadcrumbs template

I want to create custom breadcrumbs template for my site developed with django-cms.
It should display levels (number) of items in addition to item title. I've read this article and it seems that I should use {{ node.level }} like this, for example:
{% for ance in ancestors %}
<li>
{% if not forloop.last %}
**{{ ance.level }}** - {{ ance.get_menu_title }} <span class="separator">»</span>
{% else %}
<span class="active">**{{ ance.level }}** -{{ ance.get_menu_title }}</span>
{% endif %}
</li>
{% endfor %}
But this is didn't working. I exepected to see "0 - Main ..." but see only " - Main ..."
And I am also curious about get_menu_title because I didn't find that in documentation, but I am find {{ node.title }} and it seems working same as get_menu_title

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

Categories

Resources