Passing CHOICES to as an ID to template - python

As you can see the photo above, I have displayed all "CATEGORY_CHOICES" (in forms of (Groceries, Groceries) in category.html.
views.py
def category(request):
context = {'CATEGORY_CHOICES': Category.CATEGORY_CHOICES}
return render(request, 'category.html', context)
def view_category(request, category_id):
category = Category.objects.get(category_id=category_id)
return render(request, 'view_category.html', {'category':category})
models.py
class Category(models.Model):
CATEGORY_CHOICES = [
('Groceries', 'Groceries'),
('Salary', 'Salary'),
('Bills', 'Bills'),
('Rent', 'Rent'),
('Gym', 'Gym'),
('Restaurant', 'Restaurant'),
('Vacation', 'Vacation'),
('Travel', 'Travel'),
('Gift', 'Gift'),
('Investments', 'Investments'),
('Savings', 'Savings'),
('Entertainment', 'Entertainment'),
('Internet', 'Internet'),
('Healthcare', 'Healthcare'),
('Lifestyle', 'Lifestyle'),
('Insurance', 'Insurance'),
('Other', 'Other'),
]
category_choices = models.CharField(max_length=50, blank=False, choices=CATEGORY_CHOICES)
budget = models.DecimalField(max_digits=10, decimal_places=2)
start_date = models.DateField(blank=False)
end_date = models.DateField(blank=False)
spending_limit = models.DecimalField(max_digits=10, decimal_places=2)
category.html
{% extends 'base_content.html' %}
{% block content %}
<div class="container">
<div class="row justify-content-center">
<div class="row" style="width:90%">
<div class="col-sm-4 text-center my-3">
<div class="card card-block" style="height : 300px">
<div class="card-body align-items-center d-flex justify-content-center">
<h5 class="card-title">Overall</h5>
</div>
</div>
</div>
{% for item in CATEGORY_CHOICES %}
<div class="col-sm-4 text-center my-3">
<div class="card card-block" style="height : 300px">
<div class="card-body align-items-center d-flex justify-content-center">
<h5 class="card-title">{{ item.1 }}</h5>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home_page, name ='home_page'),
path('feed/',views.feed, name ='feed'),
path('sign_up/', views.SignUpView.as_view(), name='sign_up'),
path('log_in/', views.LogInView.as_view(), name='log_in'),
path('log_out/', views.log_out, name='log_out'),
path('profile/', views.ProfileUpdateView.as_view(), name='profile'),
path('new_transaction/',views.new_transaction, name ='new_transaction'),
path('category',views.category, name = 'category'),
path('category/view/<str:???***confused this part too***>/',views.view_category, name
= 'view_category'),
As you can see in the code, I'm trying to pass category name to make url to be category/view/'category_name'. However, this category name is not a field of Category model but from category_choices. I need to fetch the exact category_choices to pass into url. Two things are confusing to me. First, how am I going to extract category choice into url as an ID? Second is how am I going to configure the url for 'view_category'?
error message
NoReverseMatch at /category
Reverse for 'view_category' with arguments '(('Groceries', 'Groceries'),)' not found.
1 pattern(s) tried: ['category/view/(?P<category_id>[0-9]+)/\\Z']
Of course the error is found at

Related

NoReverseMatch at /blog Reverse for 'single_blog' with no arguments not found. 1 pattern(s) tried: ['single_blog/(?P<id>[0-9]+)/$']

