You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django首页无法跳转至详情页求助(服务器500错误)

Django 5.0 详情页跳转500错误排查

我正在跟随codemy.com的教程学习,教程适配Django 3.8或4.X版本,但使用Django 5.0时出现问题。尝试过教程推荐的类视图,现改用函数视图便于调试。首页可正常显示,但点击首页锚点标签跳转至详情页时出现服务器500错误,怀疑是主键传递或URL配置存在问题。


相关代码

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")

models.py

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)

urls.py

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"),
        ]

home.html(首页模板)

<!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>

错误排查与修复

  • 视图函数上下文语法错误
    articleDetail函数中,render的上下文参数使用了集合{object, obj},这是Python语法错误,必须用字典格式传递键值对:

    return render(request, "article_detail.html", {'object': obj})
    

    这个错误是导致服务器500的直接原因,Python无法解析这种格式的上下文参数。

  • 模板URL标签语法错误
    首页模板中,URL标签误用了{$ ... $},正确的Django模板标签格式是{% ... %}:

    <a href="{% url 'article-detail' article.pk %}">{{article.title}}</a>
    

    原语法会生成无效链接,可能传递错误参数或触发额外异常。

  • URL路径末尾建议添加斜杠(可选)
    虽然Django默认兼容无斜杠路径,但为了符合Web规范、避免潜在跳转问题,建议修改URL配置:

    path('article/<int:pk>/', articleDetail, name="article-detail"),
    

修正后的关键代码片段

修正后的articleDetail函数

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

修正后的首页跳转链接

<a href="{% url 'article-detail' article.pk %}">{{article.title}}</a>

内容的提问来源于stack exchange,提问作者Macaire Alexander Bell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 05:50:02