python 我如何修复此错误“Reverse for 'poll_details' with arguments '('',)' not found.”

rqdpfwrv  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(101)

我错误地删除了我的django项目中的db.sqlite3文件,我试图运行python manage.py migrate来创建一个新的,但它不起作用。我终于让它与python manage.py migrate --run-syncdb一起工作了。
现在,当我运行服务器并尝试访问主页时(以前的数据库中填充了polls),我希望看到一个空白页面,但我得到了这个错误:
未找到带参数“('',)”的“poll_details”的反向。已尝试1种模式:['poll/(?P <question_id>[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 %}

问题模型

class Question(models.Model):
    question_text = models.CharField(max_length=150)
    pub_date = models.DateTimeField("date-asked", default=datetime.datetime.now())
    description = models.TextField()
    author = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="questions_asked")
    is_active = models.BooleanField(default=True)

    likers = models.ManyToManyField(UserProfile, blank=True, related_name="liked_polls")

    def toggle_status(self):
            if self.is_active:
                self.is_active = False
            else:
                self.is_active = True

    def __str__(self):
        return f"{self.question_text}"
ajsxfq5m

ajsxfq5m1#

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 %}

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

  • 内置模板标签和过滤器 *

相关问题