0

I have a basic webapp deployed on Heroku which has couple of information stored about students i.e. their names, IDs, and graduation date. Instead of storing a certificate for every student in the server which takes more space, I have stored a blank certificate in the server. Every time a user enters the ID, if the student with that ID exists, I want to overlay their name and graduation date on the blank certificate and make it available for download.

I have a function that does the overlaying but the file download is not working! When I click the download button in the web-app, it attempts to download a file called "download.html" but fails.

Can anyone guide?

Views.py

## shows a form where user enters an ID (CNIC) number. 
def index (request): 
    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():
            cnic = form.cleaned_data["cnic"]
            if Fellow.objects.filter(CNIC=cnic):
                fellow = Fellow.objects.get(CNIC=cnic)
                return success(request, fellow)
            else:
                return fail(request, cnic) 

        else: 
            return render(request, "certificates/index.html", {
                "form": form
            })
    else:
        search = SearchForm()
        return render (request, "certificates/index.html", {
            "form": search
        })
    
## if student with given CNIC exists, success function is called
def success (request, fellow): 
    name = fellow.name.title()
    program = fellow.program
    cnic = fellow.CNIC 
    cnic = fellow.CNIC[:5]+ "-" + fellow.CNIC[5:12] + "-" + fellow.CNIC [-1]
    if "Dr." not in name: name = "Dr. " + name
    return render(request, "certificates/verified.html", {
        "name": name, 
        "program": program,
        "cnic": cnic,
        "message": f"It is verified that {name} (CNIC # {cnic}) has successfully completed NFDP training held during {program}.",
        "email": "Please e-mail <nahe.support@hec.gov.pk> to request the transcript."
    })

#triggers when download button is pressed 
def download (request):
    image = Image.open("certificates\static\certificates\Certificate_0001.jpg")
    font_type = ImageFont.truetype('arial.ttf', 70)
    font_type_2 = ImageFont.truetype('arial.ttf', 35)
    draw = ImageDraw.Draw(image)
    draw.text(xy=(810, 740), text="Name of Student", fill=(0,102,0), font=font_type)
    draw.text (xy=(330, 1230), text="Graduation Date", fill=(0,102,0), font=font_type_2)
    image.save("test.pdf", "PDF", resolution=100.0)


Verified.html

{% extends "certificates/layout.html" %}

{% block title %}
    Successful Verification
{% endblock %}

{% block body %}
    <div class="alert alert-success" role="alert">
        <h4 class="alert-heading">Graduated!</h4>
        <p>{{ message }}</p>
        <hr>
        <p class="mb-0">{{ email }}</p>
    </div>
    <div style= "background-color: beige;" class="row">
        <div class="col-2">
          <div class="list-group" id="list-tab" role="tablist">
            <a class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="list" href="#list-home" role="tab" aria-controls="home">Name</a>
            <a class="list-group-item list-group-item-action" id="list-profile-list" data-toggle="list" href="#list-profile" role="tab" aria-controls="profile">CNIC</a>
            <a class="list-group-item list-group-item-action" id="list-messages-list" data-toggle="list" href="#list-messages" role="tab" aria-controls="messages">NFDP Program</a>
          </div>
        </div>
        <div class="col-5">
          <div class="tab-content" id="nav-tabContent">
            <div style= "font-size: 25px" class="tab-pane fade show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">{{ name }}</div>
            <div style= "font-size: 25px" class="tab-pane fade" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">{{ cnic }}</div>
            <div style= "font-size: 25px" class="tab-pane fade" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">{{ program }}</div>
          </div>
        </div>
      </div>
    <br><br>
    <div style= "text-align: center;" class="button">
        <ul class="nav nav-pills">
            <li class="nav-item">
                <a class="nav-link active" href="{% url 'index' %}">Seach Again</a>
                <a class="nav-link active" href="{% url 'download' %}" download>Download Certificate</a>
            </li>
        </ul>
    </div>
{% endblock %}

urls.py

urlpatterns = [
    path("", views.index, name="index"), 
    path("download", views.download, name="download")

]
Osaama Shehzad
  • 147
  • 1
  • 2
  • 12
  • Does this answer your question? [Django download a file](https://stackoverflow.com/questions/36392510/django-download-a-file) – JPG Sep 15 '20 at 13:48
  • @ArakkalAbu thanks, but it assumes that there is an uploaded file which needs to be downloaded, which is not the case here – Osaama Shehzad Sep 15 '20 at 13:50
  • create a file (image|pdf|whatever you want) in the *temporary location* and return as HTTP stream response. – JPG Sep 15 '20 at 13:52

0 Answers0