AttributeError: 'List' object has no attribute 'is_bound'

I am using crispy forms + django_tables2.

There is a form for ‘Create’ view. I would like to use the same form to display the individual record too. I’ve the following views. The form while creating a record works fine, but when trying to view a record with the same form, error as above is thrown. Request your help. - Thank you!

views.py (Create is working, while CBV ‘DView’ is throwing error.)

class DView(View): 
    def get(self, request, pk): 
        context_data = {"form":List.objects.get(id=pk)}
        return render(request,'CRUD/view.html', context_data)

def Create(request):
    form = DForm()

    if request.method == 'POST':
        form = DForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list_D')

    context={
        'form': form,
        }
    return render(request,'CRUD/add.html', context)

form to use both for creating & viewing details

I need this empty form to be filled with data when the pk value is passed.

I tried the below formview which works without error, but doesn’t populate data of the record selected, as I don’t know how to pass pk to it.

class DView1(FormView):
    form_class = DForm
    template_name = 'CRUD/view.html'
    success_url = 'list_D'
    
    def form_valid(self, form):        
        print(form.cleaned_data)
        return super().form_valid(form)

Error is thrown at line 6 in the view.html file

File "F:\Program Files\Python310\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py", line 113, in get_render
    node_context.update({"is_bound": actual_form.is_bound})
AttributeError: 'List' object has no attribute 'is_bound'
{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}

{% crispy form %}

{% endblock %}

I think the problem is you’re passing a model instance into your template, but passing it to Crispy Forms tags as though it’s a Form.

Specifically with this line in DView:

        context_data = {"form":List.objects.get(id=pk)}

If I modify that line as below taking in the model’s name,

context_data = {"List":List.objects.get(id=pk)}

then I am getting error as below,

Failed lookup for key [form] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'List': <List: 8008>}]

Could you please let me know what is the exact way to display the selected record’s detail in the same form as that of one used with Create? - Thank you!

You missed the point of the previous response.

Your template is expecting to find a key in the context named “form”.

In DView, you’re not setting the context key “form” to an instance of a form. You’re setting it to an instance of List.

Changing the key name in your context from “form” to “List” doesn’t fix that. (You’ve changed it from setting “form” to being of the wrong data type to not setting it at all.)

Sorry. I am still stuck and not able to get through this. Could you please explain how exactly to be done for the same? I am very new to application development and finding hard to find a solution or even any reference link for writing views.py for my need. - thank you!

When I reference something called the context, what am I talking about? (What is a context?)

What is a Django form?

When I talk about something being an instance of List, what does this mean?

(I need to know where to start an explanation)

:slight_smile: Possible to let me know how to arrive at the solution for my need?

use the same form ‘DForm’ for both ‘Create.html’ & ‘View.html’? primary one is to create new record whereas the later is to view the already entered record. - Thank you!

Will I do everything I can to help you learn what you need to know, to be able to move forward past this point? Absolutely.

But for me to help you move forward, I need to understand what your level of knowledge is now.