My views.py
class SearchView(TemplateView):
template_name = 'search.html'
def get(self, request, *args, **kwargs):
q = request.GET.get('q', '')
self.results = Item.objects.filter(title__icontains=q)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
return super().get_context_data(results=self.results, **kwargs)
My urls.py
url(r'^search/$', SearchView.as_view(), name='search')
My search.html
{% extends 'base.html' %} {% load static %} {% block content %}
<body>
<h1>Search Result</h1>
<ul>
{% for item in q %}
<li>
{{ q.title }}, {{ q.price }}
</li>
{% endfor %}
</ul>
</body>
{% endblock%}}
My nav.html
<form method="GET" action="{% url 'core:search' %}">
This is the code that i used but due to some missing or error in this above code i can't get any data if i make any search in my website, Can some one please tell me what is the mistake i have done.
Thank you.
My Models.py
class Item(models.Model):
title = models.CharField(max_length=50)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
model_no = models.CharField(max_length=100)
try this:
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['results'] = self.results
return context
in html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<body>
<h1>Search Result</h1>
<ul>
{% for item in results %}
<li>
{{ item.title }}, {{ item.price }}
</li>
{% endfor %}
</ul>
</body>
{% endblock%}}
Related
I made a form and then i made little search form and it works but, i didnt display the search term on the search results page...
I just wanna show Search Results for (Search Term)
Here are my codes.
Views.py
class SearchResultsView(ListView):
model = Post
template_name = 'search.html'
def get_queryset(self):
query = self.request.GET.get('q')
object_list = Post.objects.filter(Q(soru1__icontains=query) | Q(soru2__icontains=query) |
Q(soru3__icontains=query) |
Q(soru4__icontains=query) |
)
return object_list
Base.html ( search bar in it)
<form class="d-flex" action="{% url 'search' %}" method="get">
<input class="form-control me-2" name="q" type="text" placeholder="Arama...">
<button class="btn btn-outline-success" type="submit">Ara</button></form>
And search.html
{% extends 'base.html' %}
{% block content %}
<h1>Search Results for </h1>
<ul>
{% for Post in object_list %}
<li>
{{ Post.soru1 }}
</li>
{% endfor %}
</ul>
{% endblock %}
You need to add query to context:
Views.py
class SearchResultsView(ListView):
model = Post
template_name = 'search.html'
def get_queryset(self):
query = self.request.GET.get('q')
object_list = Post.objects.filter(Q(soru1__icontains=query) | Q(soru2__icontains=query) |
Q(soru3__icontains=query) |
Q(soru4__icontains=query) )
return object_list
def get_context_data(self, **kwargs):
context = super(SearchResultsView, self).get_context_data(**kwargs)
context['query'] = self.request.GET.get('q')
return context
and in template:
{% extends 'base.html' %}
{% block content %}
<h1>Search Results for {{ query }} </h1>
<ul>
{% for Post in object_list %}
<li>
{{ Post.soru1 }}
</li>
{% endfor %}
</ul>
{% endblock %}
I have designed my search box in my django app but the search only returns the extended html page, not the search results.
#View
class SearchListProject(ListView):
paginate_by = 4
template_name = 'website/search_project.html'
def get_queryset(self):
search_project = self.request.GET.get('q')
return Project.objects.filter(description__icontains=search_project)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['search_project'] = self.request.GET.get('q')
return context
#url
path('search_project/', SearchListProject.as_view(), name='search_project'),
path('search_project/page/<int:page>', SearchListProject.as_view(), name='search_project')
#html
{% extends 'website/project_list.html' %}
<h3 class="alert alert-primary text-center">Search: {{ search_project }}</h3>
{% block previous_page_url %}
{% url 'website:search_project' page_obj.previous_page_number %}?q={{ search_project }}
{% endblock %}
{% block next_page_url %}
{% url 'website:search_project' page_obj.next_page_number%}?q={{ search_project }}
{% endblock %}
#my form
<form action="{% url 'website:search_project' %}">
<input type="text" name="q">
<button type="submit"><i class="icofont-search"></i></button>
</form>
So I am trying to pass a list of objects into my template. I want my profile.html to reflect the information in the model. I found some documentation on ListView but nothing on CreateView.
The only thing that is passing through to the template is {{ user.username }}. Any suggestions would be greatly appreciated.
profile.html
{% extends 'base.html' %}
{% block title %}User Profile{% endblock %}
{% block content %}
{% if user.is_authenticated %}
<p>User: {{ user.username }} logged in.</p>
<p>homepage</p>
<p>logout</p>
{% else %}
login |
signup
{% endif %}
{% endblock %}
models.py
class Volunteer(CustomUser):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, primary_key=True)
about_me = models.TextField(default='')
class Meta:
db_table = "volunteer"
forms.py
class VolunteerSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Volunteer
fields = ("username", "email", "first_name", "last_name",)
#transaction.atomic
def save(self):
user = super(VolunteerSignUpForm, self).save(commit=False)
user.is_volunteer = True
user.save()
return user
views.py
class VolunteerSignUp(CreateView):
form_class = VolunteerSignUpForm
success_url = reverse_lazy('login')
template_name = 'volunteer_signup.html'
#added code from answer
def get_context_data(self, **kwargs):
context = super(VolunteerSignUp, self).get_context_data(**kwargs)
context['profile_list'] = Volunteer.objects.all()
return context
profile.html Here are some things iv tried to get the info across that didn't work.
<ul>
{% for volunteer in object_list %}
<li>{{ volunteer.about_me }}</li>
{% endfor %}
</ul>
<ul>
{% for volunteer in profile_list %}
<li>{{ volunteer.about_me }}</li>
{% endfor %}
</ul>
{% if profile_list %}
{% else %}
<p>There is no info.</p>
{% endif %}
You can use get_context_data() method in your class.
class VolunteerSignUp(CreateView):
form_class = VolunteerSignUpForm
success_url = reverse_lazy('login')
template_name = 'volunteer_signup.html'
def get_context_data(self, **kwargs):
data = super(VolunteerSignUp, self).get_context_data(**kwargs)
data['profile_list'] = 'your queryset goes here'
return data
models.py file
class Company(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Contact(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
def __str__(self):
return self.first_name
views.py file
class company_detail(DetailView):
def get(self, request, *args, **kwargs):
company = get_object_or_404(Company, pk=kwargs['pk'])
context = {'company': company}
return render(request, 'crm/company_detail.html', context)
company_detail.html file
{% extends 'base.html' %}
{% block content %}
<div id="container">
<ul>
<li>{{company.name}}</li>
</ul>
{% for company in object_list %}
{{ company.name }}
{% for Contact in company.contact_set.all %}
{{Contact.first_name}}
{% empty %}
<!-- no entries -->
{% endfor %}
{% endfor %}
</div>
{% endblock content %}
I'm trying to get the Contacts who are under that company to show up on the company_detail.html page. How do I reverse query it properly to show all Contacts under that company?
Thanks in advance
There is no object_list in your DetailView's context, only the object. You need to remove the for loop over object_list in the template
{% for contact in company.contact_set.all %}
{{ contact.first_name }}
{% empty %}
<!-- no entries -->
{% endfor %}
I want to show a table containing a set of model objects.
My model class:
from django.dbfrom django.db import models
from django.utils.translation import ugettext_lazy as _
class DamageKind(models.Model):
name = models.CharField(_('damage kind'), max_length=64)
regions = models.ManyToManyField(Region)
def __str__(self):
return self.name
class Meta:
verbose_name = _('damage kind')
verbose_name_plural = _('damage kinds')
my form class:
from django import forms
from crispy_forms.helper import FormHelper
from .models import DamageKind
class DamageKindList(forms.Form):
def __init__(self, *args, **kwargs):
self.damagekinds = kwargs.pop('damagekinds', [])
self.helper = FormHelper()
self.helper.form_method = 'post'
super().__init__(*args, **kwargs)
My base template base.html:
<!DOCTYPE html>
<html>
<body>
{% block list %}
{% endblock %}
</body>
</html>
my list_damagekinds.html:
{% extends "./base.html" %}
{% load crispy_forms_tags %}
{% block list %}
<form action="" method="post">
{% csrf_token %}
{{ damagekind_form }}
</form>
{% endblock %}
and my views.py:
def list_damagekinds(request):
damagekinds = DamageKind.objects.all()
return render(
request,
'damage/list_damagekinds.html',
{'damagekind_form': DamageKindList(damagekinds=damagekinds), }
)
so my question is how I can make a table containing all the names of the damagekinds by not beeing editable, so just showing these. And with using the crispy_forms FormHelper and not code it into the template.
Thanks in advance.
my list_damagekinds.html
{% extends "./base.html" %}
{% load crispy_forms_tags %}
{% block list %}
<form action="" method="post">
{% csrf_token %}
<table>
{% for obj in damagekind_form %}
<tr> {{ obj.name }} </tr>
{% endfor %}
</table>
</form>
{% endblock %}