找不到带有关键字参数'{'pk':''}'的'communication-detail'。尝试了1种模式:['communication / communication /(?P <pk> [0-9] +)/ $']

时间:2019-08-23 00:29:15

标签: python django python-3.x django-2.2

我想在我的项目中创建一个新应用(通信),该应用具有与原始应用(调查)非常相似的属性。我基本上将相同的代码复制并粘贴到新应用中,并重命名了所有内容。不幸的是,现在“通讯”的详细信息页面已损坏。正确的pk显示在页面的URL中,并且已正确访问视图。但是,Django调试页面声称已通过的pk无效('')。当我打印item.id时,所有内容都可以正确显示。

我尝试将urls.py中引用的视图更改为我知道可以正常工作且成功的其他视图。我的理论是我的特定观点不起作用?

views.py

class CommunicationView(LoginRequiredMixin, View):
    model = Communication
    fields = ('name', 'language', 'prompt_type')

    def get_success_url(self, **kwargs):
        return self.object.get_prompt_url()
        return reverse_lazy('communication-prompts', args = (self.object.id,))

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if self.request.GET.get('project'):
            context['project'] = Project.objects.get(id=self.request.GET.get('project'))
        return context

    def get_initial(self):
        initial = super().get_initial()
        # context = self.get_context_data(**kwargs)
        if self.request.GET.get('project'):
            initial['project'] = Project.objects.get(id=self.request.GET.get('project'))
        return initial.copy()

class CommunicationDetail(CommunicationView, DetailView):
    template_name='communication/communication_detail.html'

    def get_context_data(self, **kwargs):
        self.object = self.get_object()
        context = super().get_context_data(**kwargs)
        context['project'] = self.object.project
        print(context)
        return context

urls.py

app_name = 'communication'
urlpatterns = [
    path('', include([
        path('', views.CommunicationList.as_view(), name='communication-list'),
        path('add/communication/', views.CommunicationCreate.as_view(), name='communication-create'),
        path('<int:pk>/', include([
            path('', views.CommunicationDetail.as_view(), name='communication-detail'),
            path('run/', views.run_communication, name='run-communication'),
            path('responses/', views.CommunicationResponse.as_view(), name='communication-response'),
            path('prompts/', include([
                path('', views.CommunicationPrompts.as_view(), name='communication-prompts'),
                path('sound/', views.CommunicationPromptSound.as_view(), name='communication-prompt-sound'),
            ])),
            path('questions/', include([
                path('', views.QuestionList.as_view(), name='question-list'),
                path('add/', views.QuestionCreate.as_view(), name='question-create'),
                path('<int:question_pk>/', include([
                    path('', views.QuestionDetail.as_view(), name='question-detail'),
                    path('run/', views.run_question, name='run-question'),
                    path('save/', views.save_response, name='save_response'),
                    path('next/', views.run_question, name='question'),
                    path('update/', views.QuestionUpdate.as_view(), name='question-update'),
                    path('sound/', views.QuestionSound.as_view(), name='question-sound'),
                    path('remove/', views.QuestionDelete.as_view(), name='question-delete'),
                ])),
            ])),
        ]))
    ])),
]

communication_list.html

  <tbody>
    {% for item in communication_list %}
    <tr>
      <td><a href="{% url 'communication:communication-detail' pk=item.id %}{% if project %}?project={{ item.project.id }}{% endif %}">{{ item.name }}</a></td>
      <td>{{ item.project }}</td>
      <td><a href="{% url 'contact-list' %}?communication={{ item.id }}">{{ item.respondents.count }}</td>
      <td><a href="">?</td>
    </tr>
    {% endfor %}
  </tbody>

models.py

class Communication(models.Model):
    VOICE = 'voice'
    SMS = 'sms'
    WHATSAPP = 'whatsapp'
    COMMUNICATION_CHOICES = (
        (VOICE, 'Voice'),
        (SMS, 'SMS'),
        (WHATSAPP, 'WhatsApp'),
    )
    DEFAULT = 'default'
    TEXT = 'text'
    SOUND = 'sound'
    NONE = 'none'
    PROMPT_CHOICES = (
        (DEFAULT, 'Default Prompts'),
        (TEXT, 'Custom Text'),
        (SOUND, 'Custom Sound'),
        (NONE, 'No Prompts')
    )

    name = models.CharField(max_length=255)
    communication_type = models.CharField(max_length=30, choices=COMMUNICATION_CHOICES, default=VOICE)
    language = models.ForeignKey(Language, on_delete=models.SET_NULL, blank=True, null=True)
    project = models.ForeignKey(Project, on_delete=models.SET_NULL, blank=True, null=True)
    prompt_type = models.CharField(max_length=50, choices=PROMPT_CHOICES, default=DEFAULT)
    welcome_prompt = models.ForeignKey(Prompt, on_delete=models.SET_NULL, blank=True, null=True, related_name='welcome_communications')
    text_prompt = models.ForeignKey(Prompt, on_delete=models.SET_NULL, blank=True, null=True, related_name='text_communicationes')
    yes_no_prompt = models.ForeignKey(Prompt, on_delete=models.SET_NULL, blank=True, null=True, related_name='yes_no_communications')
    numeric_prompt = models.ForeignKey(Prompt, on_delete=models.SET_NULL, blank=True, null=True, related_name='numeric_communications')
    goodbye_prompt = models.ForeignKey(Prompt, on_delete=models.SET_NULL, blank=True, null=True, related_name='goodbye_communications')
    respondents = models.ManyToManyField(Contact, blank=True)

    @property
    def responses(self):
        return QuestionResponse.objects.filter(question__communication__id=self.id)

    @property
    def first_question(self):
        return Question.objects.filter(communication__id=self.id
                                       ).order_by('id').first()
    def __str__(self):
        return '%s' % self.name

    def get_absolute_url(self):
        return reverse('communication-detail', kwargs={'pk':self.id})

    def get_prompt_url(self):
        if self.prompt_type == self.TEXT:
            return reverse('communication-prompts', kwargs={'pk':self.id})
        elif self.prompt_type == self.SOUND:
            return reverse('communication-prompt-sound', kwargs={'pk':self.id}) 
        else:
            self.add_prompts(prompts=Prompt.objects.filter(is_default=True))
            return reverse('question-create', kwargs={'pk':self.id})  

    def add_prompts(self, prompts):
        for prompt in prompts:
            setattr(self, '{}_prompt'.format(prompt.category), prompt)
        self.save()
        return self

    def get_prompt(self, kind):
        prompt = getattr(self, '{}_prompt'.format(kind))
        # prompt = getattr(self, '{}_prompt'.format(self.kind.replace('-','_')))
        return prompt.get_twiml_data(self.prompt_type)

    def say_prompt(self, twiml, kind):
        verb, args, kwargs = self.get_prompt(kind=kind)
        getattr(twiml, verb)(*args, **kwargs)
        return twiml

0 个答案:

没有答案