How to make a search engine for my comments - django

I am trying to make a search engine through the posted comments. The goal is to search the comments as easiest as possible. See below the architecture of the program:
views.py
class ComentarioListar (LoginRequiredMixin,ListView):
login_url='/'
redirect_field_name='redirigido'
model = Comentario
template_name = 'home/comentario_listar.html'
class busqueda (ListView):
model=Comentario
template_name='home/search.html'
def get_queryset(self):
qs=Comentario.objects.filter()
keywords=self.request.GET.get('q')
if keywords:
query=SearchQuery(keywords)
vector=SearchVector('titulo')
qs=qs.annotate(search=vector).filter(search=query)
qs=qs.annotate(rank=SearchRank(vector,query)).order_by('-rank')
return render(request, 'home/search.html', {'qs':qs} )
urls.py
urlpatterns = [
url(r'^listar$', ComentarioListar.as_view(), name="listar"),
url(r'^search$',busqueda, name="search"),]
models.py
class Comentario (models.Model):
titulo = models.CharField(max_length=50)
texto = models.CharField(max_length=200)
autor = models.ForeignKey (Perfil, null=True, blank=True, on_delete=models.CASCADE)
fecha_publicacion = models.DateTimeField(auto_now_add=True)
tag = models.ManyToManyField(Tags, blank=True)
def __str__(self):
return (self.titulo)
cometario_listar.html (where I have created the search button)
<form id="searchform" action="search/" method="GET" accept-charset="utf-8">
<button class="searchbutton" type="submit">
<i class="fa fa-search"></i>
</button>
<input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search" id="busqueda">
</form>
{% endblock %}
search.html
{% extends 'base/base.html' %}
{% block content %}
<table class="table table-striped">
<thead class="thead-default">
<tr>
<td>Titulo</td>
<td>Descripcion</td>
<td>Autor</td>
<td>Fecha de publicación</td>
<td>Etiqueta</td>
</thead>
<tbody>
{% if qs %}
{% for comentario in object_list %}
<tr>
<td>{{comentario.titulo}}</td>
<td>{{comentario.texto}}</td>
<td>{{comentario.autor}}</td>
<td>{{comentario.fecha_publicacion}}</td>
{% for tag in comentario.tag.all %}
<td>{{ tag.nombre }}</td>
{% endfor %}
{% endfor %}
</tr>
{% else %}
<h1>No hay registros de comentarios</h1>
{% endif %}
</tbody>
</table>
The problem is that it does not work. 'http://127.0.0.1:8000/home/search/?q=nueva' appears correctly, but it does not make any filtering. Is it possible to use the same .html file (comentario_listar.html) to make the search?
thank you in advance for your help.

Related

how can you display data from three related tables in django?

