我如何修复此错误“找不到带参数‘(’’,)’的‘poll_details’的反转。”

问题描述 投票:0回答:1

我错误地删除了我的 django 项目中的 db.sqlite3 文件,我尝试运行

python manage.py migrate
来创建一个新文件,但它没有用。我终于让它与
python manage.py migrate --run-syncdb
一起工作。 现在的问题是,当我运行服务器并尝试访问主页(以前使用来自前数据库的民意调查填充)时,我收到此错误Reverse for 'poll_details' with arguments '('',)' not found .尝试了 1 种模式:['poll/(?P[0-9]+)$']

因为我的数据库中没有任何东西了(还)我想我应该期待一个空白页。

这是呈现索引页面的索引视图

    def index(request):
        user = request.user

        if user.is_authenticated:
            return HttpResponseRedirect(reverse("poll:home"))
    
        latest_questions = Question.objects.order_by("-pub_date")[:10]  
        return render(request, "poll/index.html", {"questions": latest_questions})

这是索引模板

    {% block body %}
        <h1> Polls </h1>

        <ol>
            {% for question in questions %}

                {% empty %}
                <p> There are no polls yet</p>

                <li>{{ question }}</li> 
                <a href="{% url 'poll:poll_details' question.id %}"> View Poll </a>


            {% endfor %}
        </ol>


    
    
    {% endblock %}
python django
1个回答
-1
投票

像这样在

Django templates
中创建一个支票:

{% block body %}
    <h1> Polls </h1>

    <ol>
        {% if questions %}
            {% for question in questions %}
                <li>{{ question }}</li> 
                <a href="{% url 'poll:poll_details' question.id %}"> View Poll </a>
            {% endfor %}
        {% else %}
            <p> nothing to show </p>
        {% endif %}
    </ol>
{% endblock %}

当数据库中没有搜索时,它可能会显示一条消息

内置模板标签和过滤器

© www.soinside.com 2019 - 2024. All rights reserved.