Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to Django, I was following a tutorial.
I am trying to display my jobs from model.py to HTML


Models.py:

class Jobs(models.Model):
    FULL_TIME = 'FT' 
    PART_TIME = 'PT'
    REMOTE = 'RT'
    TIER1 = 't1'
    TIER2 = 't2'
    TIER3 = 't3'
    TIER4 = 't4'
    TIER5 = 't5'
    
    TYPE_CHOICES = [
        (FULL_TIME, 'Full Time'),
        (PART_TIME, 'Part Time'),
        (REMOTE, 'Remote'),
        
    ]
    
    EXP_CHOICES = [
        (TIER1, 'Less than 2yrs'),
        (TIER2, '2yrs - 5yrs'),
        (TIER3, '5yrs - 10yrs'),
        (TIER4, '10yrs - 15yrs'),
        (TIER5, 'More than 15yrs')
    ]
    
    
    title = models.CharField(max_length=200)
    company = models.CharField(max_length=100)
    location = models.CharField(max_length=200)
    salary = models.CharField(max_length=100)
    type = models.CharField(max_length=10,choices =TYPE_CHOICES, default=FULL_TIME)
    experience = models.CharField(max_length=10,choices = EXP_CHOICES, default = TIER1)
    summary = models.TextField() 
    description = models.TextField() 
    requirements = models.TextField()
    logo = models.ImageField(default = 'default.png', upload_to = 'upload_imges')
    date_created = models.DateTimeField(default=timezone.now)
    owner = models.ForeignKey(User, on_delete=models.CASCADE) 
    
    def _str_(self):
        return '{} is looking for {}'.format(self.company, self.title) 

jobs=[
    {"title": "Python developer"},
    {"title": "Python developer"}
]

for job in jobs:
    print(job['title'])


Views.py:
def home(request):
    job_list = Jobs.objects.all() #import jobs from models and push it to front end
    return render(request, 'home.html', {'jobs' : job_list}) 


Home.html(pasting the relevant code as the whole is really big):
{% for job in jobss %}
    <section class="site-section">
      <div class="container">

        <div class="row mb-5 justify-content-center">
          <div class="col-md-7 text-center">
            <h2 class="section-title mb-2">43,167 Job Listed</h2>
          </div>
        </div>
        
        <ul class="job-listings mb-5">
          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_2.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>{{ job.title }}</h2>
                {{ job.company }}
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 {{ job.location }}
              </div>
              <div class="job-listing-meta">
                {{ job.type }}
              </div>
            </div>
            
          </li>
          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{{ job.logo.url }}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Digital Marketing Director</h2>
                Sprint
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 Overland Park, Kansas 
              </div>
              <div class="job-listing-meta">
                Full Time
              </div>
            </div>
          </li>

          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_3.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Back-end Engineer (Python)</h2>
                Amazon
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 Overland Park, Kansas 
              </div>
              <div class="job-listing-meta">
                Full Time
              </div>
            </div>
          </li>

          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_4.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Senior Art Director</h2>
                Microsoft
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 Anywhere 
              </div>
              <div class="job-listing-meta">
                Full Time
              </div>
            </div>
          </li>

          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_5.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Product Designer</h2>
                Puma
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 San Mateo, CA 
              </div>
              <div class="job-listing-meta">
                Full Time
              </div>
            </div>
          </li>
          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_1.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Product Designer</h2>
                Adidas
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 New York, New York
              </div>
              <div class="job-listing-meta">
                Part Time
              </div>
            </div>
            
          </li>
          <li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
            <a href="#"></a>
            <div class="job-listing-logo">
              <img src="{% static 'app/images/job_logo_2.jpg' %}" alt="Free Website Template by Free-Template.co" class="img-fluid">
            </div>

            <div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
              <div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
                <h2>Digital Marketing Director</h2>
                Sprint
              </div>
              <div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
                 Overland Park, Kansas 
              </div>
              <div class="job-listing-meta">
                Full Time
              </div>
            </div>
          </li>
     {% endfor %}


What I have tried:

This is what I did.
-Made a job_list in the models to iterate over jobs I have added from the admin panel,
- Register jobs model in admin.py
- Added code in views.py
- Added logic in HTML to display that

I have made three job objects in my admin panel already
Posted
Updated 2-Jul-22 18:28pm
Comments
Richard MacCutchan 2-Jul-22 11:19am    
You should direct this question to the writer of the tutorial.

1 solution

There a typo in the code

{% for job in jobss %}


Should be

{% for job in jobs %}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900