I'm new to django and I'm having trouble displaying related data from three related databases in a table. I hope you can help me.
Models.py
class recursos (models.Model):
gruporecurso= models.CharField (max_length=50, choices=TIPORECURSO, default=TIPODEFAULT)
descripcion = models.CharField(max_length=100)
estado_disponible =models.BooleanField(default= True)
inventario = models.CharField(max_length=40,blank= True, null=True)
observacion = models.TextField(blank= True, null=True)
def __str__(self):
return self.descripcion
class remito (models.Model):
fecha = models.DateField()
hora = models.TimeField()
responsable = models. ForeignKey(personas, null=True, blank= True, verbose_name='Responsable',on_delete=models.CASCADE)
area_pedido = models.CharField(max_length=20, verbose_name='Área del pedido')
operador = models.CharField(max_length=40, choices=OPERADOR,verbose_name='Entregado por')
lugardeuso = models.CharField(max_length=40,verbose_name='Lugar uso')
observacion = models.TextField(blank= True, null=True)
class detalle_remito (models.Model):
id_remito = models.ForeignKey(remito, null=True, blank=False, on_delete=models.CASCADE)
id_recurso = models.ForeignKey(recursos, null=True, blank=False, on_delete=models.CASCADE)
cantidad = models.IntegerField(default=1)
def __str__(self):
return f' {self.id_recurso.descripcion}'
Views.py
def home (request):
remitos= remito.objects.all()
recursolist= detalle_remito.objects.all()
page= request.GET.get('page',1)
try:
paginator = Paginator(remitos,5)
remitos=paginator.page(page)
except:
raise Http404
return render(request,"RemitoTicapp/home.html",{'entity': remitos ,'recursolist':recursolist,'paginator': paginator})
home.html
{% extends "RemitoTicapp/base.html" %}
{% load static %}
{% block content %}
<!-- Heading
<section class="page-section clearfix">
<div class="container">
</div>
</div>
</section>
-->
<!-- Message -->
<section class="page-section cta">
<div class="container mx-auto">
<div class="row">
<!--<div class="col-xl-9 mx-auto"> -->
<div class="cta-inner text-center rounded">
<h2 class="section-heading mb-2">
<span class="section-heading-upper">Sistema de Gestión de pedidos</span>
<span class="section-heading-lower"> Elementos cedidos</span>
</h2>
<!--<p class="mb-0"> -->
<table class="table table-striped" bg white>
<thead>
<tr>
<th scope="col">Fecha entrega</th>
<th scope="col">Responsable</th>
<th scope="col">Area Pedido</th>
<th scope="col">Lugar de uso</th>
<th scope="col">Recurso / cantidad</th>
<th scope="col">Operador</th>
<th scope="col">Observaciones</th>
</tr>
</thead>
<tbody>
{% for remito in entity %}
<tr>
<td scope="row">{{ remito.fecha }}</td>
<td>{{ remito.responsable }}</td>
<td>{{ remito.area_pedido }}</td>
<td>{{ remito.lugardeuso }}</td>
<td> {% for recurso in recursolis %}
{% if recurso.id_remito == remito.pk %}
<li>{{ recurso.id_recurso.descripcion }} {{recurso.cantidad}}</li>
{% endif %}
{% endfor %}
</td>
<td>{{ remito.operador }}</td>
<td>{{ remito.observacion }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div>
{% include 'RemitoTicapp/paginator.html' %}
</div>
<!-- </p> -->
</div>
</div>
<!-- </div> -->
</div>
</section>
{% endblock%}
We should show the data of Remito with the resources of detalle_remito with the description of each one of them from the recursos table.
From already thank you very much
Django will make the reverse foreign key relation accessible with the related model's name followed by a _set-suffix, exposing an instance of RelatedManager.
{% for detalle_remito in remito.detalle_remito_set.all %}
{{ detalle_remito.id_recurso.descripcion }}
{% endfor %}
No need to gather addional data for that in your view.
Where you have {% for recurso in recursolis %}, you could instead write {% for recurso in remito.detalle_remito_set.all %} and remove the if statement - it will loop through the detalle_remito objects that are connected to the recurso object

How can i display count of each column in django

Display
I would like to count the number of students for each subjects but currently it only displays the number of students for one subject. it only counts the number of student for one subject but i would like to count the number of students in each subject
Added the models.py. Ps still new to django
views.py
class SubjectView(TemplateView):
template_name='subjects.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
username=self.request.user.id
#filter subject taught by lecturer
classdets=ClassDetail.objects.all().filter(teacher=username).values_list('subject_id')
print(classdets)
#filters all subjects from the class details
subj=Subject.objects.filter(id__in=classdets)
print(subj)
#counts number of students
subjID=Subject.objects.values('id')
num_student=ClassDetail.objects.filter(id__in=subjID).count
print(num_student)
context['subjects'] = subj
context['no_students'] = num_student
return context
template
{% extends "base.html" %}
{% load static %}
{% block title %}Subjects{% endblock title %}
{% block sidenavbar %}
{% include 'partials/sidenavbar.html' %}
{% endblock %}
{% block navbar %}
{% include 'partials/navbar.html' %}
{% endblock %}
{% block subject %}
<div class="container-fluid">
<div class="col">
<div class="card bg-transparent">
<!-- Card header -->
<div>
<div class="card-header bg-transparent border-0">
<h3 class="mb-0" style="text-transform: uppercase">Current Subjects</h3>
{% comment %} View Students {% endcomment %}
</div>
</div>
<!-- Translucent table -->
<div class="table-responsive">
<table class="table align-items-center table-flush" id="datatable-buttons">
<thead class="thead-light">
<tr>
<th>Subject Name</th>
<th>Subject Code</th>
<th>Number of Students</th>
{% comment %} <th>Generate Attendance Code</th> {% endcomment %}
</tr>
</thead>
{% if user.is_authenticated %}
{% for subject in subjects %}
<tbody class="list" style="text-transform: capitalize">
<tr>
<th scope="row">
<div class="media align-items-center">
<a href="#" class="avatar rounded-circle mr-3">
{% if subject.thumbnail %}
<img alt="Logo" src="{{subject.thumbnail.url}}" />
{% endif %}
</a>
<div class="media-body">
<span class="name mb-0 text-sm">{{subject}}</span>
</div>
</div>
</th>
<td class="budget">{{subject.code}}</td>
<td>
<div class="d-flex align-items-center">
<span class="completion mr-2">{{no_students}}</span>
</div>
</td>
<td>
{% comment %}
<div class="d-flex align-items-center">
<span class="completion mr-2">{{attendace_code}}</span>
</div>
{% endcomment %}
</td>
</tr>
</tbody>
{% endfor %}
{% endif %}
</table>
</div>
</div>
</div>
</div>
{% endblock %}
models
def get_thumbnail(instance, filename):
path = f"static/assets/img/custom/{filename}"
return path
class Subject(models.Model):
code=models.CharField(max_length=8,unique=True,default="")
name=models.CharField(max_length=100,unique=True)
thumbnail=models.ImageField(blank=True,null=True,upload_to=get_thumbnail)
def __str__(self):
return f'{self.name}'
class Meta:
unique_together = ('name', 'code')
class ClassDetail(models.Model):
teacher=models.ForeignKey(UserProfile,on_delete=models.PROTECT)
subject=models.ForeignKey(Subject, on_delete=models.PROTECT, default="")
student=models.ManyToManyField(UserProfile,related_name="student")
def __str__(self):
return f'{self.subject}'
class Meta:
unique_together = ('teacher', 'subject')
userprofile
class UserProfile(AbstractUser):
ROLE_LECTURER = 'lecturer'
ROLE_STUDENT = 'student'
ROLE_ADMIN = 'admin'
ROLE_CHOICES = (
(ROLE_LECTURER, _('Lecturer')),
(ROLE_STUDENT, _('Student')),
(ROLE_ADMIN, _('Admin')),
)
STATUS_ACTIVE = 'active'
STATUS_INACTIVE = 'inactive'
STATUS_SUSPENDED = 'suspended'
STATUS_EXPIRED = 'expired'
STATUS_CHOICES = (
(STATUS_ACTIVE, _('Active')),
(STATUS_INACTIVE, _('Inactive')),
(STATUS_SUSPENDED, _('Suspended')),
(STATUS_EXPIRED, _("Expired")),
)
username = models.CharField(max_length=11, unique=True, verbose_name="Relevant ID")
status = models.CharField(_('status'), max_length=50, choices=STATUS_CHOICES, default=STATUS_ACTIVE)
role = models.CharField(_('role'), max_length=50, choices=ROLE_CHOICES, default="")
def __str__(self):
return f'{self.username} ({self.first_name})'
USERNAME_FIELD = 'username'
num_student =subjID.classdetail_set.all().count()
print(num_student)

Custom format an inline formset

I'm create a template in Django, adding a form and formset to the template, but I don't like how it's formatting the formset by default. I've tried .as_table and .as_ul, but it's not formatting it to my liking. I'd like to see the following from the formset:
Ingredient Percentage Delete
ingredient1 .55
ingredient2 .22
ingredient3 .33
I've tried the code in "https://stackoverflow.com/questions/17492374/how-to-render-formset-in-template-django-and-create-vertical-table" but when I implement it, I'm getting two extra column, "ID" and "Recipe Name". I don't know where those columns are coming from and I don't know how to get rid of them.
Example:
models.py
class Recipe(models.Model):
name = models.CharField(max_length=200, null=True)
description = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.name
class Recipe_Ingredient(models.Model):
recipe_name = models.ForeignKey(Recipe, null=True, on_delete = models.SET_NULL)
ingredient = models.ForeignKey(Product, null=True, on_delete= models.SET_NULL)
recipe_percent = models.DecimalField(max_digits=8, decimal_places=5, blank=True)
views.py
def recipeUpdate(request, recipe_id):
RecipeIngredientFormSet2 = inlineformset_factory(Recipe, Recipe_Ingredient, extra=10, fields=('ingredient', 'recipe_percent'))
recipe = Recipe.objects.get(pk=recipe_id)
formset = RecipeIngredientFormSet2(instance=recipe)
context = {'formset' : formset}
return render(request, 'accounts/recipe_form.html', context)
recipe_form.html
{% extends 'accounts/main.html' %}
{% load static %}
{% block content %}
<div class="row">
<div class="col-md-6">
<div class="card card-body">
<form action="" method="POST">
{{ form }}
<p></p>
<!--{{ formset.as_table }}-->
<p></p>
{{ formset.management_form}}
<!--{% for form in formset %}
{{ form }}
{% endfor %}-->
<table>
<thead>
{% for form in formset %}
{% if forloop.first %}
{% for field in form %}
<th>{{ field.label_tag }}</th>
{% endfor %}
{% endif %}
</thead>
<tbody>
<tr>
{% for field in form %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
<input type="submit" name="Submit">
</form>
</div>
</div>
</div>
{% endblock %}
Found the answer. You've got to add the formset with a .management_form tag, then in table, loop through each form in form set. If it's the first loop, add the headers to the tables. In all other loops, add each field from the form that you want in the table.
The updated HTML that I'm using is below.
{% extends 'accounts/main.html' %}
{% load static %}
{% block content %}
<div class="row">
<div class="col-md-6">
<div class="card card-body">
<form action="" method="POST">
{% csrf_token %}
{{ form }}
{{ formset.management_form }}
<table>
{{ form.id }}
{% for p in formset %}
<tr>
{% if forloop.first %}
<td>{{ p.DELETE.label_tag }}</td>
<td>{{ p.ingredient.label_tag }}</td>
<td>{{ p.recipe_percent.label_tag }}</td>
<p></p>
{% endif %}
</tr>
<!--{{ p.id }}
{{ p.ORDER }}-->
<tr></tr>
<td>{{ p.DELETE }}</td>
<td>{{ p.ingredient }}</td>
<td>{{ p.recipe_percent }}</td>
</tr>
{% endfor %}
</table>
<input type="submit" name="Submit">
</form>
</div>
</div>
</div>
{% endblock %}

Django - How to change dropdown form into table

I'm creating an application in Django and I'm having an issue with iterating through options in a dropdown from a form in Django. I was wondering if anyone could be of assistance.
detail.html
{% for subject in subject_list %}
<form action="" method="POST">
{% csrf_token %}
<table class='table table-borderless table-hover'>
<tbody>
<tr>
<td>{{ subject.subjectname }}
{% for field in form %}
{{ field.hidden }}
{% endfor %}
</td>
</tr>
</tbody>
</table>
<button type="submit" id="login-btn" class="btn">Create a new evaluation</button>
</form>
{% endfor %}
forms.py
class CreateEvalForm(forms.ModelForm):
subjectname = ModelChoiceField(queryset=Subject.objects, label='', empty_label="Choose subject..")
class Meta:
model = Evaluation
fields = ['subjectname', ]
models.py
class Subject(models.Model):
id = models.AutoField(primary_key=True)
subjectname = models.CharField(max_length=255, help_text="Type the name of the subject.")
slug = models.SlugField(max_length=200, unique=True)
Ideally we want it to look like on the picture below:
Note that the picture is in danish and Fag = Subject, Opret ny evaluering = Create new evaluation and Se evaluering = View evaluation
we want this
we don't want this
I solved this with the help from the comments by changing my for loop and table to this:
<form action="" method="POST">
{% csrf_token %}
<table class='table table-borderless table-hover'>
{% for option in form.fields.fagnavn.choices %}
{% if not forloop.first %}
<tbody>
<tr>
<td>{{ option.1 }}</td>
<td><button value="{{ option.0 }}" name="fagnavn">Opret ny evaluering</button></td>
</tr>
</tbody>
{% endif %}
{% endfor %}
</table>
</form>
I gained access to the choices in the dropdown with form.fields.fagnavn.choices and excluded the default value of "choose value" with an if statement of {% if not forloop.first %}
And that's pretty much it.

Why is my Class-based delete view not working?

My delete view is not working and the error message is:
Page not found (404) Request Method: GET.
I am trying to delete my uploaded file based on its primary key and so far, my url is able to link me correctly based on the pk.
This is my urls.py:
path('post/<int:post_id>/lesson_delete/<int:lesson_id>', LessonDeleteView.as_view(), name='lesson_delete'),
My views.py:
class LessonDeleteView(DeleteView):
model = Lesson
success_url = '../'
template_name = 'lesson_confirm_delete.html'
This is the html template that brings user to the delete view:
{% extends "store/base.html" %}
{% block content %}
<div id="main">
<table class="table mb-0">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Download</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for l in Lesson %}
<tr>
<td>
{% if l.file %}
{{ l.title }}
{% else %}
<h6>Not available</h6>
{% endif %}
</td>
<td>{{ l.post.author }}</td>
<td>{% if l.file %}
Download
{% else %}
<h6>Not available</h6>
{% endif %}
</td>
<td> <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'lesson_delete' lesson_id=l.id %}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
This is my html template for DeleteView:
{% extends "store/base.html" %}
{% block content %}
<div class="content-section" id="main">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Delete Lesson</legend>
<h2>Are you sure you want to delete the post "{{object.title}}"?
</h2>
</fieldset>
<span style="display:inline;">
<button class="btn btn-outline-danger" type="submit">Yes, Delete!
</button>
<a class="btn btn-outline-secondary" href ="">Cancel</a>
</span>
</form>
</div>
{% endblock content %}
This is my Lesson Model:
class Lesson(models.Model):
title = models.CharField(max_length=100)
file = models.FileField(upload_to="lesson/pdf")
date_posted = models.DateTimeField(default=timezone.now)
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=False, blank=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('lesson_upload', kwargs={'pk': self.pk})
Is you template called 'lesson_confirm_delete.html'?
Also, for your success url, I have feeling you don't have a path '../'. It should the specific path you want it to go to.
(Sorry, I can't comment yet.)