无法将关键字“date_created”解析到字段中

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

我在 PowerShell 上运行服务器时遇到问题。

entries_detail.html

<article>
    <h2>{{ entry.date_created|date:'Y-m-d H:i' }}</h2>
    <h3>{{ entry.title }}</h3>
    <p>{{ entry.content }}</p>
</article>

entries_list.html

{% for entry in entry_list %}
    <article>
        <h2 class="{{ entry.date_created|date:'l' }}">
            {{ entry.date_created|date:'Y-m-d H:i' }}
        </h2>
        <h3>
            <a href="{% url 'entry-detail' entry.id %}">
                {{ entry.title }}
            </a>
        </h3>
    </article>
{% endfor %}

views.py

from django.views.generic import (
    DetailView,
    ListView,
)
from django.db import models

from .models import Entry

---


class EntryListView(ListView):
    model = Entry
    queryset = Entry.objects.all().order_by("-date_created")


class EntryDetailView(DetailView):
    model = Entry

我尝试在 PowerShell 上运行

python manage.py runserver
,得到了结果:

django.core.exceptions.FieldError: Cannot resolve keyword 'date_created' into field. Choices are: content, data_created, id, title
html django django-views django-templates
1个回答
0
投票

您在

data_created
中的
Entry
模型的
queryset = Entry.objects.all().order_by("-date_created")
字段有拼写错误,您尝试使用
order_by
进行
-date_created
,因此其引发错误无效已提交

你需要编写查询集

queryset = Entry.objects.all().order_by("-data_created")

这里需要更正

-data_created")
而不是
-date_created")

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