0
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/utils/deprecation.py", line 138, in __call__
    response = self.process_response(request, response)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/middleware/clickjacking.py", line 27, in process_response
    if response.get("X-Frame-Options") is not None:
AttributeError: 'int' object has no attribute 'get'

Here is my code,

#views.py

class BookList(generic.View):
  model = Book
  def get(self, request, *args, **kwargs):
    books = Book.objects.all()

    if request.GET.get('Add') == 'Add':
      query = request.GET.get('hidden-book')
      book = Book.objects.filter(pk=query).update(moved=True)
      return book

    context = {'books': books}
    return render(request, 'main.html', context)

#main.html

<form method="GET" action="">
  <input type="hidden" name="hidden-book" value="{{ book.id }}/>
  <input type="submit" name="Add" value="Add"/>
</form>

#models.py

class Book(models.Model):
  moved = models.BooleanField(default=False)

When the user presses the 'Add' input button, it should change the moved attribute in book to True. For background, I have an application that has a list of books, each with their own add button, that upon clicking an add button, the book is added to another list of books. My code works, but when I click the add button an AttributeError at / 'int' object has no attribute 'get' is raised. When I go back to 127.0.0.1:8000/, the book has been moved from the first to second list.

Since I don't know what's causing the error, I assumed it was the query = request.GET.get('hidden-book') line because it's getting an id which is an int. I tried putting this in a try/except block, but nothing changed. I also tried to change {{ book.id }} to {{ book }} to get a string instead, but it also didn't work.

1 Answer 1

1

the root cause is

return book

get() needs to return a http response like

return render(...)

some lines below, not a query object

this should do it:

class BookList(generic.View):
  model = Book
  def get(self, request, *args, **kwargs):
    

    if request.GET.get('Add') == 'Add':
      query = request.GET.get('hidden-book')
      book = Book.objects.filter(pk=query).update(moved=True)
      
    books = Book.objects.all()

    context = {'books': books}
    return render(request, 'main.html', context)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.