Krychaz
Krychaz

Reputation: 45

function' object has no attribute 'objects

I'm trying to loop through my database based on a tutorial I'm fallowing, however when I enter my 'listings' app page I get Error: AttributeError at /listings/ - 'function' object has no attribute 'objects'

I already tried naming the variable something else so it doesn't share the name with the model, but regardless of what I do. I'm still getting the errors

So that's my views.py from listings app

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')

That's my urls.py

from django.urls import path 

from .import views

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

Here I'm looping through and imputing the data into the given format

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

I expected to see the one entry I have in my database but instead I get the Error: AttributeError at /listings/ - 'function' object has no attribute 'objects' and request <WSGIRequest: GET '/listings/'>

Upvotes: 1

Views: 867

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

You defined a function with the name listing, since that is defined after the import, it will thus take that one. Indeed, we see:

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')

You can solve the problem by importing it locally:

# Create your views here.


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

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

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

But it is strongly advisable to use Perl Case for Django models (and classes in general). So you probably should rename your listing model to Listing.

Upvotes: 1

Related Questions