如何在django中呈现字典中的每一项



我在一个django项目中工作,用户上传简历,并在结果保存在用户配置文件之前对其进行解析。我已经完成了解析部分并保存了数据,但问题是数据的呈现。一个例子是在技能字段中,其中数据存储为字典,因此我不能一次显示一个。这是我的models.py:

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(default='default.jpg', upload_to='profile_images')
bio = models.TextField()
resume        = models.FileField('Upload Resumes', upload_to='resumes/', null=True, blank=True,default='resume.docx')
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)
total_experience  = models.CharField('Total Experience (in Years)', max_length=1000, null=True, blank=True)
whatsapp       = models.URLField(null=True, blank=True)
facebook       = models.URLField(null=True, blank=True)
twitter       = models.URLField(null=True, blank=True)
linkedin       = models.URLField(null=True, blank=True)
languages    = models.CharField(max_length=1000, null=True, blank=True)
profession = models.CharField(max_length=100, null=True, blank=True)
nationality = models.CharField(max_length=100, null=True, blank=True)
def __str__(self):
return self.user.username
# resizing images
def save(self, *args, **kwargs):
super().save()
img = Image.open(self.avatar.path)
if img.height > 100 or img.width > 100:
new_img = (100, 100)
img.thumbnail(new_img)
img.save(self.avatar.path)

这里是我的views.py:

@login_required
def profile(request):
if request.method == 'POST':
profile_form = UpdateProfileForm(request.POST, request.FILES, instance=request.user.profile)
files = request.FILES.getlist('resume')
resumes_data = []
if profile_form.is_valid():
for file in files:
try:
# saving the file
resume = profile_form.cleaned_data['resume']
parser = ResumeParser(file.temporary_file_path())
data = parser.get_extracted_data()
resumes_data.append(data)
profile_form.instance.name = data.get('name')
profile_form.instance.email              = data.get('email')
profile_form.instance.mobile_number      = data.get('mobile_number')
if data.get('degree') is not None:
profile_form.instance.education      = ', '.join(data.get('degree'))
else:
profile_form.instance.education      = None
profile_form.instance.company_names      = data.get('company_names')
profile_form.instance.college_name       = data.get('college_name')
profile_form.instance.designation        = data.get('designation')
profile_form.instance.total_experience   = data.get('total_experience')
if data.get('skills') is not None:
profile_form.instance.skills         = data.get('skills')
else:
profile_form.instance.skills         = None
if data.get('experience') is not None:
profile_form.instance.experience     = ','.join(data.get('experience'))
else:
profile_form.instance.experience     = None
profile_form.save()
return redirect('users-profile')
except IntegrityError:
messages.warning(request, 'Duplicate resume found')
return redirect('users-profile')

profile_form.save()
messages.success(request, 'Your profile is updated successfully')
return redirect('userprofile')
else:
profile_form = UpdateProfileForm(instance=request.user.profile)
return render(request, 'user/resumeprofile.html', {'profile_form': profile_form})
@login_required
def myprofile(request, user_id):
profile = Profile.objects.get(id=user_id)
context = {'profile':profile}
return render(request, 'user/profile.html', context)

这是我的配置文件模板:

<div class="mt-4">
<h5 class="fs-18 fw-bold">Skills</h5>

<span class="badge fs-13 bg-soft-blue "><p>{{user.profile.skills}}</p></span>
</div>

在当前设置中,我得到的结果如下图所示:渲染结果的截图

正如我从你的图像中看到的,你将所有的skills保存在一个列表中。所以你可以试试:

{% for skill in user.profile.skills %}
{{skill}}
{% endfor %}

如果你想把它打印成一个列表,你可以这样尝试:

<ul>
{% for skill in user.profile.skills %}
<li>{{skill}}</li>
{% endfor %}
</ul>

相关内容

  • 没有找到相关文章

最新更新