提交后将数据保留在表格中

时间:2019-11-09 20:54:03

标签: python django

我正在通过Django中的两个字段来实现搜索。

我想在搜索后保留输入数据。

例如,我输入“ C ++”并选择“ IT”

enter image description here

然后我收到了默认值

enter image description here

我尝试解析 request 变量-例如数据= request.POST.copy() 但没有取得结果。这个问题的原因是什么?

如何解决此问题?

这是我的代码: 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><a href="{% url 'vacancy_detail' pk=vacancy.pk %}">{{vacancy.title}}</a></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 %}

1 个答案:

答案 0 :(得分:1)

在搜索结果模板中,您应该使用{{query}}和{{query2}},并使用一些逻辑来填充文本和下拉框。