In this article, we will guide you through creating a Job Board using Django in Python.
Job Board Using DjangoBelow, is the step-by-step Implementation of a language learning app using Django in Python:
Starting the Project FolderTo start the project use this command
django-admin startproject Job_Board cd Job_Board To start the app use this command
python manage.py startapp home Now add this app to the ‘settings.py’
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'home', ] File Structure.jpeg) 3
Setting Necessary Filesmodel.py:–The Job model represents the core data structure for our job board application. Each Job instance corresponds to a job listing with various attributes such as the job title, description, company, location, and creation date. Here’s the model again for reference:
Python
from django.db import models
class Job(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
company = models.CharField(max_length=100)
location = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
home/views.py : Views in Django are responsible for handling requests and returning responses. They act as the middle layer between the models (database) and templates (HTML). In our job board application, we have defined two views: job_list and job_detail.
Python
from django.shortcuts import render, get_object_or_404
from .models import Job
def job_list(request):
jobs = Job.objects.all()
return render(request, 'job_list.html', {'jobs': jobs})
def job_detail(request, job_id):
job = get_object_or_404(Job, pk=job_id)
return render(request, 'job_detail.html', {'job': job})
Creating GUI:templates/create_community_post : This HTML template renders a form to create a new community post in the Job Board. It includes fields for the post title and content, with validation to ensure they are required.
HTML
<!-- templates/job_details.html -->
<!DOCTYPE html>
<html>
<head>
<title>{{ job.title }}</title>
</head>
<body>
<h1>{{ job.title }}</h1>
<p>{{ job.description }}</p>
<p>Company: {{ job.company }}</p>
<p>Location: {{ job.location }}</p>
<p>Posted on: {{ job.created_at }}</p>
<a href="{% url 'job_list' %}">Back to Job List</a>
</body>
</html>
HTML
<!--This template is responsible for displaying a list of job postings.
It's a simple HTML file with embedded Django template language (DTL) tags to dynamically render
content from the context provided by the job_list view.-->
<!-- job_list.html-->
<!DOCTYPE html>
<html>
<head>
<title>Job List</title>
</head>
<body>
<h1>Job List</h1>
<ul>
{% for job in jobs %}
<li>
<a href="{% url 'job_detail' job.id %}">{{ job.title }}</a> - {{ job.company }} - {{ job.location }}
</li>
{% endfor %}
</ul>
</body>
</html>
urls.py :-This file is usually located in the root of the project directory (where settings.py is located). It includes the URL configurations for the entire project.
Python
//urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("home.urls")),
]
home/urls.py:
Python
from django.urls import path
from . import views
urlpatterns = [
path('', views.job_list, name='job_list'),
path('<int:job_id>/', views.job_detail, name='job_detail'),
]
The admin.py file is where you register your models with the Django admin site, enabling you to manage them through the web-based admin interface.
Python
from django.contrib import admin
from .models import Job
admin.site.register(Job)
Deployment of the ProjectRun these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate Create the superuser using the below command :python3 manage.py createsuperuser Run the server with the help of following command:python3 manage.py runserver
Output:
|