简体   繁体   English

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

[英]Keep data in the form after submit

I am implementing search by two fields in form in Django.我正在通过 Django 中的两个字段实现搜索。

I want to keep input data after search.我想在搜索后保留输入数据。

For example I input "C++" and chose "IT"例如我输入“C++”并选择“IT”

在此处输入图像描述

then I received Default values然后我收到了默认值

在此处输入图像描述

I tried to parse request variable --- eg data = request.POST.copy() but did not achieved result.我试图解析请求变量 --- 例如 data = request.POST.copy() 但没有得到结果。 What is the reason of this problem?这个问题的原因是什么?

How can I solve this problem?我怎么解决这个问题?

This is my code: models.py这是我的代码: 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网址.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视图.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 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 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.在您的搜索结果模板中,您应该使用 {{ query }} 和 {{ query2 }} 以及一些逻辑来填充文本和下拉框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM