Home > database >  Unable to view database object in django in html
Unable to view database object in django in html

Time:07-07

The object in the database is not visible in the html.

Model

class OutFormatTemplate(models.Model):
    title = models.CharField(max_length=80)
    template = models.FileField(upload_to='out_put_temp/')

    def __str__(self):
        return f"{self.title}"

View

def upload_form(request):
    out_form = OutFormatTemplate.objects.all()
    for i in out_form:
        print(i.id, i.title)
    form = UploadBookForm()
    context = {
        'form': form,
        'outform': out_form
    }
    return render(request, 'file_uploader.html', context)

HTML

{% if outfrom %}
    <p>Out Form Exist</p>
{% else %}
    <p>Not Exist</p>
{% endif %}
<div  id="out-template">
    <label for="template-out" >Select Output Template</label>
    <select  aria-label="Default out select example" name="out_template_id" id="template-out">
        <option value="0" selected></option>
        {% for out in outfrom %}
        <option value="{{ out.id }}">{{ out.title }}</option>
        {% endfor %}
    </select>
</div>

The database value

Table values

But in the webpage these values are not shown

Form

Iam new to django, I need help. Have i done anything wrong here.

CodePudding user response:

It is look like you have a typo, where you are naming the variable in the context (outform) but in your template you are calling (outfrom).

  • Related