如何将 django 网站的数据永久存储到 sqlite3 数据库中?
我正在尝试使用 Django 构建一个与 SQLite3 数据库连接的简历解析器网站。我上传的简历中解析的数据存储在数据库中,但每当我刷新网站时,它就会消失。我想要的是将解析后的数据永久保存到我的数据库中,每当添加新数据时,它应该具有以前的数据,并且应将新数据添加到其中。我是 Django 新手,所以这是我不知道的事情。任何帮助将不胜感激。我在下面包含了我的 models.py 文件。
from django.db import models
from django import forms
from django.forms import ClearableFileInput
# for deleting media files after record is deleted
from django.db.models.signals import post_delete
from django.dispatch import receiver
class Resume(models.Model):
resume = models.FileField('Upload Resumes', upload_to='resumes/')
name = models.CharField('Name', max_length=255, null=True, blank=True)
email = models.CharField('Email', max_length=255, null=True, blank=True)
mobile_number = models.CharField('Mobile Number', max_length=255, null=True, blank=True)
education = models.CharField('Education', max_length=255, null=True, blank=True)
skills = models.CharField('Skills', max_length=1000, null=True, blank=True)
company_name = models.CharField('Company Name', max_length=1000, null=True, blank=True)
college_name = models.CharField('College Name', max_length=1000, null=True, blank=True)
designation = models.CharField('Designation', max_length=1000, null=True, blank=True)
experience = models.CharField('Experience', max_length=1000, null=True, blank=True)
uploaded_on = models.DateTimeField('Uploaded On', auto_now_add=True)
total_experience = models.CharField('Total Experience (in Years)', max_length=1000, null=True, blank=True)
class UploadResumeModelForm(forms.ModelForm):
class Meta:
model = Resume
fields = ['resume']
widgets = {
'resume': ClearableFileInput(attrs={'multiple': True}),
}
#delete the resume files associated with each object or record
@receiver(post_delete, sender=Resume)
def submission_delete(sender, instance, **kwargs):
instance.resume.delete(False)
views.py 文件如下
from django.shortcuts import render, redirect
from pyresparser import ResumeParser
from .models import Resume, UploadResumeModelForm
from django.contrib import messages
from django.conf import settings
from django.db import IntegrityError
from django.http import HttpResponse, FileResponse, Http404
import os
def homepage(request):
if request.method == 'POST':
Resume.objects.all().delete()
file_form = UploadResumeModelForm(request.POST, request.FILES)
files = request.FILES.getlist('resume')
resumes_data = []
if file_form.is_valid():
for file in files:
try:
# saving the file
resume = Resume(resume=file)
resume.save()
# extracting resume entities
parser = ResumeParser(os.path.join(settings.MEDIA_ROOT, resume.resume.name))
data = parser.get_extracted_data()
resumes_data.append(data)
resume.name = data.get('name')
resume.email = data.get('email')
resume.mobile_number = data.get('mobile_number')
if data.get('degree') is not None:
resume.education = ', '.join(data.get('degree'))
else:
resume.education = None
resume.company_names = data.get('company_names')
resume.college_name = data.get('college_name')
resume.designation = data.get('designation')
resume.total_experience = data.get('total_experience')
if data.get('skills') is not None:
resume.skills = ', '.join(data.get('skills'))
else:
resume.skills = None
if data.get('experience') is not None:
resume.experience = ', '.join(data.get('experience'))
else:
resume.experience = None
resume.save()
except IntegrityError:
messages.warning(request, 'Duplicate resume found:', file.name)
return redirect('homepage')
resumes = Resume.objects.all()
messages.success(request, 'Resumes uploaded!')
context = {
'resumes': resumes,
}
return render(request, 'base.html', context)
else:
form = UploadResumeModelForm()
return render(request, 'base.html', {'form': form})
I am trying to build a Resume Parser website using Django which is connected with a SQLite3 database. The parsed data from the resumes I upload gets stored in the database but whenever I refresh my website it disappears. What I want is to permanently save that parsed data into my database and whenever new data is being added, it should have the previous data and the new data should be added to it. I am new to Django, so this is something I don't know. Any help would be appreciated. I am including my models.py file below.
from django.db import models
from django import forms
from django.forms import ClearableFileInput
# for deleting media files after record is deleted
from django.db.models.signals import post_delete
from django.dispatch import receiver
class Resume(models.Model):
resume = models.FileField('Upload Resumes', upload_to='resumes/')
name = models.CharField('Name', max_length=255, null=True, blank=True)
email = models.CharField('Email', max_length=255, null=True, blank=True)
mobile_number = models.CharField('Mobile Number', max_length=255, null=True, blank=True)
education = models.CharField('Education', max_length=255, null=True, blank=True)
skills = models.CharField('Skills', max_length=1000, null=True, blank=True)
company_name = models.CharField('Company Name', max_length=1000, null=True, blank=True)
college_name = models.CharField('College Name', max_length=1000, null=True, blank=True)
designation = models.CharField('Designation', max_length=1000, null=True, blank=True)
experience = models.CharField('Experience', max_length=1000, null=True, blank=True)
uploaded_on = models.DateTimeField('Uploaded On', auto_now_add=True)
total_experience = models.CharField('Total Experience (in Years)', max_length=1000, null=True, blank=True)
class UploadResumeModelForm(forms.ModelForm):
class Meta:
model = Resume
fields = ['resume']
widgets = {
'resume': ClearableFileInput(attrs={'multiple': True}),
}
#delete the resume files associated with each object or record
@receiver(post_delete, sender=Resume)
def submission_delete(sender, instance, **kwargs):
instance.resume.delete(False)
The views.py file is below
from django.shortcuts import render, redirect
from pyresparser import ResumeParser
from .models import Resume, UploadResumeModelForm
from django.contrib import messages
from django.conf import settings
from django.db import IntegrityError
from django.http import HttpResponse, FileResponse, Http404
import os
def homepage(request):
if request.method == 'POST':
Resume.objects.all().delete()
file_form = UploadResumeModelForm(request.POST, request.FILES)
files = request.FILES.getlist('resume')
resumes_data = []
if file_form.is_valid():
for file in files:
try:
# saving the file
resume = Resume(resume=file)
resume.save()
# extracting resume entities
parser = ResumeParser(os.path.join(settings.MEDIA_ROOT, resume.resume.name))
data = parser.get_extracted_data()
resumes_data.append(data)
resume.name = data.get('name')
resume.email = data.get('email')
resume.mobile_number = data.get('mobile_number')
if data.get('degree') is not None:
resume.education = ', '.join(data.get('degree'))
else:
resume.education = None
resume.company_names = data.get('company_names')
resume.college_name = data.get('college_name')
resume.designation = data.get('designation')
resume.total_experience = data.get('total_experience')
if data.get('skills') is not None:
resume.skills = ', '.join(data.get('skills'))
else:
resume.skills = None
if data.get('experience') is not None:
resume.experience = ', '.join(data.get('experience'))
else:
resume.experience = None
resume.save()
except IntegrityError:
messages.warning(request, 'Duplicate resume found:', file.name)
return redirect('homepage')
resumes = Resume.objects.all()
messages.success(request, 'Resumes uploaded!')
context = {
'resumes': resumes,
}
return render(request, 'base.html', context)
else:
form = UploadResumeModelForm()
return render(request, 'base.html', {'form': form})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论