I am working on a django project whereby users upload resumes and they are parsed before the results are save in user profile. I have achieved the parsing part and saved the data bu the problem is rendering the data. An example is in the skills field whereby the data is stored as a dictionary and therefore I cannot display them one at a time.
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
# 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)
And here is my 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)
Here is my template for the profile:
<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>
In the current setup, the results I am getting are as shown in the image:
A screenshot of the results being renedered
As I can see from your image, you are saving all your skills in a list. So you can try doing:
{% for skill in user.profile.skills %}
{{skill}}
{% endfor %}
If you want to print it as a list you can try it out this way:
<ul>
{% for skill in user.profile.skills %}
<li>{{skill}}</li>
{% endfor %}
</ul>
Related
How do add new record every time I update the screen? instead it updates the existing record.?
Here are the models.
class Dealer(models.Model):
city = models.CharField('City', max_length=30, blank=True, null=True)
contact_name = models.CharField('Contact name', max_length=250, blank=True, null=True)
dealer_id = models.CharField('dealer_id', max_length=50, primary_key=True)
dealership_name = models.CharField('Dealership name', max_length=100, blank=True, null=True)
dsr = models.CharField('DSR', max_length=50, blank=True, null=True)
email = models.EmailField('Email', blank=True, null=True)
fax = models.CharField('Fax', max_length=20, blank=True, null=True)
dealer_notes = models.TextField('notes', blank=True, null=True)
phone = models.CharField('Phone', max_length=20, blank=True, null=True)
rating = models.CharField('Rating', max_length=20, blank=True, null=True)
state = models.CharField('State', max_length=5, blank=True, null=True)
street = models.CharField('Street', max_length=60, blank=True, null=True)
zip = models.CharField('Zip', max_length=10, blank=True, null=True)
def __str__(self):
return self.dealer_id
class Activity(models.Model):
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE)
activity_number = models.PositiveIntegerField(default=1)
date_created = models.DateField('date created', blank=True, null=True)
created_by = models.CharField('created_by', max_length=50, blank=True, null=True)
type = models.CharField('state', max_length=50, blank=True, null=True)
activity_notes = models.TextField(max_length=500, blank=True, null=True)
def __str__(self):
return f"{self.dealer_id} {self.activity_number}"
When it lands in dealer notes page it will get the details from the user to enter model fields and save.For each record there can be many number of activity notes. But when I tried to add a new note it updates the existing one, i want it create a new record.
def create_notes(request, dealer_id):
item, _ = Activity.objects.get_or_create(dealer_id_id=dealer_id)
form = ActivityForm(instance=item)
if request.method == 'POST':
form = ActivityForm(request.POST, instance=item)
if form.is_valid():
try:
form.save()
except:
pass
else:
return redirect('dealernotes/' + request.post.get('dealer_id_id'))
return render(request,
'dealernotes.html',
{'form': item,},
)
Can you help to create a new note every time? by activity number i should be able to update as well.
urls:
path('dealernotes/<str:dealer_id>?/', views.update_notes, name='dealernotes')
How do I fix this issue?
Don't use get_or_create and instead just use create
item = Activity.objects.create(dealer_id_id=dealer_id)
Edit 2 (scraping Edit1)
Here, Try to restructure it so that Activity is only created when you submit the form.
Note: We're changing the template, don't forget about that
def create_notes(request, dealer_id):
if request.method == 'POST':
form = ActivityForm(request.POST)
if form.is_valid():
try:
form.save()
except:
pass
else:
# debugging error:
print(form.errors.as_data())
return redirect('dealernotes/' + request.post.get('dealer_id_id'))
dealerObj = Dealer.objects.get(pk=dealer_id)
form = ActivityForm(initial={'dealer_id' : dealerObj})
data = {
'form': form,
}
return render(request, 'dealernotes.html', data)
Template
We're going to replace the PK input with a dealer_id, that way the form is valid on the post
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
I am working on a rental website project where users can create a login and add a listing. Now I want the edit access to be given to only the owners of those listing and superuser. I am able to set the edit access either to superuser or to the user, I am trying to figure out how to do both.
models.py
class Listing(models.Model):
category = models.ForeignKey(
'Category', null=True, blank=True, on_delete=models.SET_NULL)
listing_name = models.CharField(max_length=250, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
price = models.CharField(max_length=250, null=True)
short_description = models.CharField(max_length=720)
description = models.TextField()
bedroom = models.CharField(max_length=250, null=True, blank=True,)
bathroom = models.CharField(max_length=250, null=True, blank=True,)
lease = models.CharField(max_length=250, null=True, blank=True)
contact_name = models.CharField(max_length=250, null=True)
email_address = models.CharField(max_length=250, null=True)
contact_number = models.CharField(max_length=12, null=True)
image_url = models.URLField(max_length=1024, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
image_one = models.ImageField(null=True, blank=True)
image_two = models.ImageField(null=True, blank=True)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.listing_name
views.py
#login_required
def edit_listing(request, listing_id):
listing = get_object_or_404(Listing, pk=listing_id)
if not request.user.is_superuser or request.user == listing.user:
messages.error(
request, 'Sorry, you dont have the permission to do that.')
return redirect(reverse('home'))
if request.method == 'POST':
if request.user == listing.user:
form = ListingForm(request.POST, request.FILES, instance=listing)
if form.is_valid:
form.instance.user = request.user
form.save()
messages.success(request, 'Successfully updated the listing')
return redirect(reverse('listing_detail', args=[listing.id]))
else:
messages.error(
request, 'Failed to update. Please ensure the form is valid.')
else:
form = ListingForm(instance=listing)
template = 'listings/edit_listing.html'
context = {
'listing': listing,
'form': form,
}
return render(request, template, context)
Particularly this line in the views is what I'm trying to achieve
if not request.user.is_superuser or request.user == listing.user:
messages.error(
request, 'Sorry, you dont have the permission to do that.')
return redirect(reverse('home'))
[![Nurse admin page[![][1]][1][models.py
from django.db import models
#Work Related aka Department and Work Shift
class Department(models.Model):
name = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.name
class WorkShift(models.Model):
name = models.CharField(max_length=200, null=True, blank=True)
start_datetime = models.DateTimeField(null=True, blank=True)
end_datetime = models.DateTimeField(null=True, blank=True)
def __str__(self):
return self.name
#Personel Related aka Employees and Patients
class Doctor(models.Model):
name = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
department = models.ForeignKey(Department, default="", null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Nurse(models.Model):
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
department = models.ForeignKey(Department, default="", null=True, blank=True, on_delete=models.CASCADE)
reports_to = models.OneToOneField(Doctor, default="", blank=True, null=True, on_delete=models.CASCADE)
work_shift = models.OneToOneField(WorkShift, default="", blank=True, null=True, on_delete=models.CASCADE)
def __str__(self):
return str(name)
class Patient(models.Model):
STATUS = (
('Sick', 'Sick'),
('Healing', 'Healing'),
('Cured', 'Cured'),
('Deceased', 'Deceased'),
)
name = models.CharField(max_length=200, null=True, blank=True)
description = models.TextField(blank=True, null=True)
status = models.CharField(max_length=200, null=True, blank=True, choices=STATUS)
department = models.ForeignKey(Department, default="", null=True, blank=True, on_delete=models.CASCADE)
care = models.ForeignKey(Nurse, default="", blank=True, null=True, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
def __str__(self):
return self.name
views.py
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
from .models import Doctor, Nurse, Patient
from django.http import HttpResponse
# Create your views here.
def index(request):
patient = Patient.objects.all()
nurse = Nurse.objects.all()
doctor = Doctor.objects.all()
total_patient = patient.count()
sick = patient.filter(status='Sick').count()
healing = patient.filter(status='Healing').count()
cured = patient.filter(status='Cured').count()
total_nurse = nurse.count()
# if request.method == 'POST':
# form =
context = {
'patient':patient, 'nurse':nurse,
'doctor':doctor, 'total_patient':total_patient,
'sick':sick, 'healing':healing, 'cured':cured,
'total_nurse':total_nurse
}
return render(request, 'lifesaver/index.html', context)
def patient(request):
patient = Patient.objects.all()
total_patient = patient.count()
context = {
'patient':patient,
'total_patient':total_patient
}
return render(request, 'lifesaver/patient.html', context)][1]
forms.py
from django import forms
from .models import Doctor, Nurse, Patient
from django.auth.contrib.forms import UserCreationForm
class DoctorForm(forms.ModelForm):
name = forms.CharField(widget = forms.TextInput(attrs =
{
'placeholder': 'Add a New Doctor',
'class': 'form-control'
}
))
department = forms.ModelChoiceField(queryset=department.objects.all)
class NurseForm(forms.ModelForm):
name = forms.CharField(widget = forms.TextInput(attrs =
{
'placeholder': 'Add a New Nurse',
'class': 'form-control'
}
))
class PatientForm(forms.ModelForm):
name = forms.CharField(widget = forms.TextInput(attrs =
{
'placeholder': 'Add a New Patient'
'class': 'form-control'
}))
HTML for patient
{% extends 'lifesaver/main.html' %}
{% block content %}
<h1>SUPERSTAR</h1>
{% for patient in patient %}
{{patient.name}}
{% endfor %}
{% endblock content %}
I get this error when I go to try to add another Nurse. The URL is http://127.0.0.1:8000/admin/lifesaver/nurse/add/. Everything else behaves as expected, except the adding the Nurse part.
If I try to remove the def __str___ part, the error still displays. I believe the error lies in the:
work_shift = models.OneToOneField(WorkShift, default="",
blank=True,
null=True,
on_delete=models.CASCADE)
part since when I included that code, the error spawned. Furthermore, the code is to add a work shift to certain employees and the goal is that the employees shift will display in their profile.
How do I fix this issue?
EDIT: When accessing the HTML template, the web page behaves as expected and has no issues.
In your Nurse model replace this:
def __str__(self):
return str(Nurse.name)
with this:
def __str__(self):
return self.name
My problem is have two models job and company and i want to get all jobs in this company
My urls.py:
url(r'^jobs/(?P<slug>[\w-]+)/$', views.job_at_company, name='job_at_company'),
My views.py:
def job_at_company(request, slug):
return render(request, 'jobs.html')
My models.py:
class Company(models.Model):
title = models.CharField(max_length=100, blank=False)
slug = models.SlugField(blank=True, default='')
city = models.CharField(max_length=100, blank=False)
contact_info = models.CharField(max_length=100, blank=False, default=0)
facebook = models.CharField(max_length=100, blank=True)
twitter = models.CharField(max_length=100, blank=True)
linkedin = models.CharField(max_length=100, blank=True)
logo = models.ImageField(upload_to="logo", default=0)
class Jobs(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(blank=True, default='')
company = models.ForeignKey(Company, on_delete=models.CASCADE)
price = models.IntegerField(default='')
Description = models.TextField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
job_type = models.CharField(max_length=100, choices=(('Full Time', 'Full Time'),('Part Time', 'Part Time')),default='Full Time')
in the views.py we can add this
def job_at_company(request, slug):
results = Jobs.objects.filter(company__slug=slug)
context = {'items':results}
return render(request, 'jobs.html',context)
Suppose you pass id in the url. The id is the primary key of the company. You would have to modify your url to accept id like -
url(r'^jobs/(?P<slug>[\w-]+)/(?P<pk>[\d]+)$', views.job_at_company, name='job_at_company')
And modify your views.py -
def job_at_company(request, slug, pk):
jobs_qs = Jobs.objects.filter(company__id=pk)
return render(request, 'jobs.html', {'jobs': jobs_qs})
And use it in your html like -
{% for job in jobs %}
{{job.title}}
{% endfor %}
Look at this link. Django's documentation is helpful, follow that
I would like to display only the pictures uploaded by the creator (user) on their individual profiles.
How would I alter my code to display that?
Thank you!
models.py:
class Photo(models.Model):
creator = models.ForeignKey(MyUser, null=False, blank=False)
category = models.ForeignKey("Category", default=1, null=True, blank=True)
title = models.CharField(max_length=30, null=True, blank=True)
description = models.TextField(max_length=120, null=True, blank=True)
image = models.ImageField(upload_to='user/photos/', null=True, blank=True)
slug = models.SlugField(null=False, blank=False)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False, null=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)
class Meta:
unique_together = ('slug', 'category')
ordering = ['-timestamp']
def __unicode__(self):
return "%s" %(self.creator)
def get_image_url(self):
return "%s/%s" %(settings.MEDIA_URL, self.image)
def get_absolute_url(self):
return "%s/%s" %(self.creator, self.slug)
views.py:
#login_required
def account_home(request, username):
try:
u = MyUser.objects.get(username=username)
except:
u = None
photo_set = Photo.objects.all()
context = {
"photo_set": photo_set,
"notifications": notifications,
"transactions": transactions
}
return render(request, "accounts/account_home.html", context)
.html:
{% for photo in photo_set %}
<img src="{{ photo.get_image_url }}" class='img-responsive'>
<hr/>
{% endfor %}
You have a ForeignKey to user, so you can just filter the photos by that:
photo_set = Photo.objects.filter(creator=u)
or even better use the reverse relationship:
photo_set = u.photo_set.all()
Also, never ever ever ever use a blank except statement in your code. The only exception you are expecting the get to raise is MyUser.DoesNotExist, so you should catch that only.