Menu
CoddyTech

Tìm kiếm việc làm

Thuộc phần View dựa trên lớp trong hành trình Django của Coddy. Bài học 18 trên 18.

Tính năng cuối cùng là ô tìm kiếm trên trang việc làm. Khách truy cập nhập developer, nhấn Tìm kiếm và chỉ thấy những việc làm có từ đó trong tiêu đề.

Biểu mẫu tìm kiếm dùng GET, nên từ khóa sẽ xuất hiện trong URL dưới dạng ?q=developer, nhờ đó liên kết hoặc dấu trang có thể lưu lại từ khóa này. View đọc từ khóa giống như cách đọc ?city=, trong get_queryset(). Mọi người có thể nhập chữ hoa hoặc chữ thường, vì vậy hãy dùng kiểu tra cứu không phân biệt chữ hoa chữ thường.

Trong template, yêu cầu hiện tại là request, vì vậy {{ request.GET.q }} là từ khóa tìm kiếm: đặt từ khóa này vào giá trị của ô tìm kiếm.

Các bộ lọc được áp dụng nối tiếp: lọc theo thành phố nếu có, rồi lọc theo từ khóa nếu có. Các liên kết phân trang đã dùng {% querystring %}, nên từ khóa tìm kiếm vẫn được giữ lại khi chuyển trang.

challenge icon

Thử thách

Trung bình

Thêm ô tìm kiếm việc làm.

  1. Trong jobs/views.py, ở JobListView.get_queryset(), đọc q từ request.GET theo cách tương tự như city. Khi có giá trị, chỉ giữ lại những việc làm có tiêu đề chứa từ đó, không phân biệt chữ hoa chữ thường.
  2. Trong job_list.html, bên dưới <h1>, thêm biểu mẫu GET: một ô có tên q, giá trị là request.GET.q, và nút Search.

Tìm kiếm developer: bạn thấy 4 việc làm và ô vẫn giữ từ đó.

Tự làm thử

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.urls import reverse_lazy
from django.views.generic import CreateView, DeleteView, DetailView, ListView, TemplateView, UpdateView

from .models import Job


class HomeView(TemplateView):
    template_name = "jobs/home.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["count"] = Job.objects.count()
        context["latest"] = Job.objects.order_by("-posted").first()
        return context


class AboutView(TemplateView):
    template_name = "jobs/about.html"


class JobListView(ListView):
    model = Job
    context_object_name = "jobs"
    ordering = ["-posted"]
    paginate_by = 5

    def get_queryset(self):
        jobs = super().get_queryset()
        city = self.request.GET.get("city")
        if city:
            jobs = jobs.filter(city=city)
        return jobs


class JobDetailView(DetailView):
    model = Job


class JobCreateView(LoginRequiredMixin, CreateView):
    model = Job
    fields = ["title", "company", "city", "salary", "description"]

    def form_valid(self, form):
        form.instance.posted_by = self.request.user
        return super().form_valid(form)


class JobUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Job
    fields = ["title", "company", "city", "salary", "description"]

    def test_func(self):
        return self.get_object().posted_by == self.request.user


class JobDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Job
    success_url = reverse_lazy("jobs:list")

    def test_func(self):
        return self.get_object().posted_by == self.request.user


class MyJobsView(LoginRequiredMixin, ListView):
    template_name = "jobs/my_jobs.html"
    context_object_name = "jobs"

    def get_queryset(self):
        return Job.objects.filter(posted_by=self.request.user).order_by("-posted")
quiz iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

Tất cả bài học trong View dựa trên lớp

Tự luyện tập: Playground Django