PurePhoenix
PurePhoenix

Reputation: 15

Django "Cannot resolve keyword" error. Can someone explain this?

I am just begining to learn how to use django. I am comming up with this error in my CLI

  File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py", line 1337,
  in setup_
    "Choices are: %s" % (name, ", ".join(names)))
  FieldError: Cannot resolve keyword 'likes' into field. Choices are: id, name,
  page

My problem is that I don't know what it means or how to fix it. Can someone please explain? This is the output that the debugger log gave me:

Error during template rendering

In template C:\Users\aharon\Desktop\TEMP\index.html, error at line 40
Cannot resolve keyword 'likes' into field. Choices are: id, name, page
30  </html>
31  
32  <html>
33      <head>
34          <title>Rango</title>
35      </head>
36  
37      <body>
38          <h1>Rango says...hello world!</h1>
39  
40          {% if categories %} <--this was highlighted in the debugger
41              <ul>
42                  {% for category in categories %}
43                  <li>{{ category.name }}</li>
44                  {% endfor %}
45              </ul>
46          {% else %}
47              <strong>There are no categories present.</strong>
48          {% endif %}
49  
50          <a href="/rango/about/">About</a>

View code:

from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category

def index(request):
    context = RequestContext(request)

    category_list = Category.objects.order_by('-likes')[:5]
    context_dict = {'categories': category_list}

    return render_to_response('index.html', context_dict, context)

Upvotes: 0

Views: 2270

Answers (4)

Joshua Gampala
Joshua Gampala

Reputation: 11

Add this line to django/models.py...in class Category

likes = models.IntegerField(default=0)

Then run:

$ python manage.py makemmigrations

$ python manage.py migrate..

and refresh the browser

Upvotes: 0

user3599592
user3599592

Reputation: 1

I'm not sure about there being a better way but I added likes in models.py so that it looks like this:

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    likes = models.IntegerField(default=0) # I added this line
    def __unicode__(self):
    return self.name

Next I deleted the database because migrate isn't working for some reason

then: python manage.py syncdb

This fixed the likes issue but it also killed all the information from the earlier step in the tutorial where we made the populate_rango.py file. So I then ran that again.

python populate_rango.py

then my rango page displayed properly.

Upvotes: 0

Yogesh dwivedi Geitpl
Yogesh dwivedi Geitpl

Reputation: 4462

Your problem caused in your model LIKES field

"likes" field not in your model. add "likes" field and the run migration.

Upvotes: 1

Maciej Gol
Maciej Gol

Reputation: 15864

The issue is caused by the following line:

category_list = Category.objects.order_by('-likes')[:5]

It appears that Category model has no field likes, but id, name and page.

Upvotes: 2

Related Questions