开发者

Django simple-friends templte help!

I have been trying to do this forever it seems.

I don't understand开发者_Python百科 what to put in the templates and can find no guidelines help.

  1. My Profile page need friends list

something like:

{% for friends in Friendship.objects%}

<li>are_friends: {{ friends.are_friends.user.username}}</li>
<li>is_invited: {{ friends.is_invited}}</li>

{% endfor %}


Well being a bit of newcomer to Django and programming in general this took me ages to work out but I got there in the end so Ill share what I did. First of all though the code might no be exactly what you need but you will get the idea. I also might have made some mistakes here or there is better way to do it so don't assume this is the most correct way but it does work.

First of all I split my code into two templates. One for when I am on the profile page of another user and one for when I am on my "Manage my Friends" page. Please bare in mind this is basically pure template code and there has been no stylings applied. Thats up to you to do.

{% if user|friends %}
    {% with user|friends as list %}
        Friends List
        {% for m in list %}
            {{ m }}
        {% endfor %}
    {% endwith %}
{% else %}
   Search for Friends
{% endif %}


{% if user|friendshiprequests %}
    {% with user|friendshiprequests as list %}
        {% if list.received %}
            Friendship Requests Received                
            {% for m in list.received %}
                    {{ m }}
                        <a href="{% url friendship_accept m.from_user %}">Accept Request</a>
                        <a href="{% url friendship_decline m.from_user %}">Decline Request</a>
                        <a href="{% url user_block m.from_user %}">Block User</a>
            {% endfor %}
        {% endif %}

        {% if list.sent %}
            Pending Friend Requests Sent by You
            {% for m in list.sent %}
                {% if not user|isblockedby:m.to_user %}
                    {{ m.to_user }}
                        <a href="{% url friendship_cancel m.to_user %}">Cancel Request</a>
                {% endif %}
            {% endfor %}
        {% endif %}
    {% endwith %}
 {% endif %}

  {% if user|blocks %}
      {% with user|blocks as list %}
          {% if list.applied %}
              List of Blocked Users
              {% for m in list.applied %}
                  {{ m }}
              {% endfor %}
          {% endif %}
      {% endwith %}
  {% endif %}

For the profile page of another user I have the following code. Bear i nmind I have a profile application which is passing in the username of the profile I am looking at and want to interact with. You may need to find another way to do it.

    {% if not user == profile.user %}
        {% if not user|isblockedby:profile.user %}
            {% if not profile.user|isblockedby:user %}
                {% if not user|isfriendswith:profile.user %}
                        {% if not profile.user|isfriendshiprequest:user %}
                            {% if user|isfriendshiprequest:profile.user %}
                                You have already sent a friend request                                    
                                    <a href="{% url friendship_cancel profile.user %}">Cancel Friend Request</a>                                   
                            {% else %}                                   
                                    <a href="{% url friendship_request profile.user %}">Send Friend Request</a>                                  
                                                                        <a href="{% url user_block profile.user %}">Block User</a>                                   
                            {% endif %}
                        {% else %}
                        {% endif %}
                {% else %}
                    You and {{ profile.user}} are friends                       
                        <a href="{% url friendship_delete profile.user %}">trans Unfriend</a>                                                
                        <a href="{% url user_block profile.user %}">Block User</a>                        
                {% endif %}
            {% else %}
                You have blocked this User                    
                    <a href="{% url user_unblock profile.user %}">Unblock User</a>                    
            {% endif %}
        {% else %}
            You have been blocked by this user
        {% endif %}
    {% endif %}

    {% if not user == profile.user %}
        {% if not user|isblockedby:profile.user %}
            {% if not profile.user|isblockedby:user %}
                {% if not user|isfriendswith:profile.user %}
                    {% if profile.user|isfriendshiprequest:user %}
                            Accept Friendship Request                                
                                <a href="{% url friendship_accept profile.user %}">Accept Request</a>                                
                                <a href="{% url friendship_decline profile.user %}">Decline Request</a>                               
                                <a href="{% url user_block profile.user %}">Block User</a>
                    {% endif %}
                {% endif %}
            {% endif %}
        {% endif %}
    {% endif %}

Hope this helps.


This guide should be a good start:

http://docs.djangoproject.com/en/dev/ref/templates/


Let's analyze the code a little bit:

Friendship.objects would return the default manager on Friendship model. At this point you probably want to call one of the methods that return an iterator on this manager. For example the following template code iterates over all Friendship's:

{% for friendship in Friendship.objects.all %}

But this doesn't make a lot of sense. You probably want to iterate over Friendships for a particular user. The following code iterates over friends of the active user:

{% for friendship in user.friendship.friends.all %}
  {{ friendship.user.username }}
{% endfor %}

The code above may not work. I can't remember why. But there is already a tag that gives you friends of a specific user:

{% load friends_tags %}
{% friends_of user %}
{% for friend in friends %}
  {{ friend.username }}
{% endfor %}

Rant Section

It seems the app is not usable without reading the source. This is not good at all. The author of this app should have written better documentation. I should allocate some time to work on these issues sometime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