Django/Python HTML recommend friends - python

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

Related

how to use name space instead of hard coded url in django templales for pagination

i am newbie in django,i have django templates where i have added the following code for pagination.here you can see that i have apply hard coded url for pagination.but i don't want to use hard coded url ,i want to use namespace instead of the hard coded url.how can i do this.
Template:
<span class="page-links">
{% if page_obj.has_previous %}
{% if query_string %}
previous
{% else %}
previous
{% endif %}
{% endif %}
</span>
my urls:
url(r'^(?P<chain_pk>[0-9]+)/full/combination/$',
CombinationSearchList.as_view(), name='dash_combination_search_list'),
Update
my django version is 1.6
<span class="page-links">
{% if page_obj.has_previous %}
{% if query_string %}
previous
{% else %}
previous
{% endif %}
{% endif %}
</span>
Please note that I have used point.id on the url to provide the value for the point.id in your hardcoded url. This can be any other variable, like object.pk or object.id or any other context variable. If this is not clear post again with the view code of the page and I can help.

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

Get nested dict items using Jinja2 in Flask

for this dictionary with this Flask controller
projects = {
'life-calc':{'url':'life-calc',
'title': 'Life Calculator'},
'text-game':{'url':'text-game',
'title':'Text Adventure'},
'fill-it-up':{'url':'fill-it-up',
'title':'Fill It Up'},
'rock-paper-scissors':{'url':'rock-paper-scissors',
'title':'Rock, Paper, Scissors'},
'bubble-popper':{'url':'bubble-popper',
'title':'Bubble Popper'}
}
#app.route('/')
def index():
return render_template("index.html",
projects = projects)
and the template as such
<h1>
List of My Projects
</h1>
<ol>
<li>
Life Calculator
</li>
<li>
Adventure Game
</li>
<li>
Fill It Up
</li>
<li>
Rock Paper Scissors
</li>
<li>
Bubble Popper
</li>
</ol>
<p>test section below</p>
<ol>
{% for project in projects %}
<li>{{ project['title'] }} </li>
{% endfor %}
</ol>
{% endblock %}
How can I access the items in the dict to print a list of my projects as in the HTML above the test?
I solved my own problem with help from Rendering a python dict in Jinja2 / Werkzeug
The template block should be
{% for key, value in projects.iteritems() %}
<li><a href={{value['url']}}>{{value['title']}}</a></li>
{% endfor %}
But I'm still curious as to how to access further nested dictionaries, and if this is the smartest way to create a simple menu.
I think you want to know how access the nested dict in template
If you think I got your question
Generally, This is the way to access the nested dictionary items in dictionary.
If the iterables are getting nested further just you have to increase the forloop depth level whether it is list or dict.
Here I am giving just a generic example in my own way for your understanding
Data:
parent_dict = {1: {'A':'val1','B':'val2'}, 2:{'C':'val3','D':'val4'}}
iteration in jinja2:
{% for key,parent_dict_item in parent_dict.items() %}
{% for key2, nested_value in parent_dict_item.items() %}
<li>{{ nested_value }} </li>
{% endfor %}
{% endfor %}
Answer:
<li>val1 </li>
<li>val2 </li>
<li>val3 </li>
<li>val4 </li>
Instead of expanding the key and value in the loop, you can also use the key to reference the item in the dict itself:
{% for project in projects %}
<li>{{ projects[project].title }} </li>
{% endfor %}

How To Access Many to Many Attribute in Django

I am very new to web development and I have created a sample project using Django. So far I have a Django powered page that displays the contents of one of my database's model objects which is called Publications. The code I have in my view template is:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% endfor %}
</ul>
</body></html>
This works fine, but now I would like to access and display a many to many attribute on Publications called Tags. I have tried adding another for tag as follows:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% for tag in publication_list.tags %}
<li>{{ tag.title }} </li>
{% endfor %}
{% endfor %}
</ul>
</body></html>
I realize this is quite wrong, but I don't see how to access the Tags model. For reference, my function for displaying the publications in the view is:
def display_publications(request):
publication_list = Publication.objects.order_by('title')[:10]
return render(request, 'publications.html', {'publication_list': publication_list})
And my Publications and Tag Models are:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
Any help is appreciated.
What you are doing only accesses the ManyRelatedManager. You need to specify a query against that manager. In python, it would be:
publication.tags.all()
In a django template it would be:
{% for tag in publication.tags.all %}
{{ tag }}
{% endfor %}
This should be covered in the official documention on many-to-many relationships.
Edit: Here's a good example of how many-to-many relationships work: https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/
Because you seem to be having some trouble with this, given your comments on the other question, here are the changes to the template. You do not need to modify the view at all from what you have given above.
{% for publication in publication_list %}
<li>{{ publication.title }}
<ul>
{% for tag in publication.tags.all %}
<li>{{ tag.title }} </li>
{% endfor %}
</li>
</ul>
{% endfor %}

Control flow in Django template language

I have the following code, written inside a Django template.
{% if user.is_authenticated %}
<div style="float: right;">
{% for rel in RELATIONS %}
{% if rel.group_id == g.id %}
Unsubscribe
{% endif %}
{% else %}
Enrol
{% endfor %}
</div>
{% endif %}
The purpose of the code is to check if there is a match and then print out the unsubscribe tag. If there is not match print out the subscribe tag.
The reason I am having trouble doing this is because you in Django templates, I read that you can't have variables (i.e. a True or False).
UPDATE: (The question)
I want to only print out the Unsubscribe/subscribe button once. Print out the unsubscribe button only if there is a match inside the for loop. Otherwise print out the subscribe button if there is no match (i.e. no match at all for rel.group_id == g.id )
UPDATE 2:
While doing some research earlier I found this: https://code.djangoproject.com/ticket/3481
This might add some context to my problem.
Thank you for your help.
Seems like your if/else/endif are mixed up. Try
{% if user.is_authenticated %}
<div style="float: right;">
{% for rel in RELATIONS %}
{% if rel.group_id == g.id %}
Unsubscribe
{% else %}
Enrol
{% endif %}
{% endfor %}
</div>
{% endif %}
UPDATE
You want to check if g is in relations and make your decission based upon that.
The way I usually solve this is to create a function in my view that does this check and passes that allong to the view. Have a look here.

Categories

Resources