[Django] Trying to open a form on button click getting Error:Failed lookup for key [%s] in %r during template rendering

306 views
Skip to first unread message

Dilipkumar Noone

unread,
Oct 7, 2019, 11:37:38 AM10/7/19
to Django users
Dear Django User Group,

I am django beginner.

Please clarify the below doubt.

Implement Scenerio:
1.I am developing one django app which needs a form on button click on html page.
2.on successful submission should display the object created in the list on the same web page.

Below is the current implementation.

when click on "Apply GSP" button getting below error:
exc
VariableDoesNotExist('Failed lookup for key [%s] in %r', ('helper', <ApplyGSPForm bound=False, valid=Unknown, fields=(model_name;model_no;os_ver;chipset;patch_level_to_be_applied;rel_cycle;applied_gsp_count;from_sas_url;to_sas_url;def_src_manifest;from_sw_version;to_sw_version;description)>))
get_response
<bound method BaseHandler._get_response of <django.core.handlers.wsgi.WSGIHandler object at 0x03FA61F0>>
request
<WSGIRequest: GET '/gspapp/ApplyGSPList/requestForm/'>

Note: I am using bootstrap4 model using datatarget attribute to render the crispy form using django templates on button click. 

Please suggest how to resolve this issue.

models.py:
-----------
class ApplyGSP(models.Model):
"""
This class is used to Apply the GoogleSecurityPatches when the user requested to apply the GSP
"""
user = models.CharField("User", max_length=100, default="")
model_name = models.CharField(max_length=100, blank=True)
model_no = models.CharField(max_length=100, blank=True)
os_ver = models.ForeignKey(OSVersion, on_delete=models.CASCADE, verbose_name="os Version")
chipset = models.ManyToManyField(Chipset)
patch_level_to_be_applied = models.CharField(max_length=200, blank=True)
rel_cycle = models.ForeignKey(ReleaseCycle, on_delete=models.CASCADE, verbose_name="Release Cycle")
requested_time = models.DateTimeField(default=timezone.now)
applied_gsp_count = models.IntegerField(null=True)
applied_security_patch_level = models.TextField(default="")
from_sas_url = models.ForeignKey(SourceSASURL, on_delete=models.CASCADE, verbose_name="Source SAS URL")
to_sas_url = models.URLField()
def_src_manifest = models.ManyToManyField(DefaultSrcManifest)
# from_manifest = models.CharField(max_length=200)
# to_manifest = models.CharField(max_length=200)
from_sw_version = models.IntegerField(null=True)
to_sw_version = models.IntegerField(null=True)
description = models.CharField(max_length=200)

class Meta:
verbose_name_plural = "ApplyGSPPatches"

def __str__(self):
return self.user + str(self.pk)

forms.py:
----------
class ApplyGSPForm(ModelForm):
"""
This class is used to define the form input fields to apply the google security patches for each user request
from common SAS board:7620
"""

class Meta:
model = ApplyGSP

fields = ['model_name', 'model_no', 'os_ver', 'chipset', 'patch_level_to_be_applied', 'rel_cycle',
'applied_gsp_count', 'from_sas_url', 'to_sas_url', 'def_src_manifest', 'from_sw_version',
'to_sw_version', 'description']
exclude = ['user', 'requested_time', 'applied_security_patch_level']

ApplyGSP.html:
-------------------
<!DOCTYPE html>
{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block static %}
{% endblock static %}

{% block content %}
<div class="float-md-left">
<div class="page-header">
<a href="{%url 'gspapp:ApplyGSPRequestForm' %}">
<button type="button" class="btn-primary btn-sm" data-toggle="modal" data-target="#myApplyGSPModal"><span class="fas fa-plus"></span>ApplyGSP</button>
</a>
</div>
<div id="#myApplyGSPModal" class="model fade" role="dialog">
<form method="POST" action="">
<!-- The below CSRF Token makes form Secure -->
{% csrf_token %}
<div class="container">
<!-- {% crispy form form.helper 'bootstrap' %} -->
{% crispy form %}
<button type="submit" class="btn btn-primary">
{% if action == "submit" %}
<span class="fas fa-plus"></span>Submit
{% else %}
<span class="fas fa-edit"></span>Update
{% endif %}
</button>
<button type="reset" class="btn btn-primary" aria-label="Close" data-dismiss="modal">
<span aria-hidden="true">&times;</span>Cancel
</button>
</div>
</form>
</div>
</div>
{% endblock content %}

{% block scripts %}
{% endblock scripts %}

Myviews.py:
@login_required(login_url='/gspapp/')
def apply_gsp_request_form(request, id=None):
"""
:param request: Accept http request
:param id: Id of Requested User record
:return: http response
"""
print("Inside Apply Google Security Patch Form:", id)
print("request:", request.method)

if id:
action = 'edit'
model = get_object_or_404(ApplyGSP, pk=id)
else:
action = 'submit'
model = ApplyGSP()

message = ""

if request.method == 'POST':
form = ApplyGSPForm(request.POST, instance=model)

if form.is_valid():
new_apply_gsp_request = form.save(commit=False)
new_apply_gsp_request.user = request.user
new_apply_gsp_request.save()
return HttpResponseRedirect('/gspapp/ApplyGSPList')

else:
print("when the request is GET inside ApplyGSPForm")
form = ApplyGSPForm(instance=model)

context = {'form': form, 'Message': message, 'action': action}
return render(request, 'ApplyGSP.html', context)

My urls.py: 
urlpatterns = [
re_path(r'^download/(?P<id>\d+)/$', views.download_filtered_gsp_patches, name='GSPExtractFilter'),
path('ApplyGSPList/', login_required(views.ApplyGSPList.as_view(), login_url='/gspapp/login/'), name='ApplyGSPList'),
path('ApplyGSPList/requestForm/', views.apply_gsp_request_form, name='ApplyGSPRequestForm'),
re_path(r'^editApplyGSP/requestForm/(?P<id>\d+)/$', views.apply_gsp_request_form, name='editApplyGSP'),
path('GSPReview/', views.gspreview, name='GSPReview'),

Dilipkumar Noone

unread,
Oct 8, 2019, 12:48:19 PM10/8/19
to Django users
Hi,

This issue is fixed to me by instantiating the FormHelper() in forms.py.



class ApplyGSPForm(ModelForm):

helper = FormHelper()

class Meta:
model = ApplyGSP

fields = [
'model_name', 'model_no', 'os_ver', 'chipset', 'patch_level_to_be_applied', 'rel_cycle',
'applied_gsp_count', 'from_sas_url', 'to_sas_url', 'def_src_manifest', 'from_sw_version',
'to_sw_version', 'description']
exclude = [
'user', 'requested_time', 'applied_security_patch_level']

Regards,
N.Dilip Kumar.
Reply all
Reply to author
Forward
0 new messages