I tried many times but the page not rendering , i am not understanding where i did wrong? could you please let me know , where i did wrong?
models.py
class Post(models.Model):
head = models.CharField(blank=False, unique=True, max_length=250)
date_time = models.DateTimeField(blank=True, null=True, default=None)
description = models.TextField(blank=False, max_length=1000)
by_name = models.CharField(blank=False, null=True, unique=True, max_length=250)
by_img = models.ImageField(null=True, blank=True)
def __str__(self):
return self.head
views.py
def blog(request):
blogs = Post.objects.all()
return render(request, 'blog.html', {'blogs': blogs})
def blog(request):
blogs = Post.objects.all()
return render(request, 'blog.html', {'blogs': blogs})
def dynamicblog(request, id):
obj = Post.objects.get(id=id)
return render(request, 'single_blog.html', {'obj': obj})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('contact', views.contact, name='contact'),
path('shop', views.shop, name='shop'),
path('service', views.service, name='service'),
path('blog', views.blog, name='blog'),
path('single_blog/<int:id>/', views.dynamicblog, name='single_blog'),
path('single_service', views.single_service, name='single_service'),
blog.html
{% for blog in blogs %}
<div class="col-lg-4 col-md-6 col-sm-12 news-block">
<div class="news-block-two news-block-three wow fadeInLeft" data-wow-delay="300ms" data-wow-duration="1500ms">
<div class="inner-box">
<div class="image-holder">
<figure class="image-box"><img src="{{blog.by_img.url}}" alt=""></figure>
</div>
<div class="lower-content">
<h4>{{blog.head}}</h4>
<div class="post-info">by {{blog.by_name}} on {{blog.date_time}}</div>
<div class="text">{{blog.description}}</div>
<div class="link-btn">Read More<i class="flaticon-slim-right"></i></div>
</div>
</div>
</div>
</div>
{% endfor %}
single_blog.html
<div class="col-lg-4 col-md-12 col-sm-12 sidebar-side">
{% for blog in obj %}
<div class="sidebar blog-sidebar">
<div class="contact-widget sidebar-widget wow fadeInLeft" data-wow-delay="00ms" data-wow-duration="1500ms">
<div class="widget-content">
<h4>{{blog.head}}</h4>
<div class="text">Our industrial plant services keep your generator up</div>
<div class="btn-box"><i class="fas fa-angle-right"></i>Contact Agent</div>
</div>
</div>
</div>
{% endfor %}
</div>
Your single_blog path expect some arguments. So pass it :
blog.html
{% for blog in blogs %}
<div class="col-lg-4 col-md-6 col-sm-12 news-block">
<div class="news-block-two news-block-three wow fadeInLeft" data-wow-delay="300ms" data-wow-duration="1500ms">
<div class="inner-box">
<div class="image-holder">
<figure class="image-box"><img src="{{blog.by_img.url}}" alt=""></figure>
</div>
<div class="lower-content">
<!-- | Here -->
<!-- v -->
<h4>{{blog.head}}</h4>
<div class="post-info">by {{blog.by_name}} on {{blog.date_time}}</div>
<div class="text">{{blog.description}}</div>
<div class="link-btn">
<!-- | Here -->
<!-- v -->
<a href="{% url 'single_blog' id=blog.id %}">Read More<i class="flaticon-slim-right"></i>
</a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
Refs

How to filter draft contents from django website?

