对不起,我的英语。
Dgango中的渲染模板存在一些问题。网址正确,但模板返回错误。
截屏:enter image description here
所以URL是正确的,但是它返回了任务列表,我希望得到任务详细信息。我一个人找不到问题。
和项目:enter image description here
VIEWS.PY
from django.shortcuts import render
from .models import Task
from django.views import generic
# Create your views here.
def index(request):
num_tasks = Task.objects.all().count()
num_workers = Task.objects.all().filter(status='N').count()
return render(request, 'index.html', context={'num_tasks':num_tasks,'num_workers':num_workers},)
class TaskListView(generic.ListView):
model = Task
paginate_by = 2
def get_context_data(self, **kwargs):
context = super(TaskListView, self).get_context_data(**kwargs)
context['some_data'] = 'This is just some data'
return context
class TaskDetailView(generic.ListView):
model = Task
MODELS.PY
from django.db import models
from django.utils import timezone
from django.urls import reverse #Used to generate URLs by reversing the URL patterns
# Create your models here.
class Task(models.Model):
STATUS_CHOICES = (
('A', 'Active'),
('R', 'Ready'),
('H', 'Hold'),
('N', 'Dont start')
)
task_name = models.CharField(max_length = 200, help_text = 'Укажите наименование задачи')
date_start = models.DateTimeField(default=timezone.now)
date_end = models.DateTimeField(default=timezone.now)
worker = models.CharField(max_length = 200, help_text = 'Укажите сотрудника, которому назначена задача')
status = models.CharField(max_length = 10, choices = STATUS_CHOICES, default = 'N')
class Meta():
ordering = ('date_start',)
def __str__(self):
return self.task_name
def get_absolute_url(self):
return reverse('task-detail', args=[str(self.id)])
应用程序路径中的URLS.PY:
from django.urls import path
from . import views
from django.conf.urls import url
urlpatterns = [
path('', views.index, name='index'),
path('tasks/', views.TaskListView.as_view(), name = 'tasks'),
path('task/<int:pk>', views.TaskDetailView.as_view(), name = 'task-detail'),
]
PROJECT APP中的URLS.PY
from django.contrib import admin
from django.urls import path, include
from FirstPart import views
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('FirstPart.urls')),
path('FirstPart/', include('FirstPart.urls')),
path('', RedirectView.as_view(url='/FirstPart/', permanent=True)),
]
BASE_GENERIC.HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% block title %}<title>Other topic</title>{% endblock %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
{% block navbar %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'tasks' %}">Task list</a>
</li>
<li class="nav-item dropdown">
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
{% endblock %}
<div class="container-fluid">
{% block pagination %}
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="{{ request.path }}?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
{% endif %}
{% endblock %}
{% block content %}{% endblock %}
</div>
</div>
</body>
</html>
TASK-LIST.HTML
{% extends "base_generic.html" %}
{% block content %}
<h1>Task List</h1>
{% if task_list %}
<ul>
{% for task in task_list %}
<li>
<a href="{{ task.get_absolute_url }}">{{ task.task_name}}</a> ({{task.worker}}) ({{task.status}})
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no tasks on the board.</p>
{% endif %}
{% endblock %}
TASK-DETAIL.HTML
{% extends "base_generic.html" %}
{% block content %}
<h1>Title: {{ task.task_name }}</h1>
<p><strong>task_name:</strong> <a href="">{{ task.task_name }}</a></p>
<p><strong>date_start:</strong> {{ task.date_start }}</p>
<p><strong>date_end:</strong> {{ task.date_end }}</p>
<p><strong>worker:</strong> {{ task.worker }}</p>
{% endblock %}
提前感谢。
class TaskDetailView(generic.DetailView):
model = Task
请更改您的TaskDetailView
并检查