简体   繁体   中英

Django View Problem. Filtered Tickets Are Not Appearing

I'm currently working on a project in Django that is a "bug tracker" or "ticket tracker" as some might call it. The goal here is to create a post or "ticket" that details a bug in the website or software for a development team.

Each "ticket" has general information like name, summary, etc. but also has a "category" for the software languages or bug type(Think like categories for a blog). Every ticket also has a "priority", that is developed in a similar way as to the categories, based on the severity of the bug that a reporter encounters.

I have already developed a way to click on a category name and view a list of tickets within the same category. This is successful and it works great, Naturally. my next step in the process is to use that same method of implementing ticket priorities, My goal is to be able to click on a priority and see a filtered view of all "severe" tickets with that priority. just like how my categories are set up.

Here is my problem:

When I click on a priority (low, medium, high, & severe), it will take me to my ticket_priority.html template for that particular priority, but does not filter or show any ticket of that priority when there is in fact ticket with that priority . All I'm trying to do is to take that same methodology of building out "categories" but implementing them as "priorities" for the team to filter out based on the severity of the bug.

I think there is something wrong with my ticket_priority view, but I just don't know what the deal is and need extra eyes.

First here is my models:

from django.db import models
from django.utils.timezone import timezone
from datetime import datetime
# Create your models here.

#Priority
class Priority(models.Model):
    name = models.CharField(max_length=20,null=True)

    def __str__(self):
        return self.name

#Category
class Category(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name 


#TICKET
class Ticket(models.Model):
    #BugID
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, blank=True, null=True)
    reporter = models.CharField(max_length=100, blank=True, null=True)
    created_on = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    last_modified = models.DateTimeField(auto_now=True)
    
    #BugOverview
    summary = models.TextField(blank=True, null=True)
    url = models.URLField(blank=True, null=True)
    image = models.ImageField(upload_to='media/images/', blank=True, null=True)

    #BugEnvironment
    platform = models.CharField(max_length=20, blank=True, null=True)
    operating_system = models.CharField(max_length=20, blank=True, null=True)
    browser = models.CharField(max_length=20, blank=True, null=True)
    categories = models.ManyToManyField('Category', related_name='posts')
 
    #BugDetails
    steps_to_reproduce = models.TextField(blank=True, null=True)
    expected_result = models.CharField(max_length=100, blank=True, null=True)
    actual_result = models.CharField(max_length=100, blank=True, null=True)
    
    #BugTracking
    priorities = models.ManyToManyField('Priority', related_name='posts')
    assigned_to = models.CharField(max_length=100, blank=True, null=True)
    completed = models.BooleanField(db_column='Completed', default=False)
    datecompletion = models.DateTimeField(db_column='DateCompletion', blank=True, null=True)

    def set_completion(self):
        self.completed = True
        self.datecompletion = datetime.now()
        self.save()
    
    def __str__(self):
        return self.title

Next up is my views.py for ticket_category & ticket_priority:

# TICKET CATEGORY INDEX 

@login_required(login_url='/accounts/login/')
def ticket_category(request, category):
    tickets = Ticket.objects.filter(
        categories__name__contains = category
    )
    context = {
        "category": category, 
        "tickets": tickets
    }
    return render(request, 'ticket_category.html', context)

#################################################################3

# TICKET PRIORITY INDEX

@login_required(login_url='/accounts/login/')
def ticket_priority(request, priority):
    tickets = Ticket.objects.filter(
        priorities__name__contains = priority
    )
    context = {
        "priority": priority, 
        "tickets": tickets
    }
    return render(request, 'ticket_priority.html', context)

And here is my ticket_category.html template:

{% extends "base.html" %}
{% load static %} 
{% block page_content %}

