In my Django website, I'm trying to create a search bar on my homepage that returns results that are stored in two different models(Articles, Jobs)
Currently, I get an empty result set when I search using this code:
In models.py,
class Articles(models.Model):
objects = None
news_title = models.CharField(db_column="title", max_length=400)
news_url = models.URLField(db_column="link", max_length=400)
image_link = models.CharField(db_column="image", max_length=400)
published_date = models.CharField(db_column="published_date", max_length=400)
class Meta:
managed = False
db_table = "articles"
def __str__(self):
return self.news_title
class Jobs(models.Model):
objects = None
company = models.CharField(db_column='company', max_length=100)
job_title = models.CharField(db_column='job_title', max_length=300)
experience = models.CharField(db_column='experience', max_length=300)
edu_level = models.CharField(db_column='edu_level', max_length=50)
class Meta:
managed = False
db_table = "job_list"
def __str__(self):
return self.job_title
In views.py,
class SearchView(ListView):
template_name = 'blog/search_results.html'
def get_queryset(self):
request = self.request
query = request.GET.get('q', '')
articles_results = Articles.objects.filter(Q(news_title__icontains=query))
jobs_results = Jobs.objects.filter(Q(job_title__icontains=query))
context={
'articles':articles_results,
'jobs':jobs_results,
}
return render(request, 'blog/search_results.html', context)
In main_view.html, I'm using this code for creating a search bar:
<form action="{%url 'search_results' %}" method="get" values="{{request.GET.q}}" class="search-jobs-form">
<div class="row mb-5">
<input name="q" type="text" values="{{request.GET.q}}" placeholder="search">
</div>
<button type="submit">Search</button>
</form>
And in search_results.html,
{% block content %}
{% for job in jobs %}
<h5>{{job.job_title}}</h5>
<p>{{job.company}}</p>
{% endfor %}
{% for article in articles %}
<h5>{{article.news_title}}</h5>
<p>{{article.published_date}}</p>
{% endfor %}
{% endblock %}
I ended up not using class SearchView(ListView) in views.py and made it work
Here's the code that I used
#index.html Search Bar
<form method="get" class="search-jobs-form" action="{% url 'main_search'%}">
{% csrf_token %}
<input type="text" placeholder="Job title, Company..." name="search_query">
<button type="submit" class="btn btn-info btn-lg btn-block text-white btn-search">Search Job</button>
</form>
In views,
#views.py
def main_search(request):
q = request.GET['search_query']
articles = Articles.objects.filter(news_title__icontains=q)
jobs = Jobs.objects.filter(Q(job_title__icontains=q) | Q(company__icontains=q))
return render(request, "blog/main_search.html", {'articles':articles, 'jobs':jobs, 'page_name':'Search Results', 'q':q})
Related
in my project, I have three models
models.py
class Category(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True, null=True, blank=True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True, related_name='children')
class Tag(models.Model):
tag = models.CharField(max_length=75, verbose_name='Tag')
slug = models.SlugField(null=True)
class Post(models.Model):
title = models.CharField(max_length=150)
slug = models.SlugField(max_length=150, null=True, blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
tags = models.ManyToManyField(Tag, related_name='tags', blank=True)
and I created search filter view in views.py
def is_valid_queryparam(param):
return param != '' and param is not None
class SearchPepsiView(ListView):
template_name = "blog/NewSearch.html"
model = Post
paginate_by = 10
def get_queryset(self):
return Post.objects.filter(category__slug=self.kwargs['slug'])
def get_context_data(self, *args, **kwargs):
context = super(SearchPepsiView, self).get_context_data(*args, **kwargs)
context['category'] = Post.objects.filter(category__slug=self.kwargs['category'])
return context
def get(self, request, *args, **kwargs):
request = self.request
qs = Post.objects.all()
categories = Category.objects.filter(parent=None).order_by('name')
PostOrAuthor_query = request.GET.get('PostOrAuthor')
SearchCategory_query = request.GET.get('SearchCategory')
if is_valid_queryparam(PostOrAuthor_query):
qs = qs.filter(Q(title__icontains=PostOrAuthor_query) |
Q(content__icontains=PostOrAuthor_query) |
Q(author__username__icontains=PostOrAuthor_query)).distinct()
if is_valid_queryparam(SearchCategory_query) and SearchCategory_query != 'Choose...':
qs = qs.filter(category__name=SearchCategory_query)
count = qs.count() or 0
return render(request, self.template_name, {
'queryset': qs,
'categories': categories,
'count': count,
})
and I created Post Per Category View in views.py
class PostPerCategoryCBV(ListView):
model = Post
template_name = 'blog/Category_Projects.html'
def get_queryset(self):
self.category = Category.objects.get(slug=self.kwargs['slug'])
return Post.objects.filter(category=self.category)
def get_context_data(self, **kwargs):
context = super(PostPerCategoryCBV, self).get_context_data(**kwargs)
context['category'] = self.category
return context
and I created Post Per Tag View in views.py
class PostPerTagCBV(ListView):
model = Post
template_name = 'blog/tag_projects.html'
def get_queryset(self):
qs = super().get_queryset()
qs = qs.filter(tags__slug=self.kwargs['slug'])
return qs
and in templates, I created NewSearch.html
<form class="col-12 mt-5 mb-5">
<div class="form-group">
<label for="formGroupExampleInput">Post or Author</label>
<input type="search" name="PostOrAuthor" class="form-control" id="inputAddress" placeholder="Post or Author">
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="SearchCategory">Category</label>
<select id="SearchCategory" name="SearchCategory" class="form-control">
<option selected>Choose...</option>
{% for cat in categories %}
<option value="{{ cat.name }}">{{ cat.name }}</option>
{% endfor %}
</select>
</div>
</div>
<button type="submit" class="btn btn-primary col-12">Search</button>
</form>
</div>
<div class="row">
<p>You have<b> {{ count }} </b>search results </p>
</div>
{% if category %}
<div class="row">
{% for post in category %}
{% include 'blog/Post_Loop.html' %}
{% endfor %}
</div>
{% else %}
<div class="row">
{% for post in queryset %}
{% include 'blog/Post_Loop.html' %}
{% endfor %}
</div>
{% endif %}
</div>
and Here is my URLs.py
path('post_list/', PostListCBV.as_view(), name='list-post'),
path("search-pepsi/", SearchPepsiView.as_view(), name="search-pepsi"),
path('tags_projects/<str:slug>/', PostPerTagCBV.as_view(), name='tag-posts'),
path('category_posts/<str:slug>/', SearchPepsiView.as_view(), name='category-posts'),
My success:
When all search fields are empty .. it list all posts.
when entering any search query .. it backs with the search result.
my problem is:
I believe that there are someway I can combine ListView of Post_Per_Category and Post_Per_Tag with Search View (in ONE View and one template) .. so I created def get_queryset(self): to use it but it didn't worked.
Anybody can help please ...
Thanks in-advance
Model
class Timetable(models.Model):
day = models.CharField(max_length=9,choices=timetable_choices)
start = models.IntegerField()
end = models.IntegerField()
period = models.CharField(max_length=12)
Views
class Timetableadding(CreateView):
model = Timetable
fields = ['day','period','start' ,'end']
success_url = '/dashboard'
What I need is to process a view similar to following image ,
NB: I am not good in js so i want a solution without the use of JS
Views
class Timetableadding(CreateView):
model = Timetable
success_url = '/dashboard/'
form_class = Timetableform
template_name = 'form.html'
def get_context_data(self, **kwargs):
context = super(Timetableadding, self).get_context_data(**kwargs)
context['formset'] = TimetableFormSet(queryset=Timetable.objects.none())
context['day_form'] = DayForm()
return context
def post(self, request, *args, **kwargs):
formset = TimetableFormSet(request.POST)
day_form = DayForm(data=request.POST)
if formset.is_valid() and day_form.is_valid():
return self.form_valid(formset,day_form)
def form_valid(self, formset,day_form):
day = day_form.cleaned_data['day']
instances = formset.save(commit=False)
for instance in instances:
instance.day = day
instance.save()
return HttpResponseRedirect('/dashboard/')
Forms
class DayForm(Form):
day = ModelChoiceField(queryset=Day.objects.all())
class Timetableform(ModelForm):
class Meta:
model = Timetable
fields = ( 'day','start', 'end', 'period')
TimetableFormSet = modelformset_factory(Timetable, fields=('start', 'end', 'period'),extra=8,)
Template
{% csrf_token %}
{{ day_form }} <br>
{{ formset.management_form }}
{% for form in formset %}
{{ form }}<br><br>
{% endfor %}
CREATE FORMS.PY
class MyForm(ModelForm):
class Meta:
model = Timetable
fields = ['day','start','end','period',]
Views.py
from django.forms import formset_factory
class YourView(CreateView):
form = formset_factory(MyForm)
model = Timetable
success_url ="Your success url"
template_name = "your template"
In Your Templates
<form method="post">{% csrf_token %}
<fieldset>
<div class="row">
<div class="form-group col-lg-12">
{{ form.management_form }}
{% for contact in form %}
<div class="link-formset">
{{ contact.as_p }}
</div>
{% endfor %}
<button class="user-sent" type="submit" value="Send"> Send</button>
</div>
</div>
</fieldset>
<script src="{% static 'forms/jquery.formset.js' %}"></script>
<script>
$('.link-formset').formset({
addText: '<i class="fa fa-plus"></i> Add User',
deleteText: '<i class="fa fa-trash-o"></i>Remove'
});
</script>
</form>
I want to set a Boolean field is_rep in a retest model to true when the retest form is submitted.
Now it is just get added up in a retest model.
Because I want to trigger other events when the request is submitted.
My code
models.py
class Retest(models.Model):
semester = models.ForeignKey(Semester)
dept = models.ForeignKey(Departement)
batch = models.ForeignKey(Batch)
date = models.DateField(default=0)
subject = models.ForeignKey(Subject)
name = models.CharField(max_length=50)
admnno = models.CharField(max_length=50)
reason = models.CharField(max_length=50)
proof = models.CharField(max_length=200)
is_hod = models.BooleanField(default=False)
is_principal = models.BooleanField(default=False)
notify = models.BooleanField(default=False)
is_sure = models.BooleanField(default=False)
is_rep = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse( 'retest:retestform')
def __str__(self):
return self.name
urls.py
url(r'^retest/retestform/$',login_required(views.RetestCreate.as_view()), name='retestform')
views.py
class RetestCreate(CreateView):
model = Retest
fields = ['semester', 'dept', 'batch', 'date', 'subject', 'name', 'admnno', 'reason', 'proof', 'is_sure']
template
<form class="form_horizontal" action="" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{% include 'retest/form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
If you want to set the boolean field to true, when the form is submitted, you just have to handle it in the view.
Submit the form and before saving it into the database just set the is_rep = true.
is_rep is a field which is in the model, but not used in the form.
So, if you want to change that then you have to manually write a view for it. Try to use base view instead of generic views to understand the workflow of the views and forms.
I'd recommend using something like this:
class RetestView(View):
def get(self, request, *args, **kwargs):
..............
return render(request, self.template, {"some_context"}
def post(self, request, *args, **kwargs):
form_data = your_form(request.POST)
if form_data.is_valid():
new_object = form_data.save(commit=False)
new_object.is_rep = True
new_object.save()
return render(request, self.template, {"some...context"})
Hope you got what you were looking for..!
form_template
{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ field.errors }} </span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }} </label>
<div class ="col-sm-12">
<div class="form-control">
{{ field }}</div></div>
</div>
{% endfor %}
I have created a Django form to get the data from user and search that given data in database models.
But i am not able to use the dropdown functionality correctly. I have used ModelChoiceField but I am getting it empty.
I have three fields(forms.py) which i am searching in the database.
models.py
class Release(models.Model):
number = models.CharField(max_length=50,unique=True)
notes = models.CharField(max_length=50)
changes = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.number)
class Metamodule(models.Model):
name = models.CharField(max_length=50,unique=True)
version = models.IntegerField(default=0)
release = models.ForeignKey(Release,related_name="num")
createdate = models.DateField(auto_now=True, null=True)
createdby = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.name)
class Module(models.Model):
name = models.CharField(max_length=50,unique=True)
version = models.IntegerField(default=0)
metamodule = models.ForeignKey(Metamodule,related_name="metaname")
createdate = models.DateField(auto_now=True, null=True)
changes = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.name)
forms.py
class ModuleForm(forms.Form):
release_num = forms.ModelChoiceField(queryset=Release.objects.all().values('number'),empty_label='Pick a Release')
metamodule_name = forms.CharField(max_length=50)
module_name = forms.CharField(max_length=50)
views.py
from django.shortcuts import render
from search.forms import ModuleForm
from django.http import HttpResponse
from search.models import Module,Metamodule,Release
def searchview(request):
if request.method == 'GET':
form = ModuleForm(request.GET)
if form.is_valid():
release_num = form.cleaned_data['release_num']
metamodule_name = form.cleaned_data['metamodule_name']
module_name = form.cleaned_data['module_name']
results = Module.objects.filter(metamodule__release__number=release_num).filter(metamodule__name=metamodule_name).filter(name=module_name)
return render(request,'search/search_result.html',{'form': form, 'results': results})
else:
form = ModuleForm()
return render(request, 'search/search_form.html',{'form': form})
search_form.html
<html>
<head>
<title>Search</title>
</head>
<body>
{% if error %}
<p style="color: red;">Please submit a search term.</p>
{% endif %}
<form action="/search/" method="get">
<select name="release_num">
{% for release_num in release_num %}
<option value="{{ release_num.number }}">{{ release_num.number }}</option>
{% endfor %}
</select>
<p><label for="metamodule_name">Metamodule:</label>
<input type="text" name="metamodule_name">
<p><label for="module_name">Module:</label>
<input type="text" name="module_name">
<input type="submit" value="Search">
</form>
</body>
</html>
try in the search_form.html:
<form action="/search/" method="get">
{{ form.release_num }}
{{ form.metamodule_name }}
{{ form.module_name }}
I am trying to use Taggit for users to be able to tag their posts while they are submitting a form. But I can successfully let them type manually(their tags), I am trying to change to check boxes. Any ideas?
forms.py
class TalesForm(ModelForm):
class Meta:
model = Tale
fields = ('title', 'body', 'tags')
m_tags = TagField()
models.py
class Tale(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField(max_length = 10000)
pub_date = models.DateTimeField(default = datetime.now)
poster = models.CharField(max_length = 30)
tags = TaggableManager()
def __unicode__(self):
return self.title
views.py
def add(request):
if request.user.is_authenticated():
if request.method=='POST':
form=TalesForm(request.POST)
if form.is_valid():
m_tags = form.cleaned_data['tags']
newTale=Tale(title=form.cleaned_data['title'], body=form.cleaned_data['body'], poster = request.user)
newTale.save()
for m_tag in m_tags:
newTale.tags.add(m_tag)
#form.save_m2m()
return HttpResponseRedirect('/home/%s'%newTale.id)
else:
return render_to_response('story/add.html', {'form':form}, context_instance=RequestContext(request))
else:
form=TalesForm()
args = {}
args.update(csrf(request))
args['form'] = form
context= {'form': form}
return render_to_response('story/add.html',context, context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/home')
html
<form method="post" action="/home/add/">
{% csrf_token %}
<div class="register_div">
{% if form.title.errors %}<p class="error" >{{ form.title.errors }}</p>{% endif %}
<p><label for="title"{% if form.title.errors %} class="error"{% endif %}>Title:</label></p>
<p>{{ form.title}}</p>
</div>
<div class="register_div">
{% if form.body.errors %}<p class="error" >{{ form.body.errors }}</p>{% endif %}
<p><label for="body"{% if form.body.errors %} class="error"{% endif %}>Body:</label></p>
<p>{{ form.body }}</p>
</div>
<div class="register_div">
<p><label for="tag">Tags:</label></p>
<p>{{ form.tags }}</p>
</div>
<input type="submit" value="Add Story" name="submit" />
</form>
Define your form this way:
from taggit.models import Tag
class TalesForm(ModelForm):
tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
widget=forms.CheckboxSelectMultiple())
class Meta:
model = Tale
fields = ('title', 'body', 'tags')