0

Help, i am seeing the error above and i have tried many edits to no avail.

  #template   
  <header class="w3-container w3-blue">
    <h1><a href="{% url 'details' pk=Testimony.pk %}">{{Testimony.Title}}</h1>
  </header>

    #my urls
    re_path('<int:pk>/', views.detail.as_view(), name='details'),



    #my views.py
   class detail(DetailView):
    model = Testimony
    template= 'details.html'

    def TestimonyDetailView(request, pk):
        Testimony = get_object_or_404(Testimony, pk)
        return render(request, self.template, context={'Testimony': Testimony})
HAN YAO FOONG
  • 137
  • 1
  • 8
  • 4
    `return render(request, self.template, context={'Testimony': Testimony})` you're not passing `Jeans` to your context. Try this: `context={'Jeans': Jeans, 'Testimony': Testimony}. Your template does not know what Jeans.pk is if you don't pass the variable Jeans to your context – Matt Jan 06 '19 at 19:28
  • Are your urls namespaced properly? If they are within an app, you may have to use app_name:url_name in template url – coderDude Jan 06 '19 at 20:17
  • Seemingly so. My other urls work fine. – HAN YAO FOONG Jan 06 '19 at 20:29

1 Answers1

0

In order to use a variable coming from a view to templates, you need to send it through a context.

In your case, Jean as a variable is not present in the template since you did not send it via the context context={'Testimony': Testimony}.

to solve it you need it to add a key to the dict(context) representing the variable as a string.

return render(request, self.template, context = {'Jean':Jean,'Testimony': Testimony})

NOTE that you can send variables through context_processors as well.

This answer will help you understand the process.

Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
  • I just edited my code to the above. Still seeing the same error. Any ideas? Any help would be appreciated. – HAN YAO FOONG Jan 06 '19 at 20:14
  • Error: Reverse for 'details' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['\\.(?P[a-z0-9]+)/?$', '/']. It points to the line in my template above. – HAN YAO FOONG Jan 06 '19 at 20:14
  • wow you are using CBV, you need to send the variable via `get_context_processor`. and I am not sure that your view is correct – Lemayzeur Jan 06 '19 at 20:20