I have two options for my articles in my django website: "draft" and "published"
I wrote sth that helps me show only the articles that are on "published" status in admin page.
But the code doesn't work. when I click on the specified category for each article, even the draft ones show up. How can I fix it?
#################
#models.py
from django.db import models
from django.utils import timezone
# my managers
class ArticleManager(models.Manager):
def published(self):
return self.filter(status='Published')
# Create your models here.
class Category(models.Model):
title = models.CharField(max_length=300, verbose_name="Category Topic")
slug = models.SlugField(max_length=100, unique=True, verbose_name="Category Address")
status = models.BooleanField(default=True, verbose_name="Do you want to show?")
position = models.IntegerField(verbose_name="position")
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
ordering = ['position']
def __str__(self):
return self.title
class Article(models.Model):
STATUS_CHOICES = (
('Draft', 'Draft'),
('Published', 'Published')
)
title = models.CharField(max_length=300)
slug = models.SlugField(max_length=100, unique=True)
category = models.ManyToManyField(Category, verbose_name="Category", related_name="articles")
description = models.TextField()
thumbnail = models.ImageField(upload_to="images")
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
class Meta:
ordering = ['-publish']
def __str__(self):
return self.title
def category_published(self):
return self.category.filter(status=True)
objects = ArticleManager()
############
#Views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse, Http404
from .models import Article, Category
# Create your views here.
def home(request):
context = {
"articles": Article.objects.published()
}
return render(request, 'website/home.html', context)
def detail(request, slug):
context = {
"article": get_object_or_404(Article.objects.published(), slug=slug)
}
return render(request, 'website/detail.html', context)
def article(request):
context = {
"articles": Article.objects.filter(status="Published"),
"category": Category.objects.filter(status=True)
}
return render(request, 'website/article.html', context)
def category(request, slug):
cat = get_object_or_404(Category, slug=slug, status=True)
context = {
"category": cat.articles.all()
}
return render(request, 'website/category.html', context)
###########
#Category.html page
{% extends 'website/base.html' %}
{% load static %}
{% block main %}
<main id="main">
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Blog</h2>
<ol>
<li>Home</li>
<li>Blog</li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<!-- ======= Blog Section ======= -->
<section id="blog" class="blog">
<div class="container">
<div class="row">
<div class="col-lg-8 entries">
{% for article in category %}
<article class="entry" data-aos="fade-up">
<div class="entry-img">
<img src="{{ article.thumbnail.url }}" alt="" class="img-fluid">
</div>
<h2 class="entry-title">
{{ article.title }}
</h2>
<div class="entry-meta">
<ul>
<li class="d-flex align-items-center"><i class="icofont-user"></i>
John Doe</li>
<li class="d-flex align-items-center"><i class="icofont-wall-clock"></i>
<time>{{ article.publish }}</time></li>
<li class="d-flex align-items-center"><i class="icofont-tags"></i>
<ul class="tags">
{% for cat in article.category_published %}
#{{ cat.title }}
{% endfor %}
</ul>
</li>
<li class="d-flex align-items-center"><i class="icofont-comment"></i>
12 Comments</li>
</ul>
</div>
<div class="entry-content">
{{ article.description|truncatewords:30}}
<div class="read-more">
Read More
</div>
</div>
</article><!-- End blog entry -->
{% endfor %}
<div class="blog-pagination">
<ul class="justify-content-center">
<li class="disabled"><i class="icofont-rounded-left"></i></li>
<li>1</li>
<li class="active">2</li>
<li>3</li>
<li><i class="icofont-rounded-right"></i></li>
</ul>
</div>
</div><!-- End blog entries list -->
<div class="col-lg-4">
<div class="sidebar" data-aos="fade-left">
<h3 class="sidebar-title">Search</h3>
<div class="sidebar-item search-form">
<form action="">
<input type="text">
<button type="submit"><i class="icofont-search"></i></button>
</form>
</div><!-- End sidebar search formn-->
{# <h3 class="sidebar-title">Categories</h3>#}
{# <div class="sidebar-item categories">#}
{# <ul>#}
{# {% for cat in category %}#}
{# <li class="active"><a href="{% url 'website:category' cat.slug %}">#{{ cat.title
}}</a></li>#}
{# {% endfor %}#}
{# </ul>#}
{##}
{# </div><!-- End sidebar categories-->#}
<h3 class="sidebar-title">Recent Posts</h3>
<div class="sidebar-item recent-posts">
<div class="post-item clearfix">
<img src="assets/img/blog-recent-posts-1.jpg" alt="">
<h4>Nihil blanditiis at in nihil autem</h4>
<time datetime="2020-01-01">Jan 1, 2020</time>
</div>
<div class="post-item clearfix">
<img src="assets/img/blog-recent-posts-2.jpg" alt="">
<h4>Quidem autem et impedit</h4>
<time datetime="2020-01-01">Jan 1, 2020</time>
</div>
<div class="post-item clearfix">
<img src="assets/img/blog-recent-posts-3.jpg" alt="">
<h4>Id quia et et ut maxime similique occaecati ut</h4>
<time datetime="2020-01-01">Jan 1, 2020</time>
</div>
<div class="post-item clearfix">
<img src="assets/img/blog-recent-posts-4.jpg" alt="">
<h4>Laborum corporis quo dara net para</h4>
<time datetime="2020-01-01">Jan 1, 2020</time>
</div>
<div class="post-item clearfix">
<img src="assets/img/blog-recent-posts-5.jpg" alt="">
<h4>Et dolores corrupti quae illo quod dolor</h4>
<time datetime="2020-01-01">Jan 1, 2020</time>
</div>
</div><!-- End sidebar recent posts-->
<h3 class="sidebar-title">Tags</h3>
<div class="sidebar-item tags">
<ul>
<li>App</li>
<li>IT</li>
<li>Business</li>
<li>Business</li>
<li>Mac</li>
<li>Design</li>
<li>Office</li>
<li>Creative</li>
<li>Studio</li>
<li>Smart</li>
<li>Tips</li>
<li>Marketing</li>
</ul>
</div><!-- End sidebar tags-->
</div><!-- End sidebar -->
</div><!-- End blog sidebar -->
</div>
</div>
</section><!-- End Blog Section -->
</main><!-- End #main -->
{% endblock %}
Your category view is returning all articles, if you just want published articles in the category you need to filter them.
def category(request, slug):
# filter only published articles
cat = get_object_or_404(Category, slug=slug, status=True)
context = {
"published_articles_in_category": cat.articles.filter(status='Published')
}

Django form doesn't appear on template

I'm currently working on a project that require Django Forms but I ended up with some issues. My form doesn't display at all ... No field appear on my template.
So my code :
models.py
class Place(models.Model):
name = models.CharField(max_length=255)
longitudeMax = models.DecimalField(max_digits=8, decimal_places = 4 ,blank=True)
longitudeMin = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)
latitudeMax = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)
latitudeMin = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)
datasetPath = models.CharField(max_length=255)
isActive = models.BooleanField(default=True)
def __str__(self):
return self.name
def get_place(self, name):
return None
forms.py
class NewPlaceForm(forms.Form):
name = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder" : "Name",
"class": "form-control"
}
))
longMax = forms.DecimalField(
widget=forms.NumberInput(
attrs={
"placeholder" : "Longitude Max",
"class": "form-control"
}
))
longMin = forms.DecimalField(
widget=forms.NumberInput(
attrs={
"placeholder" : "Longitude Min",
"class": "form-control"
}
))
latMax = forms.DecimalField(
widget=forms.NumberInput(
attrs={
"placeholder" : "Latitude Max",
"class": "form-control"
}
))
latMin = forms.DecimalField(
widget=forms.NumberInput(
attrs={
"placeholder" : "Latitude Min",
"class": "form-control"
}
))
class Meta:
model = Place
fields = ('name', 'longitudeMax', 'longitudeMin', 'latitudeMax', 'latitudeMin')
views.py
def upload(request):
msg = None
if request.method == "POST":
form = NewPlaceForm(request.POST)
if form.is_valid():
form.save()
msg = 'Place created'
else:
msg = 'Form is not valid'
else:
form = NewPlaceForm()
return render(request, "pages/place_upload.html", {"form": form, "msg" : msg})
urls.py
from django.urls import path, re_path
from app import views
urlpatterns = [
#Data modification page
path('pages/place_modification.html', views.modification, name="place_modification"),
#New place adding page
path('pages/place_upload.html', views.upload, name="place_upload"),
# Matches any html file
re_path(r'^.*\.html', views.pages, name='pages'),
# The home page
path('', views.index, name='home'),
]
html file
<form method="post" action="">
{% csrf_token %}
<div class="card-body">
<div class="row">
<div class="col-md-6 col-lg-4">
<div class="form-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
</div>
{{ form.name }}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-lg-4">
<div class="form-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
</div>
{{ form.longMax }}
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
</div>
{{ form.longMin }}
</div>
</div >
<div class="col-md-6 col-lg-4">
<div class="input-group mb-3">
<div class="input-group-prepend">
</div>
{{ form.latMax }}
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
</div>
{{ form.latMin }}
</div>
</div>
<div class="col-md-6 col-lg-4">
<div class="form-group form-floating-label">
<small id="emailHelp2" class="form-text text-muted">Use negative numbers for South and positive numbers for North</small>
</div>
<div class="form-group form-floating-label">
<small id="emailHelp2" class="form-text text-muted">Use negative numbers for West and positive numbers for East</small>
</div>
</div>
</div>
</div>
<div class="card-action">
<button type="submit" class="btn btn-success">Submit</button>
<button class="btn btn-danger">Cancel</button>
</div>
</form>
I don't see why it doesn't work and it's been several days that I try to make it work ... Did I do something wrong ? Have I forgot something ? Because I don't feel like I did and it's getting on my nerve ahaha
Django handles the form rendering out of the box, to display a basic form, try:
<form method="POST" action="{% url 'namespaced:url' %}">
{ csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit">
</form>
That said, it looks like you're using bootstrap in your template, you might find the package django-bootstrap4 useful (https://github.com/zostera/django-bootstrap4).
Quick example:
pip install django-bootstrap4
and add bootstrap4 to your installed apps.
Then in your template:
{% load bootstrap4 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
<form action="{% url 'namespaced:url' %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</form>
If you want, you can render each field manually using:
{% bootstrap_field field %}, so you can do things like <div class="col-md-6">{% bootstrap_field my_field %}</div>, to manually arrange your form fields on the page.

Django Template Filter With Respect to Boolean Variable

My Html
{% for category in categories %}
<div class="row">
<h3 style="padding-left: 15px; padding-bottom: 15px">{% filter upper %}{{ category.name }}{% endfilter %}</h3>
</div>
<div class="row">
{% with products=category.product.all|is_available:True %}
{% for product in products|slice:":4" %}
<div class="product-width col-xl-3 col-lg-3 col-md-3 col-sm-6 col-12 mb-30">
<div class="product-wrapper">
<div class="product-img">
<a href="{% url 'shop:product' category.name product.id %}">
<img alt="" src="{{product.image.all.0.image.url }}">
</a>
<div class="product-action">
<a class="action-wishlist" href="#" title="Wishlist">
<i class="ion-android-favorite-outline"></i>
</a>
<a class="action-cart" href="#" title="Add To Cart">
<i class="ion-android-add"></i>
</a>
</div>
</div>
<div class="product-content text-left">
<div class="product-title">
<h4>
{{ product.name|title }}
</h4>
</div>
<div class="product-price-wrapper">
<span>{{product.price}} TL</span>
</div>
</div>
</div>
</div>
{% endfor %}
{% endwith %}
</div>
<div class="row justify-content-end">
Daha Fazla...
</div>
{% endfor %}
My Model
Each product has a many-to-many relation with categories and products also have an is_available variable.
class ProductCategories(models.Model):
name = models.CharField(max_length = 60)
image = models.ImageField(upload_to = 'ProductCategories')
publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
is_available = models.BooleanField()
class Product(models.Model):
category = models.ManyToManyField(ProductCategories, related_name="product")
name = models.CharField(max_length = 60)
price = models.DecimalField(max_digits=65, decimal_places=2)
description = models.TextField()
publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
stock_number = models.IntegerField()
is_available = models.BooleanField()
My View
categories = ProductCategories.objects.all()
return render(request, 'shop/shopping.html', {'categories' : categories})
I am listing 4 products under each category but I would like to filter products that are available.
Should I filter products within view class and pass to template filtered product object separate Queryset or should I apply all filters within the template?
If I should filter them within the template as I tried above, is there any way to filter product objects according to their availability?
Thanks,
class ProductManager(models.Manager):
def is_available(self):
return self.get_queryset().filter(is_available=True)
class Product(models.Model):
--------
objects = ProductManager()
views.py
product = Product.objects.is_available()
return render(request, 'shop/shopping.html', {'products' : product})
templates
{% for product in products %}
{{ product.name }}
{% for item in product.category.all %}
{{ item.name }}
{% endfor %}{% endfor %}
Create a folder called "templatetags" at the same level as models.py and views.py in your application folder
Create a new file with the desired name in this folder. For example : 'app_tags.py'
Create a new file named __ init __.py in this folder
Open app_tags.py and write this code sample for create custom template filter:
from ..models import ProductCategories
from django import template
register = template.Library()
#register.filter
def is_available(value, arg):
products = value.filter(is_available = arg)
return products
And use like this in your Html:
{% load app_tags %}
...
...
...
{% with products=category.product.all|is_available:True %}
...
...
Please try this solution. I hope this helps to you.

Keep data in the form after submit

I am implementing search by two fields in form in Django.
I want to keep input data after search.
For example I input "C++" and chose "IT"
then I received Default values
I tried to parse request variable --- e.g. data = request.POST.copy()
but did not achieved result. What is the reason of this problem?
How can I solve this problem?
This is my code:
models.py
class Company(models.Model):
name = models.CharField(max_length=200)
about = models.TextField()
def __str__(self):
return self.name
class Vacancy(models.Model):
company_key = models.ForeignKey(Company, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
salary = models.CharField(max_length=200, default='40.000')
text = models.TextField(default="The text about vacancy")
city = models.CharField(max_length=200, default='Москва')
date_str = models.CharField(max_length=50, default='12 сентября')
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
CHOICES = [
('ALL', 'ALL'),
('IT', 'IT'),
('FINANCE', 'FINANCE'),
('OTHER', 'OTHER'),
]
department = models.CharField(
max_length=20,
choices=CHOICES,
default='ALL',
)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
urls.py
urlpatterns = [
path('', HomePageView.as_view(), name='vacancy_list'),
path('search/', SearchResultsView.as_view(), name='search_results'),
path('vacancy/<int:pk>/', views.vacancy_detail, name='vacancy_detail'),
path('accounts/login/', BBLoginView.as_view(), name='login'),
path('accounts/profile/', profile, name='profile'),
path('accounts/logout/', BBLogoutView.as_view(), name='logout'),
views.py
class HomePageView(ListView):
model = Vacancy
template_name = 'vacancy_list/vacancy_list.html'
paginate_by = 2
page_kwarg = 'vacancy'
context_object_name = 'vacancies'
def vacancy_detail(request, pk):
vacancy = get_object_or_404(Vacancy, pk=pk)
return render(request, 'vacancy_list/vacancy_detail.html', {'vacancy': vacancy})
class SearchResultsView(ListView):
model = Vacancy
template_name = 'vacancy_list/search_results.html'
paginate_by = 2
page_kwarg = 'vacancy'
context_object_name = 'vacancies'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['query'] = self.request.GET.get('q')
# added param
context['query2'] = self.request.GET.get('q2')
return context
def get_queryset(self): # new
query = self.request.GET.get('q')
query2 = self.request.GET.get('q2')
object_list = Vacancy.objects.filter(
Q(title__icontains=query) and Q(department__icontains=query2)
)
return object_list
vacancy_list.html
{% block content %}
<div class="container col-md-8" style="margin:20px;">
<div class="container" style="margin-top: 40px; font-size: 2rem; padding-left: 0px;">
<form action="{% url 'search_results' %}" method="get">
<div class="row">
<div class="col-lg-8 col-md-6 col-xs-12">
<input name="q" type="text" placeholder="Search..." class="form-control">
</div>
<div class="col-lg-3 col-md-4 col-xs-12">
<select name="q2" class="form-control" id="exampleFormControlSelect1">
<option>ALL</option>
<option>IT</option>
<option>Finance</option>
<option>Other</option>
</select>
</div>
<div class="col-lg-1 col-md-2 col-xs-12" style="padding-left: 0px;">
<button class="btn btn-primary">Primary</button>
</div>
</div>
</form>
</div>
{% for vacancy in vacancies %}
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-8">
<h1>{{vacancy.title}}</h1>
</div>
<div class="col-md-4 text-right">
<p> {{ vacancy.salary}} </p>
</div>
</div>
</div>
....
{% endfor %}
search_result.html
{% extends 'vacancy_list/base.html' %}
{% block content %}
<h1> {{request}}</h1>
<div class="container col-md-8" style="margin:20px;">
<div class="container" style="margin-top: 40px; font-size: 2rem; padding-left: 0px;">
<form action="{% url 'search_results' %}" method="get">
<div class="row">
<div class="col-lg-8 col-md-6 col-xs-12">
<input name="q" type="text" placeholder="Search..." class="form-control">
</div>
<div class="col-lg-3 col-md-4 col-xs-12">
<select name="q2" class="form-control" id="exampleFormControlSelect1">
<option>ALL</option>
<option>IT</option>
<option>Finance</option>
<option>Other</option>
</select>
</div>
<div class="col-lg-1 col-md-2 col-xs-12" style="padding-left: 0px;">
<button class="btn btn-primary">Primary</button>
</div>
</div>
</form>
</div>
{% for vacancy in vacancies %}
...
{% endfor %}
{% endblock %}
In your search results template, you should use {{ query }} and {{ query2 }} and a little bit of logic to populate the text and drop-down boxes.

Categories

Resources