【问题标题】:Keep getting Server Error - 500不断收到服务器错误 - 500
【发布时间】:2017-06-06 18:04:08
【问题描述】:

每次我在我的网站上请求博客文章时,都会收到服务器错误 500。过去 3 天我一直在尝试调试它,但我无法弄清楚发生了什么。这是博客详细信息的views.py:

def blog_detail():

    post = Post.objects.get(request_post)


    try:
        Next_Post_id = (post.id + 1)
        Next_Post = Post.objects.get(id=Next_Post_id)
        Next_Post = Next_Post.id
    except ObjectDoesNotExist:
        Next_Post = None

    # Previous Post
    try:
       Previous_Post_id = (post.id - 1)
       Previous_Post = Post.objects.get(id=Previous_Post_id)
       Previous_Post = Previous_Post.id
    except ObjectDoesNotExist:
        Previous_Post = None

    context = {'post': post, 'Next_Post': Next_Post, 'Previous_Post':  Previous_Post}
    return render(request, "BlogHome/pages/post.html", context)

这里是 post.html 模板:

{% extends "BlogHome/includes/WELL.html" %}

{% block content %}
<script>
document.title = "Pike Dzurny | {{post.title}}"
</script>
    <div class="container-fluid text-center">
        <center>
            <div class="well" id="WellPost">
                <div class="container-fluid">
                    <h2 align="center" id="TitleText">{{post.title}}</h2>
                    <h3 align="center" id="BodyText">{{ post.date|date:"m-d"}}</h3>


            <h3 align="left">{{ post.body|safe }}</h3>


                {% if post.id == 1 %}
                <ul class="pager">
                    <li class="previous disabled"><a href="/blog/{{ Previous_Post.id }}"><span
                            aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next "><a href="/blog/{{ Next_Post.id }}">Newer <span
                            aria-hidden="true">&rarr;</span></a></li>
                    <h1>hi 1</h1>
                </ul>

                {% if Next_Post is defined %}
                <ul class="pager">
                    <li class="previous disabled"><a href=""><span aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next"><a href="/blog/{{ Next_Post.id }}">Newer <span aria-hidden="true">&rarr;</span></a>
                    </li>
                </ul>
                <h1>2</h1>

                {% Previous_Post is defined %}
                <ul class="pager">
                    <li class="previous"><a href="/blog/{{ Previous_Post.id }}"><span aria-hidden="true">&larr;</span>
                        Older</a></li>
                    <li class="next disabled"><a href="">Newer <span aria-hidden="true">&rarr;</span></a></li>
                </ul>
                <h1>3</h1>

                {% else %}
                <ul class="pager">
                    <li class="previous disabled"><a href=""><span aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next disabled"><a href="">Newer <span aria-hidden="true">&rarr;</span></a></li>
                </ul>
                <h1>4</h1>
                {% endif %}


            </div>
            <div class="container-fluid">

            </div>
    </center>

</div>

{% endblock %}

Post 是包含博客文章的模型。我无法找到为什么当我请求博客页面时会引发错误的原因。有谁知道为什么?

【问题讨论】:

  • 如果此代码是精确复制和粘贴 - 您有缩进问题。
  • 如果你在开发环境中调试它,在你的设置中设置DEBUG = True,然后发布堆栈跟踪。如果您只在生产环境中看到这一点,请尝试在开发环境中复制它。
  • 另外,如果此代码是复制粘贴,您的 request_post 变量在哪里定义/分配?
  • DEBUG = False 时,Django 通常会通过电子邮件将内部服务器错误与回溯一起发送给您。您必须确保它正常工作。检查djangodeployment.com/2017/01/18/…

标签: python django python-3.x django-templates django-views


【解决方案1】:

我知道发生了什么。在@Tnerual 发表评论后,我检查了我的 urls.py 并意识到 request_post 没有定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多