I've been trying to render out user specific content, but when I use the line 'Job.objects.filter(user = request.user)' I don't get anything rendered in the form. I don't think that the problem is the template because when I use the line Job.objects.all() all instances are rendered out with no problem. What I'm trying to do is render out the instances that are 'posted' by a user.
views.py
@login_required(login_url='login')
def manage_jobs(request):
if request.user.is_employee:
return redirect('home')
else:
form = JobForm(request.POST)
jobs = Job.objects.filter(user=request.user)
if form.is_valid():
form.save()
context = {"form":form, "jobs":jobs}
return render(request, 'employer/manage-jobs.html', context)
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
name = models.CharField(max_length=45, unique=False)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_employee = models.BooleanField(default=True, verbose_name='Are you using FilmLink as an employee?')
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'is_employee']
objects = MyAccountManager()
class Meta:
permissions = [
("post_jobs", "Can post jobs"),
]
def __str__(self):
return self.name
def has_perm(self, perm, obj=None):
return True
def has_perms(self, perm):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
class Job(models.Model):
company = models.CharField(max_length=40, null=True, verbose_name="Company/Employer")
description = models.TextField(null=True)
role = models.CharField(max_length=25)
area_of_filming = models.CharField(max_length=50, verbose_name="Area Of Filming", default="")
contact_email = models.EmailField(verbose_name='Contact Email', max_length=60, default='')
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(Account, default='', null=True, on_delete=models.CASCADE)
def __str__(self):
return self.company
manage-jobs.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>FilmLink</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="{% static 'lng/css/manage-jobs.css' %}" />
<link rel="stylesheet" href="{% static 'lng/css/manage-jobs-nav.css' %}" />
</head>
<body>
<button id="myBtn">Post A Job</button>
<div id="main"></div>
<div id="myModal" >
<div >
<span >×</span>
<form action="{% url 'manage-jobs' %}" method="POST">
{% csrf_token %}
<div id="company-container">
<p>Employer Name</p>
<p id="employer">{{form.company}}</p>
</div>
<div id="role-container">
<p>Role (e.g. Actor, Director)</p>
<p id="role">{{form.role}}</p>
</div>
<div >
<p>Area Of Production/Filming</p>
<p id="area-of-filming">{{form.area_of_filming}}</p>
</div>
<div id="contact-email-container">
<p>Contact Email</p>
<p id="contact-email">{{form.contact_email}}</p>
</div>
<div id="description-container">
<p>Description Of Job</p>
<p id="description">{{form.description}}</p>
</div>
<button type="submit" id="post-job">Publish Job</button>
</form>
</div>
</div>
<h1>
{% for job in jobs %} {{job.company}} {{job.description}} {% endfor %}
</h1>
</body>
</html>
urls.py
urlpatterns = [
path('', views.manage_jobs, name='manage-jobs'),
]
CodePudding user response:
When you use .filter() it returns a list, even if there is only 1 item in the list, so your options are to:
jobs = Job.objects.filter(user=request.user)[0]
or even better, use get()
jobs = Job.objects.get(user=request.user)
CodePudding user response:
put this view and try:
@login_required(login_url='login')
def manage_jobs(request):
if request.user.is_employee:
return redirect('home')
else:
form = JobForm(request.POST)
jobs = Job.objects.filter(user_id=request.user.id)
if form.is_valid():
form.save()
context = {"form":form, "jobs":jobs}
return render(request, 'employer/manage-jobs.html', context)