1

I am new to Django. I just want to build an online resume based on the details given by the user. I have separate HTML files for taking the user input and for displaying the resume. I took the user input for the certification field like this:

<div class="box"><input type="text" name="certificate1" placeholder="Certificate-name"> <input type="text" name="institute1" placeholder="Institute-name"></div><br>
    <div class="box"><input type="text" name="certificate2" placeholder="Certicate-name">  <input type="text" name="institute2" placeholder="Institute-name"></div><br>
    <div class="box"><input type="text" name="certificate3" placeholder="Certicate-name">  <input type="text" name="institute3" placeholder="Institute-name"></div><br>
    <div class="box"><input type="text" name="certificate4" placeholder="Certicate-name">  <input type="text" name="institute4" placeholder="Institute-name"></div><br>
    <div class="box"><input type="text" name="certificate5" placeholder="Certificate-name">  <input type="text" name="institute5" placeholder="Institute-name"></div><br>
    <div class="box"><input type="text" name="certificate6" placeholder="Certicate-name">  <input type="text" name="institute6" placeholder="Institute-name"></div><br>

And the code which I have written in views.py file is this:

if(request.method=="POST"):
    dictionary = {str(i):request.POST[i].capitalize() for i in request.POST}
    return render(request,"form/resume.html",dictionary)

And the code which I have written for displaying certifications in the resume is this:

{% if certificate1 %}
    <li>{{certificate1}}, {{institute1}}</li>
    {% endif %}
    {% if certificate2 %}
    <li>{{certificate2}}, {{institute2}}</li>
    {% endif %}
    {% if certificate3 %}
    <li>{{certificate3}}, {{institute3}}</li>
    {% endif %}
    {% if certificate4 %}
    <li>{{certificate4}}, {{institute4}}</li>
    {% endif %}
    {% if certificate5 %}
    <li>{{certificate5}}, {{institute5}}</li>
    {% endif %}
    {% if certificate6 %}
    <li>{{certificate6}}, {{institute6}}</li>
    {% endif %}

But I feel the code that I have written in displaying certifications in resume [2nd code] is not efficient. Is there any other way of writing the 2nd code? I want to know, how can we use for loop if possible. Or is there any other way? Thanks in advance for your valuable answers.

3 Answers3

0

You can group all your data in one array and pass them in your render function so you could iterate it in your Jinja template

Jude Maranga
  • 865
  • 2
  • 9
  • 27
  • I used many fields like skills, education, achievements, project details, and so on. And for each field, I have a maximum of 5 answers. So, I simply stored them in a dictionary by using a for loop in views.py file. Please check my edited question once. – Vallamkonda Neelima Aug 06 '20 at 16:05
0

Send certificates as a list to template like this:

certificates = [
    { "certificate": certificate1, "institute": institute1},
    { "certificate": certificate2, "institute": institute2}
]

Then in template, display it like this:

{% for c in certificates %}
    <li>{{c.certificate}}, {{c.institute}}</li>
{% endfor %}
Safiul Kabir
  • 153
  • 1
  • 7
  • I used many fields like skills, education, achievements, project details, and so on. And for each field, I have a maximum of 5 answers. So, I simply stored them in a dictionary by using a for loop in views.py file. Please check my edited question once. – Vallamkonda Neelima Aug 06 '20 at 16:05
  • @VallamkondaNeelima even then you can just use a list for iteration and add the dictionary inside it as a list element like this: `dictionary = [{str(i):request.POST[i].capitalize()} for i in request.POST]`. This will allow using loop in template as well. – Safiul Kabir Aug 06 '20 at 16:16
0

Try to use arrays so you can easily iterate through it. or if you just want to write in that way use string concatenation for the variable name like:

{% for x in '123456' %}
    {% with y=forloop.counter|stringformat:"s" %}
    {% with names="certificate"|add:y %}
        {% if names %}
          <li> {{name}} </li>
        {% endif %}
    {% endwith %}
    {% endwith %}
{% endfor %}

See this for more info: How can I concatenate forloop.counter to a string in my django template

  • as I have seen that you have edited question... you are sending dict to template. So iterate through that using for loop.{% for key, values in certificates.items %}{{key}}{{value}}{% endfor %} – Peddi Sakila Aug 06 '20 at 16:19
  • But now the **certificate1** is not a variable, it is a string-- "certificate1" and the above code you have written displays certificate1 certificate2 certificate3 and so on but not the values of them which are provided in views.py – Vallamkonda Neelima Aug 06 '20 at 17:58