Search code examples
pythonhtmldjangodjango-views

Why won't my home page redirect to a detail view (Django)


So the context is I'm following a tutorial on codemy.com. Somewhere before Django 5.0 I lost my "magic", the tutorial was written for 3.8 or 4.X maybe. I am showing a function based view although I have tried the class base view as suggested on the codemy youtube. The reason I chose function based view is it was easier for me to debug.

The Home page works fine, however when I try to click on an anchor tag from the home page to redirect to a detail view I get a server 500 Error. I believe the identifier on the homepage that is supposed to locate the document is being passed incorrectly.

views.py

 from django.shortcuts import render
 from django.views.generic import ListView #DetailView
 from django.http import HttpResponse
 from .models import Post


 class Home(ListView):
     model = Post
     template_name = "home.html"


 def articleDetail(request, pk):
     try:
         obj = Post.objects.get(pk=pk)
         return render(request, "article_detail.html", {object, obj})
     except Post.DoesNotExist:
         print("Object number: " + str(pk) + " not found")
         return HttpResponse("Object number: " + str(pk) + " not found")

the model

 from django.db import models
 from django.contrib.auth.models import User


 class Post(models.Model):
     title = models.CharField(max_length=255)
     author = models.ForeignKey(User, on_delete=models.CASCADE)
     body = models.TextField()

     def __str__(self):
         return str(self.title) + ' by: ' + str(self.author)

the urls file

 from django.urls import path,include
 from .views import Home, articleDetail

 urlpatterns = [
     path('', Home.as_view(), name="home"),
     path('article/<int:pk>', articleDetail,name="article-detail"),
         ]

the template for home, works fine until redirect

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Landing!!!</title>
     <h1> Home Pageeeeee</h1>
 </head>
 <body>
 <ul>
     <h2>Articles</h2>
     {% for article in object_list  %}
     <li>
         <a href="{$ url 'article-detail' article.pk %}">{{article.title}}</a>
         <br/>
         By: {{article.author}}
         <p>
             {{article.body}}
         </p>
     </li>
     {% endfor %}
 </ul>
 </body>
 </html>

I think my error is either how I'm passing the primary key to look up the object or how I'm asking the URL file to locate the document


Solution

  • You iterate over "object_list", which your function does not send such context: {object, obj}.

    If you want to access your articles on homepage, you do not need separate view class or function. Since you use django templating, I assume you do not use JS to load hit the view. So now you will have articles that you can to loop. You do not need other function (Remove (order_by("-created_at") if you do not have such column, or if you do not want to order articles).

    class Home(ListView):
        model = Post
        template_name = "home.html"
        context_object_name = "articles"
    
        def get_queryset(self):
            return Post.objects.all().order_by("-created_at")