Tuesday, September 22, 2020
Home Africa Issues with select drop down filtering django

Issues with select drop down filtering django

I have issues getting the output of my experience_level and country select fields despite my best efforts,please kindly help,below are snippets of my code for better understanding

choice.py file to store all my selections:

salary_choices = {
    '100000': '$100,000',
    '200000': '$200,000',
    '300000': '$300,000',
    '400000': '$400,000',
    '500000': '$500,000',
    '600000': '$600,000',
    '700000': '$700,000',
    '800000': '$800,000',
    '900000': '$900,000',
    '1000000': '$1M+',
}


Country_choices = {
    'US': 'United States',
    'AF': 'Afghanistan',
    'AL': 'Albania',
    'DZ': 'Algeria',
    'AS': 'American Samoa',
    'AD': 'Andorra',
    'AO': 'Angola',
    'AI': 'Anguilla',
    'AQ': 'Antarctica',
    'AG': 'Antigua And Barbuda',
    'AR': 'Argentina',
    'AM': 'Armenia',
    'AW': 'Aruba',
    'AU': 'Australia',
    'AT': 'Austria',
    'AZ': 'Azerbaijan',
    'BS': 'Bahamas',
    'BH': 'Bahrain',
    'BD': 'Bangladesh',
    'BB': 'Barbados',
    'BY': 'Belarus',
    'BE': 'Belgium',
    'BZ': 'Belize',
    'BJ': 'Benin',
    'BM': 'Bermuda',
    'BT': 'Bhutan',
    'BO': 'Bolivia',
    'BA': 'Bosnia And Herzegowina',
    'BW': 'Botswana',
    'BV': 'Bouvet Island',
    'BR': 'Brazil',
    'BN': 'Brunei Darussalam',
    'BG': 'Bulgaria',
    'BF': 'Burkina Faso',
    'BI': 'Burundi',
    'KH': 'Cambodia',
    'CM': 'Cameroon',
    'CA': 'Canada',
    'CV': 'Cape Verde',
    'KY': 'Cayman Islands',
    'CF': 'Central African Rep',
    'TD': 'Chad',
    'CL': 'Chile',
    'CN': 'China',
    'CX': 'Christmas Island',
    'CC': 'Cocos Islands',
    'CO': 'Colombia',
    'KM': 'Comoros',
    'CG': 'Congo',
    'CK': 'Cook Islands',
    'CR': 'Costa Rica',
    'CI': 'Cote D`ivoire',
    'HR': 'Croatia',
    'CU': 'Cuba',
    'CY': 'Cyprus',
    'CZ': 'Czech Republic',
    'DK': 'Denmark',
    'DJ': 'Djibouti',
    'DM': 'Dominica',
    'DO': 'Dominican Republic',
    'TP': 'East Timor',
    'EC': 'Ecuador',
    'EG': 'Egypt',
    'SV': 'El Salvador',
    'GQ': 'Equatorial Guinea',
    'ER': 'Eritrea',
    'EE': 'Estonia',
    'ET': 'Ethiopia',
    'FK': 'Falkland Islands (Malvinas)',
    'FO': 'Faroe Islands',
    'FJ': 'Fiji',
    'FI': 'Finland',
    'FR': 'France',
    'GF': 'French Guiana',
    'PF': 'French Polynesia',
    'TF': 'French S. Territories',
    'GA': 'Gabon',
    'GM': 'Gambia',
    'GE': 'Georgia',
    'DE': 'Germany',
    'GH': 'Ghana',
    'GI': 'Gibraltar',
    'GR': 'Greece',
    'GL': 'Greenland',
    'GD': 'Grenada',
    'GP': 'Guadeloupe',
    'GU': 'Guam',
    'GT': 'Guatemala',
    'GN': 'Guinea',
    'GW': 'Guinea-Bissau',
    'GY': 'Guyana',
    'HT': 'Haiti',
    'HN': 'Honduras',
    'HK': 'Hong Kong',
    'HU': 'Hungary',
    'IS': 'Iceland',
    'IN': 'India',
    'ID': 'Indonesia',
    'IR': 'Iran',
    'IQ': 'Iraq',
    'IE': 'Ireland',
    'IL': 'Israel',
    'IT': 'Italy',
    'JM': 'Jamaica',
    'JP': 'Japan',
    'JO': 'Jordan',
    'KZ': 'Kazakhstan',
    'KE': 'Kenya',
    'KI': 'Kiribati',
    'KP': 'Korea (North)',
    'KR': 'Korea (South)',
    'KW': 'Kuwait',
    'KG': 'Kyrgyzstan',
    'LA': 'Laos',
    'LV': 'Latvia',
    'LB': 'Lebanon',
    'LS': 'Lesotho',
    'LR': 'Liberia',
    'LY': 'Libya',
    'LI': 'Liechtenstein',
    'LT': 'Lithuania',
    'LU': 'Luxembourg',
    'MO': 'Macau',
    'MK': 'Macedonia',
    'MG': 'Madagascar',
    'MW': 'Malawi',
    'MY': 'Malaysia',
    'MV': 'Maldives',
    'ML': 'Mali',
    'MT': 'Malta',
    'MH': 'Marshall Islands',
    'MQ': 'Martinique',
    'MR': 'Mauritania',
    'MU': 'Mauritius',
    'YT': 'Mayotte',
    'MX': 'Mexico',
    'FM': 'Micronesia',
    'MD': 'Moldova',
    'MC': 'Monaco',
    'MN': 'Mongolia',
    'M0': 'Montenegro',
    'MS': 'Montserrat',
    'MA': 'Morocco',
    'MZ': 'Mozambique',
    'MM': 'Myanmar',
    'NA': 'Namibia',
    'NR': 'Nauru',
    'NP': 'Nepal',
    'NL': 'Netherlands',
    'AN': 'Netherlands Antilles',
    'NC': 'New Caledonia',
    'NZ': 'New Zealand',
    'NI': 'Nicaragua',
    'NE': 'Niger',
    'NG': 'Nigeria',
    'NU': 'Niue',
    'NF': 'Norfolk Island',
    'NIR': 'Northern Ireland',
    'MP': 'Norway',
    'OM': 'Oman',
    'PK': 'Pakistan',
    'PW': 'Palau',
    'PA': 'Panama',
    'PG': 'Papua New Guinea',
    'PY': 'Paraguay',
    'PE': 'Peru',
    'PH': 'Philippines',
    'PN': 'Pitcairn',
    'PL': 'Poland',
    'PT': 'Portugal',
    'PR': 'Puerto Rico',
    'QA': 'Qatar',
    'RE': 'Reunion',
    'RO': 'Romania',
    'RU': 'Russian Federation',
    'RW': 'Rwanda',
    'KN': 'Saint Kitts And Nevis',
    'LC': 'Saint Lucia',
    'VC': 'St Vincent/Grenadines',
    'WS': 'Samoa',
    'SM': 'San Marino',
    'ST': 'Sao Tome',
    'SA': 'Saudi Arabia',
    'SN': 'Senegal',
    'SER': 'Serbia',
    'SC': 'Seychelles',
    'SL': 'Sierra Leone',
    'SG': 'Singapore',
    'SK': 'Slovakia',
    'SI': 'Slovenia',
    'SB': 'Solomon Islands',
    'SO': 'Somalia',
    'ZA': 'South Africa',
    'ES': 'Spain',
    'LK': 'Sri Lanka',
    'SH': 'St. Helena',
    'PM': 'St.Pierre',
    'SD': 'Sudan',
    'SR': 'Suriname',
    'SZ': 'Swaziland',
    'SE': 'Sweden',
    'CH': 'Switzerland',
    'SY': 'Syrian Arab Republic',
    'TW': 'Taiwan',
    'TJ': 'Tajikistan',
    'TZ': 'Tanzania',
    'TH': 'Thailand',
    'TG': 'Togo',
    'TK': 'Tokelau',
    'TO': 'Tonga',
    'TT': 'Trinidad And Tobago',
    'TN': 'Tunisia',
    'TR': 'Turkey',
    'TM': 'Turkmenistan',
    'TV': 'Tuvalu',
    'UG': 'Uganda',
    'UA': 'Ukraine',
    'AE': 'United Arab Emirates',
    'UK': 'United Kingdom',
    'UY': 'Uruguay',
    'UZ': 'Uzbekistan',
    'VU': 'Vanuatu',
    'VA': 'Vatican City State',
    'VE': 'Venezuela',
    'VN': 'Viet Nam',
    'VG': 'Virgin Islands (British)',
    'VI': 'Virgin Islands (U.S.)',
    'WL': 'Wales',
    'EH': 'Western Sahara',
    'YE': 'Yemen',
    'YU': 'Yugoslavia',
    'ZM': 'Zambia',
    'ZW': 'Zimbabwe'
}

