1

i want to display all objects of category_request in status Rejected, but it seems that im doing something wrong here. I'm quite new to Django/Python, if somebody has an idea please scream it out to me ;)

models.py

class CategoryRequests(models.Model):
    author = models.ForeignKey(User, related_name='status', on_delete=models.CASCADE)
    title = models.CharField(max_length=20, verbose_name="Title")
    description = models.TextField(max_length=175, null=True, blank=True)
    cover = fields.ImageField(
        blank=True,
        null=True,
        validators=[default_image_size, default_image_file_extension],
        upload_to=get_file_path_user_uploads,
        dependencies=[FileDependency(processor=ImageProcessor(format='JPEG', quality=99, scale={'max_width': 1000, 'max_height': 1000}))])
    published_date = models.DateField(auto_now_add=True, null=True)
    status = StatusField()
    STATUS = Choices('Waiting', 'Rejected', 'Accepted')

views.py

def view_profile_category_requests_rejected(request, pk=None):
    if pk:
        user = get_user_model.objects.get(pk=pk)
        category_request = CategoryRequests(pk=pk)
    else:
        user = request.user
    args = {'user': user,
            'category_request': category_request}
    return render(request, 'myproject/_from_home/category_request_rejected_from_home.html', args)

Template.html

{% if user.category_request.status == Rejected %}
        {% if user.category_request_set.count == 0 %}
            <div class="centercontentfloat">
                <div class="card border-primary mb-3">
                    <div class="card-header">No Post's available yet  ...</div>
                    <div class="card-body text-primary">
                        <p class="card-text">You did not created any posts at all, go ahead and tell the world what it use to know!</p>
                        <a href="{% url 'post_new' %}">
                            <button class="btn btn-dark" type="submit">Create new Post <i class="fa fa-arrow-right"></i></button>
                        </a>
                    </div>
                </div>
            </div>
        {% else %}
            <h4 class="sub-titel-home">Rejected Request(s):</h4>
            <table class="table center class-three-box">
                <thead>
                <tr>
                    <th style="font-size: small">Title</th>
                    <th style="font-size: small">Comment's</th>
                    <th style="font-size: small">Created</th>
                </tr>
                </thead>
                <tbody>
                {% for category_request in user.category_request_set.all %}
                    <tr>
                        <td><a href="{% url 'category_request_detail' pk=category_request.pk %}">{{ category_request.title }}</a></td>
                        <td>{{ category_request.comment_set.count }}</td>
                        <td>{{ category_request.published_date }}</td>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% endif %}
    {% else %}
    <h1 class="center">Rejected-Status filter does not work</h1>
    {% endif %}

    </div>

So how do i filter only category requests in status rejected and display it in my Table?

Thanks in advance.

1 Answers1

0

Business logic, such as filtering, etc. is typically written at the view level, not at the template level. In fact this is one of the reasons why Django restricted the template syntax, such that making function calls, etc. is quite hard.

What we basically want is all the CategoryRequests where the author is the User with the given primary key pk, and where the status is Rejected. We can obtain this with a filter like:

CategoryRequests.objects.filter(author__pk=pk, status='Rejected')

So we can define such queryset in the view:

def view_profile_category_requests_rejected(request, pk=None):
    if pk:
        category_requests = CategoryRequests.objects.filter(
            author__pk=pk, status='Rejected'
        )
    else:
        category_requests = CategoryRequests.objects.filter(
            author=request.user, status='Rejected'
        )
    return render(
        request,
        'myproject/_from_home/category_request_rejected_from_home.html', 
        {'category_requests': category_requests}
    )

in the template, we can then iterate over category_requests:

{% if not category_requests %}
    <div class="centercontentfloat">
        <div class="card border-primary mb-3">
            <div class="card-header">No Posts available yet...</div>
            <div class="card-body text-primary">
                <p class="card-text">You did not created any posts at all, go ahead and tell the world what it use to know!</p>
                <a href="{% url 'post_new' %}">
                    <button class="btn btn-dark" type="submit">Create new Post <i class="fa fa-arrow-right"></i></button>
                </a>
            </div>
        </div>
    </div>
{% else %}
    <h4 class="sub-titel-home">Rejected Request(s):</h4>
    <table class="table center class-three-box">
        <thead>
        <tr>
            <th style="font-size: small">Title</th>
            <th style="font-size: small">Comment's</th>
            <th style="font-size: small">Created</th>
        </tr>
        </thead>
        <tbody>
        {% for category_request in category_requests %}
            <tr>
                <td><a href="{% url 'category_request_detail' pk=category_request.pk %}">{{ category_request.title }}</a></td>
                <td>{{ category_request.comment_set.count }}</td>
                <td>{{ category_request.published_date }}</td>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endif %}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555