简体   繁体   English

如果存在用户创建的方法实例,则显示它们,否则显示字符串 Django

[英]if there are instances of method that user created display them else display a string Django

code:代码:

views.py:视图.py:

@login_required(login_url='loginPage')
def boardPage(request):
    if Board.objects.filter(user=request.user).exists():
        boards = get_list_or_404(Board, user=request.user)
        context = {'boards': boards}
        return render(request, 'tasks/boards.html', context)
    else:
        context = {'boards': False}
        return render(request, 'tasks/boards.html', context)

boards.html:板。html:

{% extends 'tasks/main.html' %}
{% block content %}
    <center>board page<hr>
        <a href="{% url 'boardAdd' %}">add board</a> <br><br>
        your boards: <br> <br>
        {% if boards is false %}
            <p>you don't have any boards... Have you tried creating one? ;)</p>
        {% else %}
            {% for board in boards %}
                <a href="{% url 'insideBoard' board.id %}">{{board}}<br></a>
            {% endfor %}
        {% endif %}
        <br>
        <br>
        <hr>
        <a href="{% url 'logoutPage' %}">Logout</a>
    </center>
    {% if messages %}
        {% for message in messages %}
            <u><br>{% if message.tags %} {% endif %}>{{ message }}</u>
         {% endfor %}
    {% endif %}
{% endblock %}

models.py:模型.py:

class Board(models.Model):
    title = models.CharField(max_length=50, null=True)
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

class Task(models.Model):
    title = models.CharField(max_length=200, null=True)
    done = models.BooleanField(default=False, null=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
    board = models.ForeignKey(Board, null=True, on_delete=models.CASCADE)
    due_to = models.DateField(null=True, validators=[dateValidation])

    def __str__(self):
        return self.title

every time user deletes all of his/her boards or new user visits that page (new users doesn't have any boards yet, they are able to create one there) I get "TypeError at / 'bool' object is not iterable" how do I need to change the code to see a string instead of instances of class on that page?每次用户删除他/她的所有板或新用户访问该页面时(新用户还没有任何板,他们可以在那里创建一个)我得到“TypeError at / 'bool' object is not iterable” 如何我是否需要更改代码以在该页面上查看字符串而不是 class 的实例?

You don't need to add new variable, just check if array is empty.您不需要添加新变量,只需检查数组是否为空。 You are getting error as you are trying to iterate over Boolean in your template ( {'boards': False} )当您尝试在模板中迭代Boolean时遇到错误( {'boards': False}

@login_required(login_url='loginPage')
def boardPage(request):
        boards = Board.objects.filter(user=request.user)
        context = {'boards': boards}
        return render(request, 'tasks/boards.html', context)

{% extends 'tasks/main.html' %}
{% block content %}
    <center>board page<hr>
        <a href="{% url 'boardAdd' %}">add board</a> <br><br>
        your boards: <br> <br>
        {% if not boards %}
            <p>you don't have any boards... Have you tried creating one? ;)</p>
        {% else %}
            {% for board in boards %}
                <a href="{% url 'insideBoard' board.id %}">{{board}}<br></a>
            {% endfor %}
        {% endif %}
        <br>
        <br>
        <hr>
        <a href="{% url 'logoutPage' %}">Logout</a>
    </center>
    {% if messages %}
        {% for message in messages %}
            <u><br>{% if message.tags %} {% endif %}>{{ message }}</u>
         {% endfor %}
    {% endif %}
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM