编写一个django的求职招聘系统代码
时间: 2023-05-20 20:05:33 浏览: 104
非常感谢您的提问,我可以为您提供一些关于编写 Django 求职招聘系统的代码建议。首先,您需要创建一个 Django 项目并安装必要的依赖项。然后,您可以创建一个名为 "jobs" 的应用程序,并在其中创建模型来表示职位和应聘者。以下是一个简单的模型示例:
```
from django.db import models
class Job(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
location = models.CharField(max_length=100)
salary = models.IntegerField()
class Applicant(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
phone = models.CharField(max_length=20)
resume = models.FileField(upload_to='resumes/')
job = models.ForeignKey(Job, on_delete=models.CASCADE)
```
接下来,您可以创建视图来处理职位和应聘者的创建、编辑和删除。以下是一个简单的视图示例:
```
from django.shortcuts import render, redirect
from .models import Job, Applicant
from .forms import JobForm, ApplicantForm
def job_list(request):
jobs = Job.objects.all()
return render(request, 'jobs/job_list.html', {'jobs': jobs})
def job_detail(request, pk):
job = Job.objects.get(pk=pk)
applicants = Applicant.objects.filter(job=job)
return render(request, 'jobs/job_detail.html', {'job': job, 'applicants': applicants})
def job_create(request):
if request.method == 'POST':
form = JobForm(request.POST)
if form.is_valid():
form.save()
return redirect('job_list')
else:
form = JobForm()
return render(request, 'jobs/job_form.html', {'form': form})
def job_edit(request, pk):
job = Job.objects.get(pk=pk)
if request.method == 'POST':
form = JobForm(request.POST, instance=job)
if form.is_valid():
form.save()
return redirect('job_list')
else:
form = JobForm(instance=job)
return render(request, 'jobs/job_form.html', {'form': form})
def job_delete(request, pk):
job = Job.objects.get(pk=pk)
job.delete()
return redirect('job_list')
def applicant_create(request, pk):
job = Job.objects.get(pk=pk)
if request.method == 'POST':
form = ApplicantForm(request.POST, request.FILES)
if form.is_valid():
applicant = form.save(commit=False)
applicant.job = job
applicant.save()
return redirect('job_detail', pk=pk)
else:
form = ApplicantForm()
return render(request, 'jobs/applicant_form.html', {'form': form, 'job': job})
```
最后,您可以创建模板来呈现职位和应聘者的列表、详细信息和表单。以下是一个简单的模板示例:
```
{% extends 'base.html' %}
{% block content %}
<h1>Jobs</h1>
<ul>
{% for job in jobs %}
<li><a href="{% url 'job_detail' pk=job.pk %}">{{ job.title }}</a></li>
{% empty %}
<li>No jobs available.</li>
{% endfor %}
</ul>
<a href="{% url 'job_create' %}">Create job</a>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<h1>{{ job.title }}</h1>
<p>{{ job.description }}</p>
<p>{{ job.location }}</p>
<p>{{ job.salary }}</p>
<h2>Applicants</h2>
<ul>
{% for applicant in applicants %}
<li>{{ applicant.name }} - <a href="{{ applicant.resume.url }}">Resume</a></li>
{% empty %}
<li>No applicants yet.</li>
{% endfor %}
</ul>
<a href="{% url 'applicant_create' pk=job.pk %}">Apply for this job</a>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<h1>Create job</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<h1>Edit job</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<h1>Apply for {{ job.title }}</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Apply</button>
</form>
{% endblock %}
```
希望这些代码建议能够帮助您开始编写 Django 求职招聘系统!
阅读全文