Моя цель:
Если у пользователя оценка> 1, отображаются только задания, имеющие соответствующую категорию. Итак, если категория 1 (название теста) - DataScience, отображаются только задания, относящиеся к науке о данных.
Каков мой результат:
Если оценка> 1, отображаются все предложения о работе, представленные в базе данных. (как на картинке)
Что я делаю не так? Есть предложения?
quiz / models.py
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Questions(models.Model):
CAT_CHOICES = (
('datascience', 'DataScience'),
('productowner', 'ProductOwner'),
('businessanalyst', 'BusinessAnalyst'),
#('sports','Sports'),
#('movies','Movies'),
#('maths','Maths'),
#('generalknowledge','GeneralKnowledge'),
)
question = models.CharField(max_length = 250)
optiona = models.CharField(max_length = 100)
optionb = models.CharField(max_length = 100)
optionc = models.CharField(max_length = 100)
optiond = models.CharField(max_length = 100)
answer = models.CharField(max_length = 100)
catagory = models.CharField(max_length=20, choices = CAT_CHOICES)
student = models.ManyToManyField(User)
class Meta:
ordering = ('-catagory',)
def __str__(self):
return self.question
jobs / models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class post_job(models.Model):
posizione= models.CharField(max_length=20)
descrizione= models.TextField(max_length=60)
requisiti= models.TextField(max_length=60)
nome_azienda= models.CharField(max_length=20, default=' inserisci nome')
email_referente= models.CharField(max_length=20, default='inserisci email')
core / views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView
from .decorators import allowed_user
# Create your views here.
from quiz.models import Questions
from jobs.models import post_job
from functools import wraps
@allowed_user(allowed_roles=['Admin','Students'])
def userProfileView(request, username):
user= get_object_or_404(User, username=username)
jobs = post_job.objects.all()
categories = Questions.CAT_CHOICES
scores = []
for category in categories:
score = Questions.objects.filter(catagory=category[0], student= user).count()
scores.append(score)
context = {
'user' : user, 'categories_scores' : zip( categories,scores),
'jobs': jobs
}
return render(request, 'core/user_profile.html' , context)
core / user_profile. html
{% extends 'base.html'%}
{% block content %}
<br>
<div class="card-header">
<h3> {% if request.user == user %} Il tuo {% endif %} Profilo Utente </h3>
<br>
<h1> Ciao Studente: @{{ user }}</h1>
</div>
<br>
<br>
{% for category,score in categories_scores %}
<div class="card">
<div class="card-header">
{{ category.1 }} Score
</div>
<div class="card-body">
<h5 class="card-title">CONGRATS!</h5>
<p class="card-text"> Here your SCORE: </p>
<a href="#" class="btn btn-primary">{{ score }}</a>
{% if score > 1 %}
{% if request.user == user %}
<br>
<br>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 99%" aria-valuenow="99" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<p class="card-text"> Congratulations! Now you can apply to:</p>
<br>
{% for job in jobs %}
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
{{job.posizione}} <strong> {{job.nome_azienda}} </strong>
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<ul>
<li> {{job.descrizione}} </li>
<li> {{job.email_referente}} </li>
</ul>
</div>
</div>
</div>
{% endfor %}
{% endif %}
{% else %}
<br>
<br>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<p class="card-text"> Still not meet the requirements to apply, keep going! </p>
{% endif %}
</div>
</div>
<br>
{% endfor %}
{% endblock content %}