function'对象没有属性'objects >>

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

我正在尝试基于一个休假的教程来遍历我的数据库,但是当我进入“列表”应用页面时,我得到了Error: AttributeError at /listings/ - 'function' object has no attribute 'objects'

我已经尝试过将变量命名为其他名称,因此它不与模型共享名称,但是不管我做什么。我仍然遇到错误

所以这是我来自商家信息应用的views.py

from django.shortcuts import render
from listings.models import listing 
# Create your views here.


def index(request):
  listings = listing.objects.all()

  context = {
    'patients' : listings
  }
  return render(request, 'listings/listings.html')

def listing(request):
  return render(request, 'listings/listing.html')

这是我的urls.py

from django.urls import path 

from .import views

urlpatterns = [
  path('', views.index, name ='listings'),
  path('<int:listing_id>', views.listing, name ='listing'),

在这里,我遍历数据并将其估算为给定格式

 {% if listings %}
        {% for listing in listings %}
        <div class="col-md-6 col-lg-4 mb-4">
          <div class="card listing-preview">
            <div class="card-body">
              <div class="listing-heading text-center">
                <h4 class="text-primary">Jane Doe</h4>
                <p>
                  <i class="fas fa-map-marker text-secondary"></i> Bishopstown Co,Cork</p>
              </div>
              <hr>
              <div class="row py-2 text-secondary">
                <div class="col-6">
                  <i class="fas fa-asterisk"> Risk:</i> Low</div>
              </div>
              <hr>
              <div class="row text-secondary pb-2">
                <div class="col-6">
                  <i class="fas fa-clock"></i> 2 days ago</div>
              </div>
              <hr>
              <a href="listing.html" class="btn btn-primary btn-block">More Info</a>
            </div>
          </div>
        </div>
        {% endfor %}
      {% else %}  
        <div class="col-md-12">
          <p>No Patients</p>
        </div>
      {% endif %}

我希望看到数据库中有一个条目,但是却得到了Error: AttributeError at /listings/ - 'function' object has no attribute 'objects'request <WSGIRequest: GET '/listings/'>

[我正在尝试基于一个休假的教程来遍历数据库,但是,当我进入“列表”应用页面时,出现错误:/ listings /的AttributeError-“函数”对象没有属性'。 。

python django
1个回答
1
投票

您用名称listing定义了一个函数,因为该函数是在导入之后定义的,因此它将采用该函数。实际上,我们看到:

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