experience_level = {
    'EXP': 'Experienced(5years and above)',
    'INT': 'Intermediate(2-5years)',
    'JU': 'Junior(0-2years)',
    'INTE': 'Intern'
}

views.py:

def jobsearch(request):
    queryset_list = JobListing.objects.order_by('-list_date')
    # Keywords
    if 'keywords' in request.GET:
        keywords = request.GET['keywords']
        if keywords:
            queryset_list = queryset_list.filter(description__icontains=keywords)
    # city
    if 'city' in request.GET:
        city = request.GET["city"]
        if city:
            queryset_list = queryset_list.filter(city__iexact=city)
        # country
    if 'country' in request.GET:
        q = request.GET["country"]
        if q:
            queryset_list = queryset_list.filter(country__iexact=q)
        # experience
    if 'experience' in request.GET:
        experience = request.GET["experience"]
        if experience:
            queryset_list = queryset_list.filter(experience_level__exact=experience)
        # Price
    if "salary" in request.GET:
        salary = request.GET["salary"]
        if salary:
            queryset_list = queryset_list.filter(salary__exact=salary)
    context = {'salary_choices': salary_choices,
               'experience_level': experience_level, 'country_choices': Country_choices,
               'jobs': queryset_list,
               }
    return render(request, 'joblistings/jobsearch.html', context)

