Malashenkov.ea
Malashenkov.ea

Reputation: 47

how can i insert html code from database into django template?

i tried to insert it via dtl variables but there were html tags on the page

models.py

html code in desciption field

class Vacancy(models.Model):
    title = models.CharField(max_length=100)
    specialty = models.ForeignKey(Specialty, on_delete=models.CASCADE, related_name="vacancies")
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name="vacancies")
    skills = models.TextField()
    description = models.TextField()
    salary_min = models.FloatField()
    salary_max = models.FloatField()
    published_at = models.DateTimeField()

html

 <a href="#"><img src="/media/company_images/{{ vacancy.company.logo }}" width="130" height="80" alt=""></a>
          <div class="d-flex align-items-baseline align-content-baseline">
            <h1 class="h2 mt-4 font-weight-bold" >{{ vacancy.title }}</h1>
            <p class="m-0 pl-3">{{ vacancy.salary_min }}р – {{ vacancy.salary_max }}р</p>
          </div>
          <p class="mt-2">{{ vacancy.skills }}</p>
          <p class="text-muted mb-4">Primal Assault (15-30 человек), Рязань или удаленно</p>
          <div style="line-height: 1.8;">
            {{ vacancy.description }}
          </div>

Upvotes: 1

Views: 2267

Answers (1)

Hussain Pettiwala
Hussain Pettiwala

Reputation: 1674

If you want to render HTML from your database you can use the safe filter in your jinja HTML.

{{ YOUR_HTML_CONTENT_FROM_DATABASE | safe }}

You can also use the autoescape as defined here.

Upvotes: 5

Related Questions