<div class = "container"> 
    
    <div class = "row"> 

        <!-- Post Content Column -->
        <div class="col-lg-8">
            <h1> {{ category | title }} </h1>
            {% for ticket in tickets %}
            <div class="card">
                {% for priority in ticket.priorities.all %}
                <div class="card-header">
                  Ticket #{{ ticket.id}} |
                  <a href="{% url 'ticket_priority' priority.name %}">
                    {{ priority.name }} 
                  </a>
                </div>
                {% endfor %}
                <div class="card-body">
                  <h5 class="card-title">{{ ticket.title }}</h5>
                  <p class="card-text">{{ ticket.summary|truncatewords:20 }}</p>
                  <p class="card-text"> Categories: 
                    {% for category in ticket.categories.all %}
                    <a href="{% url 'ticket_category' category.name %}">
                        {{ category.name }} 
                    </a>
                    {% endfor %}
                  </p>
                  <a href="{% url 'ticket_detail' ticket.pk %}" class="btn btn-primary">View Ticket </a>
                  <a href="{% url 'edit_ticket' ticket.pk %}" class="btn btn-secondary">Edit Ticket </a>
                  <a href="{% url 'completed_ticket' ticket.pk %}" class="btn btn-success"> Ticket Completed </a>
                  <a href="{% url 'delete_ticket' ticket.pk %}" class="btn btn-danger"> Delete Ticket </a>
                </div>
              </div>
              <br>
            {% endfor %}

        
        </div><!-- Column -->

        {% include "sidebar.html" %}
    </div> <!-- Row -->

</div><!-- Container-->
{% endblock %}

And here is my ticket_priority.html template:

{% extends "base.html" %}
{% load static %} 
{% block page_content %}

<div class = "container"> 
    
    <div class = "row"> 

        <!-- Post Content Column -->
        <div class="col-lg-8">
            <h1> {{ priority | title }} </h1>
            {% for ticket in tickets %}
            <div class="card">
                {% for priority in ticket.priorities.all %}
                <div class="card-header">
                    Ticket #{{ ticket.id}} |
                    <a href="{% url 'ticket_priority' priority.name %}">
                      {{ priority.name }} 
                    </a>
                </div>
                {% endfor %}
                <div class="card-body">
                  <h5 class="card-title">{{ ticket.title }}</h5>
                  <p class="card-text">{{ ticket.summary|truncatewords:20 }}</p>
                  <p class="card-text"> Categories: 
                    {% for category in ticket.categories.all %}
                    <a href="{% url 'ticket_category' category.name %}">
                        {{ category.name }} 
                    </a>
                    {% endfor %}
                  </p>
                  <a href="{% url 'ticket_detail' ticket.pk %}" class="btn btn-primary">View Ticket </a>
                  <a href="{% url 'edit_ticket' ticket.pk %}" class="btn btn-secondary">Edit Ticket </a>
                  <a href="{% url 'completed_ticket' ticket.pk %}" class="btn btn-success"> Ticket Completed </a>
                  <a href="{% url 'delete_ticket' ticket.pk %}" class="btn btn-danger"> Delete Ticket </a>
                </div>
              </div>
              <br>
            {% endfor %}

        
        </div><!-- Column -->

        {% include "sidebar.html" %}
    </div> <!-- Row -->

</div><!-- Container-->
{% endblock %}

When the ticket_priority.html template renders, everything appears (base.html, sidebar, priority title, etc) - but no tickets appear - the makes me think that its a problem with my view.

Been stuck on this for over a day. No errors are thrown, just no tickets appear for any priority. They DO appear for each category though. Any help or a point in the right direction would be great. Thank you!

I actually ended up figuring it out on my own and the answer was, naturally, pretty simple.

There wasn't a problem with my model or views, it was my urls. The order of your URL paths DOES matter - though I'm not sure how that works.

Before the fix, my "priority" was routing to my "category" and rendering that view instead.

path("<category>/", views.ticket_category, name="ticket_category"),
path("<priority>/", views.ticket_priority, name="ticket_priority"),

Basically my urls were out of order. I simply moved the priority url above category like so.

path("<priority>/", views.ticket_priority, name="ticket_priority"),
path("<category>/", views.ticket_category, name="ticket_category"),

Now when I click on a link that leads to the priority it displays everything in the particular priority group.

Still not sure what caused it to fix, but its works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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