template:

here is the form with the name fields supposedly to tally with the items from the get request:

from django.shortcuts import get_object_or_404, render
from .models import JobListing
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .choices import Country_choices, salary_choices, experience_level
from django.db.models import Q
# Create your views here.
def jobs(request):
    joblisting = JobListing.objects.order_by('-list_date').filter(is_published=True)
    """
    Getting pages from the database
    """
    paginator = Paginator(joblisting, 3)
    page = request.GET.get('page')
    paged_joblistings = paginator.get_page(page)
    context = {
        'jobs': paged_joblistings
    }
    return render(request, 'joblistings/joblistings.html', context)
def job(request, joblisting_id):
    joblist = get_object_or_404(JobListing, pk=joblisting_id)
    context = {
        'job': joblist
    }
    return render(request, 'joblistings/joblisting.html', context)
def jobsearch(request):
    queryset_list = JobListing.objects.order_by('-list_date')
    # Keywords
    if 'keywords' in request.GET:
        keywords = request.GET['keywords']
        if keywords:
            queryset_list = queryset_list.filter(description__icontains=keywords)
    # city
    if 'city' in request.GET:
        city = request.GET["city"]
        if city:
            queryset_list = queryset_list.filter(city__iexact=city)
        # country
    if 'country' in request.GET:
        q = request.GET["country"]
        if q:
            queryset_list = queryset_list.filter(country__iexact=q)
        # experience
    if 'experience' in request.GET:
        experience = request.GET["experience"]
        if experience:
            queryset_list = queryset_list.filter(experience_level__exact=experience)
        # Price
    if "salary" in request.GET:
        salary = request.GET["salary"]
        if salary:
            queryset_list = queryset_list.filter(salary__exact=salary)
    context = {'salary_choices': salary_choices,
               'experience_level': experience_level, 'country_choices': Country_choices,
               'jobs': queryset_list,
               }
    return render(request, 'joblistings/jobsearch.html', context)

below is my models.py file which shows my models:

from django.db import models
from datetime import datetime
from recruiters.models import Recruiter
# Create your models here.
class JobListing(models.Model):
    recruiter = models.ForeignKey(Recruiter, on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=250)
    address = models.CharField(max_length=250)
    city = models.CharField(max_length=250)
    state = models.CharField(max_length=200)
    country = models.CharField(max_length=200, default='')
    zipcode = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    stack = models.CharField(max_length=250)
    experience_level = models.CharField(
        max_length=100,)
    salary = models.IntegerField()
    photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
    optional_photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    optional_photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    optional_photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    is_published = models.BooleanField(default=True)
    list_date = models.DateTimeField(default=datetime.now, blank=True)
    def __str__(self):
        return self.title

my html code:

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Republicans secure vote for Supreme Court replacement

Image copyright Getty Images Image caption Mr Romney, who has clashed fiercely with Mr Trump, was seen as a possible Republican holdout Republicans have secured the numbers needed to ensure that President Donald Trump's Supreme Court nominee will face a confirmation vote in the Senate.Senator Mitt Romney of Utah has given the party the 51…

World must prevent new Cold War, UN chief warns

Image copyright EPA Image caption Mr Guterres gave his address in person - world leaders will be providing pre-recorded messages The world must do all it can to prevent a new Cold War, the UN chief has warned, as he opened a general assembly set to be dominated by US-China tensions and the Covid-19 pandemic.Secretary-General…

‘We’ve reached a perilous turning point’ – PM

Media playback is unsupported on your device Media captionBoris Johnson: "It would be tempting to think the threat had faded"The UK has reached "a perilous turning point", Prime Minister Boris Johnson has said, as he set out a raft of new coronavirus restrictions for England which could last for up to six months.Shop staff will…

Watch: Basketball Champions League – Neptunas Klaipeda v London Lions

LiveLiveQualifier: Neptunas Klaipeda v London LionsBBC SportGot a TV Licence?You need one to watch live TV on any channel or device, and BBC programmes on iPlayer. It’s the law.Find out moreSummaryUse play icon to watch live coverageHalf-time: Neptunas Klaipeda 40-38 London LionsLions face Lithuanian side Neptunas Klaipeda in the first qualifying round of the Fiba…