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