如何解决Django中的字段错误问题?

时间:2018-07-14 07:48:51

标签: postgresql bootstrap-4 django-2.0

当我尝试使用以下代码时,我尝试从列表页面导航到详细信息页面。我有错误指出该字段错误。为此,我尝试在模型中添加一个空的Slug字段,它显示页面未找到错误。

enter image description here

 #urls.py
 from django.urls import path

 from .views import (TaskListView,TaskDetailView)


 app_name = 'Tasks'


 urlpatterns = [

     path('', TaskListView.as_view(), name='list'),
     path('<slug:slug>/', TaskDetailView.as_view(), name='detail'),
  ] 



 #views.py
 from django.shortcuts import render
 from django.http import HttpResponse
 # Create your views here.
 from django.views.generic import ListView, DetailView, View

 from .models import Taskmanager


 def home(request):
   return render(request, 'home.html')


 class TaskListView(ListView):
  template_name = 'Tasks.html'
  model = Taskmanager
  context_object_name = 'data'


 class TaskDetailView(DetailView):
   template_name = 'detail.html'
   model = Taskmanager
   context_object_name = 'data'


 #models.py
 from django.db import models
 from django.urls import reverse

 # Create your models here.
 week_number = (("week01", "week01"),
           ("week02", "week02"),
           ("week03", "week03"),
           ("week04", "week04"),
           ("week05", "week05"),
           ("week06", "week06"),
           ("week07", "week07"),
           ("week08", "week08"),
           ("week09", "week09"),
           ("week10", "week10"),
           ("week11", "week11"),
           ("week12", "week12"),
           ("week13", "week13"),
           ("week14", "week14"),
           ("week15", "week15"),
           ("week16", "week16"),
           ("week17", "week17"),
           ("week18", "week18"),
           ("week19", "week19"),
           ("week20", "week20"),
           ("week21", "week21"),
           ("week22", "week22"),
           ("week23", "week23"),
           ("week24", "week24"),
           ("week25", "week25"),
           ("week26", "week26"),
           ("week27", "week27"),
           ("week28", "week28"),
           ("week29", "week29"),
           ("week30", "week30"),
           ("week31", "week31"),
           ("week32", "week32"),
           ("week33", "week33"),
           ("week34", "week34"),
           ("week35", "week35"),
           ("week36", "week36"),
           ("week37", "week37"),
           ("week38", "week38"),
           ("week39", "week39"),
           ("week40", "week40"),
           ("week41", "week41"),
           ("week42", "week42"),
           ("week43", "week43"),
           ("week44", "week44"),
           ("week45", "week45"),
           ("week46", "week46"),
           ("week47", "week47"),
           ("week48", "week48"),
           ("week49", "week49"),
           ("week50", "week50"),
           ("week51", "week51"),
           ("week52", "week52"),
           ("week53", "week53"),

           )
class Taskmanager(models.Model):
   CurrentSprint = models.CharField(max_length=10, default="week01", 
   choices=week_number)
   todaydate = models.DateField()
   taskname = models.SlugField(max_length=200)
   testrun = models.URLField(max_length=300)
   comments = models.CharField(max_length=300)
   assignedto = models.EmailField(max_length=70)



   def __str__(self):
      return self.taskname

   def get_absolute_url(self):
      return reverse('Tasks:detail', kwargs={'slug': self.taskname})


 #Tasks.html
 <a href="{% url 'Tasks:detail' slug='detail'%}"> {{Taskmanager.todaydate}} 
 </a>

当我单击链接时需要一个输出,它需要导航到详细信息页面,该页面需要显示任务的详细信息。

1 个答案:

答案 0 :(得分:0)

尝试添加

#views.py
class TaskDetailView(DetailView):
...
def get_object(self):
    instance = get_object_or_404(Taskmanager, slug=self.kwargs['slug'])
    return instance

#models.py
django.db.models.signals import pre_save

class Taskmanager(models.Model):
    ...
    taskname = models.CharField(max_length=200)
    slug = models.SlugField(max_length=100)
    ...

    def pre_save_Taskmanager_receiver(instance, *args, **kwargs):
        if not instance.slug:
            instance.slug = instance.taskname

    pre_save.connect(pre_save_Taskmanager_receiver, sender= Taskmanager)

# task.html
<a href="{% url 'Tasks:detail' slug=data.slug %}"> {{ data.todaydate }}</a>