Keep Django selected option (form/select) after refresh

Viewed 1620

Select option in my Django app is not saved after refresh. Below is my code:

<! –– jobs.html ––>
{% block content %}
<form id="job-main-form" method="get" action="#" class="job-main-form">
    <select form="job-main-form" name="specialization" class="form-control" id="specialization">
        <option selected="selected" value="all">Všechny obory</option>
        {% for specialization in specializations %}
        <option value="{{ specialization|lower }}">{{ specialization }}</option>
        {% endfor %}
    </select>
    <button type="submit" class="btn btn-outline-white-primary job-main-form__button">
        <i class="fa fa-search"></i>
    </button>
</form>
{% endblock %}
# views.py
from django.views import generic

from .models import Job
class JobListView(generic.ListView):
    model = Job
    template_name = 'jobsportal/jobs.html'
    context_object_name = 'jobs'
    paginate_by = 5
    specializations_list = [
        'Alergologie',
        'Anesteziologie',
    ]

    def get_queryset(self):
        q = self.model.objects.all()
        if self.request.GET.get('specialization') and self.request.GET.get('specialization') != "all":
            q = q.filter(specialization__icontains=self.request.GET['specialization'])
        return q
# models.py
from django.db import models

# Create your models here.


class Hospital(models.Model):
    PRAHA = 'PHA'
    STREDOCESKY = 'STC'
    REGIONS = [
        (PRAHA, 'Hlavní město Praha'),
        (STREDOCESKY, 'Středočeský kraj'),
    ]
    key = models.CharField(max_length=20)
    name = models.CharField(max_length=200)
    region = models.CharField(max_length=50, choices=REGIONS)
    city = models.CharField(max_length=50)
    website = models.CharField(max_length=300, default="")
    logo = models.CharField(max_length=300)
    objects = models.Manager()

    def __str__(self):
        return self.name


class Job(models.Model):
    title = models.CharField(max_length=300)
    specialization = models.TextField()
    detail = models.CharField(max_length=300, default="")
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE)
    objects = models.Manager()

    def __str__(self):
        return self.title
# urls.py
from django.urls import path

from . import views

app_name = 'jobsportal'
urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),
    path('jobs/', views.JobListView.as_view(), name='jobs'),
]

So I have default option in select = "all". When I change it and click the button to filter, I get the correct filtered data, but the select option is changed back to "all". So the user won't see the option, which he chosen, which is not expected behavior.

Thanks for your help.

1 Answers

You will want to pass the "selected" value back to the html, so you can add the selected flag to the correct <option>.

<option value="{{ specialization|lower }}" 
    {% if specialization.selected %} selected {% endif %} >
    {{ specialization }}
</option>
Related