簡體   English   中英

Django 將基於 function 的視圖轉換為基於 class 的視圖

[英]Django transforming function based view into class based view

我試圖將基於功能的視圖轉換為基於 class 的視圖,但已經遇到了這個 function 的問題。

@login_required(login_url=settings.LOGIN_URL)
def list_bugs_view(request, id):

queryset = Bug.objects.filter(project=id)
project = Project.objects.filter(id=id)

context = {
    'bug_list': queryset,
    'project': project
}

return render(request, 'bugs/bug_list.html', context)

{% extends 'base.html' %}

<-- bug_list.html -->

{% block content %}

<h1>{{ project.name }}</h1>

<table class="table table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col">Bug ID</th>
            <th scope="col">Name</th>
            <th scope="col">Status</th>
            <th scope="col">Severity</th>
            <th scope="col">Creator</th>
            <th scope="col">Created</th>
            <th scope="col">Actions</th>
        </tr>
    </thead>

    <tbody>
        {% for bug in bug_list %}
            <tr>
                <th scope="row">{{ bug.id }}</th>
                <td>{{ bug.name }}</td>
                <td>{{ bug.status }}</td>
                <td>{{ bug.severity }}</td>
                <td>{{ bug.bug_creator }}</td>
                <td>{{ bug.bug_created }}</td>
                <td></td>
            </tr>
        {% endfor %}
    </tbody>
</table>

{% endblock %}

我已經使用 generic.ListView 來轉換另一個基於 function 的視圖,但不知道如何按項目過濾錯誤,該項目從以下位置接收 id:

# Class based view responsible for creating a project.
class ProjectCreateView(LoginRequiredMixin, CreateView):
model = Project
template_name = 'projects/project_create.html'
form_class = ProjectForm
success_url = '/projects/create'

# This come from LoginRequiredMixin
# Redirects page to LOGIN_URL page (the value of which is set in settings.py) when the user tires accessing the projects/project_create page but is not logged in. 
login_url = settings.LOGIN_URL

def form_valid(self, form):
    return super().form_valid(form)

def get_success_url(self):
    return reverse('projects:project_list')

project_list.html

{% extends 'base.html' %}

{% block content %}

<table class="table table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col">Project ID</th>
            <th scope="col">Name</th>
            <th scope="col">Created</th>
            <th scope="col">Creator</th>
            <th class="text-center" scope="col">Bugs</th>
            <th class="text-center" scope="col">Actions</th>
        </tr>
    </thead>

    <tbody>
        {% for project in project_list %}
            <tr>
                <th scope="row">{{ project.id }}</th>
                <td>{{ project.name }}</td>
                <td>{{ project.project_created }}</td>
                <td>{{ project.project_creator }}</td>
                <td class="text-center"><a class="btn btn-primary" href="{% url 'bugs:bug_list' project.id %}">View</a></td>
                <td class="text-center">
                    <a class="btn btn-info" href="{% url 'projects:project_detail' project.id %}">Details</a>
                    <a class="btn btn-primary" href="{% url 'projects:project_update' project.id %}">Update</a>
                    <a class="btn btn-danger" href="{% url 'projects:project_delete' project.id %}">Delete</a>
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>

{% endblock %}

我真的在努力理解如何使這項工作像其他工作一樣。

這看起來更像是一個DetailView [Django-doc] ,您還可以在其中將查詢集傳遞給queryset

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView

class ListBugsView(LoginRequiredMixin, DetailView):
    pk_url_kwarg = 'id'
    template_name = 'bugs/bug_list.html'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['queryset'] = Bug.objects.filter(project_id=self.kwargs['id'])
        return context

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM