Django简单的朋友模板帮助!

时间:2011-03-28 16:15:31

标签: django-templates

我一直试图永远这样做。

我不明白在模板中放什么,也找不到指南帮助。

  1. 我的个人资料页面 需要朋友列表
  2. 类似的东西:

    {% for friends in Friendship.objects%}
    
    <li>are_friends: {{ friends.are_friends.user.username}}</li>
    <li>is_invited: {{ friends.is_invited}}</li>
    
    {% endfor %}
    

3 个答案:

答案 0 :(得分:1)

对于Django和编程总体来说,这是一个新人,这花了我很长时间才能解决,但我最终到了那里,所以我分享了我的所作所为。首先,虽然代码可能不是你需要的,但你会得到这个想法。我也可能在这里犯了一些错误,或者有更好的方法,所以不要认为这是最正确的方法,但它确实有效。

首先,我将代码分成两个模板。一个用于我在另一个用户的个人资料页面上,另一个用于当我在“管理我的朋友”页面时。请记住,这基本上是纯模板代码,并且没有应用任何样式。这取决于你。

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

对于其他用户的个人资料页面,我有以下代码。 Bear i nmind我有一个配置文件应用程序,它传递了我正在查看的配置文件的用户名并希望与之交互。您可能需要找到另一种方法来实现它。

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

希望这有帮助。

答案 1 :(得分:0)

本指南应该是一个好的开始:

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

答案 2 :(得分:0)

让我们稍微分析一下代码:

Friendship.objects将返回Friendship模型上的默认管理器。此时,您可能希望调用其中一个返回此管理器上的迭代器的方法。例如,以下模板代码遍历所有友谊:

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

但这并没有多大意义。您可能希望针对特定用户迭代友谊。以下代码遍历活动用户的朋友:

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

上面的代码可能无效。我不记得为什么。但是已经有一个标签可以为您提供特定用户的朋友:

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

Rant部分

如果没有阅读源代码,应用程序似乎无法使用。这根本不好。这个应用程序的作者应该写出更好的文档。我应该花些时间来处理这些问题。