How to remove square brackets when rendering dictionary items in django
I am working on a django project whereby I am saving values to the database of a Model instance. The project is a resume parser which seems to be getting the values as a dictionary. I have succesfully saved the data but when rendering the output does not look good as it renders with the square brackets and quotes. Is there a way I can render them as a list? Here is my 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
My views.py
@login_required
def myprofile(request, user_id):
profile = Profile.objects.get(id=user_id)
context = {'profile':profile}
return render(request, 'user/profile.html', context)
The template;
{% for skill in user.profile.skills %}
{{skill}}
{% endfor %}
And here is how they are rendered:
[ ' S y s t e m ' , ' S p a n i s h ' , ' P u b l i c r e l a t i o n s ' , ' E n g l i s h ' , ' A d v e r t i s i n g ' , ' A d m i n i s t r a t i o n ' , ' D e p o s i t s ' , ' R e s e a r c h ' , ' P h o t o s h o p ' , ' C o m m u n i c a t i o n ' , ' E x c e l ' , ' W o r d ' , ' P o w e r p o i n t ' , ' N e w s p a p e r ' , ' M a r k e t i n g ' ]
I have tried using ', '.join()
but still it does not render them as a list
The 'skills' field in your 'Profile' model is a character field. Hence, meaning that its use is to store string values. To be able to store multiple skills associated with a profile, using a separate model class for skills is what you should try implementing.
In your models.py file, define the new model (make sure to remove the skills field from the Profile model) :
class Skills(models.Model):
skill= models.CharField(max_length=1000, null=True, blank=True)
profile_id = models.ForeignKey(Profile, null=True, on_delete=models.SET_NULL)
Every time you create a new profile object, update the skills associated with that profile before saving them. Now to get your skills to output properly in your template, your views.py could look like this:
@login_required
def myprofile(request, user_id):
profile = Profile.objects.get(id=user_id)
profile_skills = Skills.objects.get(profile_id=profile.id)
context = {'profile':profile, 'skills': profile_skills}
return render(request, 'user/profile.html', context)
Now your template will obviously look like this:
{% for skill in skills %}
{{skill}}
{% endfor %}