I have the following model:models.py
name = models.CharField(max_length=100, blank=False, null=False)
serial_number = models.CharField(max_length=100, blank=False, null=False)
make = models.CharField(max_length=100, blank=False, null=False)
model = models.CharField(max_length=100, blank=False, null=False)
color= models.CharField(max_length=100, blank=False, null=False)
date_acquired = models.DateTimeField(auto_created=True)
acquired_from = models.CharField(max_length=100, choices=ACQUIRED_FROM)
holder = models.ForeignKey(CertificateHolder, blank=False,)
dealer_license = models.ForeignKey(Dealer, blank=False)
I have created a form using ModelForm like this:
class IssueItemForm(ModelForm):
class Meta:
model = IssuedItem
fields = '__all__'
A dealer needs to check if a certificate Holder exists before issuing the item, My code for that looks like this;
views.py:
def search(request):
try:
query = request.GET.get('q')
results = 0
except ValueError:
query = None
results = None
if query:
results = CertificateHolder.objects.get(cert_no=query)
context = RequestContext(request)
return render_to_response('core/results.html', {"results": results, }, context_instance=context)
Then I have templates search.html and results.html
<form method="get" action="/search/">
<input type="text" name="q" id="id_q" value="{{ query }}"/>
<input type="submit" value="Search" />
</form>
In results.html I have
{% if results %}
<h3>The Certificate number is valid</h3>
<h3>To issues an item click the button below</h3>
<a href="{% url 'issueitem' %}">Issue A New Item</a>
{% else %}
<h3 class='error'>Please enter a valid Certificate</h3>
<form method="get" action="/search/">
<input type="text" name="q" id="id_q" value="{{ query }}"/>
<input type="submit" value="Search" />
</form>
{% endif %}
The code works if there is a certificate in the database but if there is no match it throws an error like this:
However I would like the error to be part of the code to tell the dealer that that number does not exist and that he should try another. Also when a certificate exists I would like it to be inserted into the holder field instead of fetching from the database like it is doing now. Where am i going wrong?