I was going to create my own friendship view and everything (But I couldn't figure out how to get everything working in a database) So I opted to use something called django-friendship which seems quite good, and It also came with tags to use in the HTML templates but now I am trying to list all the friend requests I can't seem to do a for statement in the tag which is:
{% friend_requests request.user %}
What I would like to be able to do is
{for friend_requests in request.user}
#show this then that#
the link to the github page for the project is this: https://github.com/revsys/django-friendship
If it comes to it I would prefer to create my own friendship implementation, but I don't know where to start.
The friend_requests tag uses a template, which already loops through and creates a list for you:
#register.inclusion_tag('friendship/templatetags/friend_requests.html')
def friend_requests(user):
"""
Inclusion tag to display friend requests
"""
return {'friend_requests': Friend.objects.requests(user)}
source
Here is the template
<ul>
{% for friend_request in friend_requests %}
<li>{{ friend_request }}</li>
{% endfor %}
</ul>
source
To customize it, create a directory friendship and inside it another one called templatetags, and then create a file called friend_requests.html in your app's template directory, and then you can customize the output there.
Related
I have taken a hint from this post
Customising tags in Django to filter posts in Post model
I have created the template tag but I am not sure how to use it in my html. I have a home.html where I want to show three featured post. I am looking for something like {% for post in featured_post %} and then show the post detail.
Also, do I necessarily need to create a featured_posts.html as in the above post because I don't want any extra page for the featured post. I just want them to add on my home page in addition to other stuff.
What I am trying to do is I have created a template tag as under
from django import template
register = template.Library()
#register.inclusion_tag('featured_posts.html')
def featured_posts(count=3):
if Post.is_featured:
featured_posts = Post.published.order_by('-publish')[:count]
return {'featured_posts': featured_posts}
The problem I am facing here is I can't import the Post model from model. My directory structure is somewhat like this:-
I have an app named posts.
Inside that I have models.py and templatetags module and inside the template tag I have blog_tags.py
I couldn't do the relative import.
And then created a new page featured_posts.html as under:-
<ul>
{% for post in featured_posts %}
<li>{{ post.title }} </li>
{% endfor %}
</ul>
Now, I want to use it in my home.html. How can I use it?
Edit:- As mentioned above I could load the models as under:-
from posts.models import Post
home.html
{% load blog_tags %}
{% featured_posts %}
Call your tag. That's it.
or
{% featured_posts count=15 %}
Note, featured_posts here is not the post list (which is iterated in for loop) from context but function name: def featured_posts(count=3). They have the same name in your code and probably this has confused you a little.
I often do things like this in a django template, with django-cms:
{% load cms_tags %}
Imprint
On production, this fails silently, and the href attribute is empty. On development, I'm forced to insert the page with id "imprint", otherwise I get a "DoesNotExist" exception.
How can I improve this situation? Maybe I'm looking for something like
{% if 'imprint'|cms_page_exists %}
...the link and stuff...
Is there a known best practice for this (not quite seldom) use case? Or do you all use it as shown first?
You could assign a tag result to a variable and then check is it empty:
{% page_url 'imprint' as url %}
{% if url %}
Imprint
{% endif %}
Other ways imply creating your own template tag or filters, so the above is the simplest one IMHO.
See also an example in the docs.
I am pretty new to Django. I am fiddling with zinnia to customize it and setting it up with my own theme/template etc. The main content displayed in the default template is following:
{% for object in object_list %}
{% include object.content_template with object_content=object.html_preview continue_reading=1 %}
{% empty %}
I understand that include includes the template inside a page. But what I cannot comprehend is: how do I find the relevant template being rendered? What is content_template? Please help me in understanding this snippet.
The template name (content_template) is being fetched from the database. It is a property of the model ContentTemplateEntry and defaults to zinnia/_entry_detail.html.
I have the following code in my template:
{% for req in user.requests_made_set.all %}
{% if not req.is_published %}
{{ req }}
{% endif %}
{% empty %}
No requests
{% endfor %}
If there are some requests but none has the is_published = True then how could I output a message (like "No requests") ?? I'd only like to use Django templates and not do it in my view!
Thanks
Even if this might be possible to achieve in the template, I (and probably many other people) would advise against it. To achieve this, you basically need to find out whether there are any objects in the database matching some criteria. That is certainly not something that belongs into a template.
Templates are intended to be used to define how stuff is displayed. The task you're solving is determining what stuff to display. This definitely belongs in a view and not a template.
If you want to avoid placing it in a view just because you want the information to appear on each page, regardless of the view, consider using a context processor which would add the required information to your template context automatically, or writing a template tag that would solve this for you.
I have build a web site for a client which has a number of applications. Now he has a new URL registered which he wants to point to the same site, but he wants the look and feel changed. That's basically he wants a new home.html and base.html for the new web site. I can easily add the new site to settings and then change the view for the home page, to display a new home2.html.
However how do I do something like this as expressed in psuedo code in base.html
{% if site_id equals 1 %}
{% include "base1.html" %}
{% endif %}
{% if site_id equals 2 %}
{% include "base2.html" %}
{% endif %}
Any ideas. There are 100s of views on the site and nearly 50 models. I cannot recreate models, and mess around. This needs to be a quick fix.
Thanks in advance
You can create a context processor to automatically add site_id to the context: http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
But I would opt for a different solution. You can simply add an extra template directory per site so Django will try the templates specifically for that site first and fall back to the normal templates if they're not available.
To extend the idea of WoLph with the context processor, I would maybe even add the switching of the template to the context processor which would clean up your templates, as otherwise you may have to repeat the if clause quite often:
from django.contrib.sites.models import Site
def base_template(request):
site = Site.objects.get_current()
template = "base%s.html" % str(site.pk)
return {'BASE_TEMPLATE': template}
And in your template: {% include BASE_TEMPLATE %}
Looks nicer to me than the switching in the templates!
Another solution would be writing a Middleware to set ´request.site´ the current site id.