【问题标题】:For loop in for loop Django templatefor循环中的for循环Django模板
【发布时间】:2021-11-25 11:58:10
【问题描述】:

我想在模板中设置“发送请求/取消请求”功能。
显示问题,如果请求存在“取消”,如果不存在“发送”。 我不知道如何正确获取请求并将“profile”与“to_profile”进行比较。
现在我在'for'循环中遇到了'for'循环...... \

在模板中,它为一个配置文件显示 3 个按钮(来自此事件的相同数量请求) enter image description here

您能否给我一些如何解决、避免或以其他方式做的提示。
谢谢!

请求模型

class EventInviteRequest(models.Model):
    from_event = models.ForeignKey(Event, on_delete=models.CASCADE,
                                   related_name='from_event', null=True)
    to_profile = models.ForeignKey(Profile, on_delete=models.CASCADE,
                                   related_name='to_profile', null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"From {self.from_event} to {self.to_profile}"

查看我尝试使用的

def get_list_of_all_users(request, event_id):
    """Get list of all users"""
    event = Event.objects.get(id=event_id)
    profiles = Profile.objects.all()
    requests = EventInviteRequest.objects.filter(from_event=event)

    context = {'profiles': profiles, 'event': event, 'requests': requests}
    return render(request, 'events/invite_all_profiles_to_event.html', context)

模板

{% extends 'base.html' %}
{% load static %}

{% block title %}All profiles{% endblock %}

{% block content %}
    
    <h3>All profiles:</h3>

    {% for profile in profiles %}
    <div style="display: inline-block; margin-right: 10px; text-align: center;">
        <a href="{% url 'account:profile' profile.id %}">
            {% if profile.photo %}
                <img src="{{ profile.photo.url }}" width="70" height="100">
            {% else %}
                <img src="{% static 'images/no_img/no_img.png' %}" width="70" height="100">
            {% endif %} 
            <h4>{{ profile.user }}</h4>
        </a>
        {% if request.user == profile.user %}
            <p>You</p>
        {% else %}
            {% if profile in request.user.profile.friends.all %}
                <p>Friends</p>
            {% else %}
                {% if profile == request.user.profile %}
                {% else %}
                    <a href="{% url 'account:send' profile.id %}">Add to friends</a>
                {% endif %}
            {% endif %}
            <br>
        {% endif %}
        {% if request.user.profile == profile %}
        {% else %}
        <br>
            The problem here
            {% for req in requests %}
                {% if req.to_profile == profile %}
                    <a href="{% url 'events:cancel_event_invite' event.id profile.id %}">Cancel</a>
                    <br>
                {% else %}
                    <a href="{% url 'events:invite_user_to_event' event.id profile.id %}">Invite to event</a>
                    <br>
                {% endif %}
            {% endfor %}
        {% endif %}
    </div>
    {% empty %}
        <p>No profiles yet</p>
    {% endfor %}
    
{% endblock %}

更新
我找到了解决方案,我不确定它有多好,但 atm 对我来说是有效的。

{% for req in profile.to_profile.all %}
        {% if profile == req.to_profile %}
            <a href="{% url 'events:cancel_event_invite' event.id profile.id %}">Cancel</a>
        {% else %}
            <a href="{% url 'events:invite_profile_redirect' event.id profile.id %}">Invite to event</a>
        {% endif %}
{% empty %}
    <a href="{% url 'events:invite_profile_redirect' event.id profile.id %}">Invite to event</a>
{% endfor %}

【问题讨论】:

    标签: python django templates python-requests


    【解决方案1】:

    尝试做这样的事情。 模型.py

    class EventInviteRequest(models.Model):
        from_event = models.ForeignKey(Event, on_delete=models.CASCADE,
                                       related_name='from_event', blank=True,null=True)
        to_profile = models.ManyToManyField(Profile, on_delete=models.CASCADE,
                                       related_name='to_profile', blank=True,null=True)
        timestamp = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return f"From {self.from_event}"
    
    1. 运行python manage.py makemigrations
    2. 运行python manage.py migrate

    views.py

    def get_list_of_all_users(request, event_id):
        """Get list of all users"""
        event = Event.objects.get(id=event_id)
        profiles = Profile.objects.all()
        requests = EventInviteRequest.objects.filter(from_event=event)
    
        context = {'profiles': profiles, 'event': event, 'requests': requests}
        return render(request, 'events/invite_all_profiles_to_event.html', context)
    

    在您遇到问题的模板中

    {% for req in requests %}
                    {% if profile in req.to_profile.all %}
                        <a href="{% url 'events:cancel_event_invite' event.id profile.id %}">Cancel</a>
                        <br>
                    {% else %}
                        <a href="{% url 'events:invite_user_to_event' event.id profile.id %}">Invite to event</a>
                        <br>
                    {% endif %}
                {% endfor %}
    

    【讨论】:

    • 我已经按照你写的那样做了,但现在在模板中它无法检测到任何现有请求,并且每个配置文件只返回“邀请参加活动”按钮 3。
    • @sergey 尝试删除数据库中的所有行。如果每个配置文件都有 3 个邀请按钮,这意味着您有重复的数据。转到您的管理页面添加数据并进行测试。跨度>
    猜你喜欢
    • 2012-07-25
    • 2021-10-17
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2013-10-26
    • 2010-11-09
    相关资源
    最近更新 更多