Django,如何将默认值设置为输入日期的今天(不使用表单)?

时间:2021-03-11 17:11:57

I'm using django to make website. I succeded remaining the input date value after submit passing the value. But I don't know how I can remain the selected value after submit. (I'm not using form)

我用django做网站。我成功地在提交后保留了输入日期值。但是我不知道如何在提交后保持选中的值。(我不是使用形式)

Also, I want to know how I set the default value to today of input type="date"!

另外,我想知道如何将输入类型="date"的默认值设置为today !

Here's my page. I want to keep remain the selected value after submit( after submit, the page return this page again)

这是我的页面。我想在提交后保留所选的值(提交后,页面再次返回此页面)

Django,如何将默认值设置为输入日期的今天(不使用表单)?

sales.management.html

sales.management.html

<form id="sales_search" action="{% url 'management:sales_search' %}" method="GET">
    <select title="team_choice" name="team_choice" class="select" id="team_choice">
        <option name='FC' value="FC" {% if team =='FC' %} selected {% endif %}>FC</option>
        <option name='Fitness' value="Fitness" {% if team =='Fitness' %} selected {% endif %}>fitness</option>
        <option name='Pilates' value="Pilates" {% if team =='Pilates' %} selected {% endif %}>pilates</option>
        <option name='All' value="All" {% if team =='All' %} selected {% endif %}>all</option>
    </select>
    <span>Start Day: <input type="date" class="startdate" name="startdate" value="{{ startdate }}" ></span>
    ~<span>End Day: <input type="date" class="enddate"  name="enddate"  value="{{ enddate }}" ></span>
    <button type="submit" class="btn btn-info" value="search" >search</button>
</form>

I tried {% if team =='FC' %} selected {% endif %} in select boxes.

我在选择框中尝试了{% if team ='FC' %}选择{% endif %}。

But, it gets error Could not parse the remainder: '=='FC'' from '=='FC''.

但是,它得到错误无法解析余数:'= 'FC' from '= 'FC'。

views.py

views.py

def sales_search(request):
     team_choice = request.GET.get('team_choice','')
     startdate = request.GET.get('startdate','') 
     enddate = request.GET.get('enddate','') 
     #Todo ( it's a long)

     context = {
         .... ,
        'startdate' : startdate,
        'enddate' : enddate,
        'team':team_choice, }

    return render(request, 'management/sales_management.html', context)

How can I keep the selected value after submit and How can I set the default value to today of input date?

如何在提交后保留选定值,如何将默认值设置为输入日期的今天?

Any help will be very helpful to me, thanks!

任何帮助都会对我很有帮助,谢谢!

2 个解决方案

#1


1  

in your models you can add DateField(default=date.today) references:https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField

在您的模型中,您可以添加DateField(default=date.today)引用:https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField

#2


0  

you need to put a space between == and 'FC' in your template.

您需要在模板中在==和'FC'之间放置一个空格。

And to set the default date to today in your views file add

并在视图文件add中设置到今天的默认日期

import datetime 

and in your context write:

在你的背景下写:

context = {
     .... ,
    'startdate' : startdate if startdate else datetime.date.today(),
    'enddate' : enddate if enddate else datetime.date.today(),
    .....
    }

#1


1  

in your models you can add DateField(default=date.today) references:https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField

在您的模型中,您可以添加DateField(default=date.today)引用:https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField

#2


0  

you need to put a space between == and 'FC' in your template.

您需要在模板中在==和'FC'之间放置一个空格。

And to set the default date to today in your views file add

并在视图文件add中设置到今天的默认日期

import datetime 

and in your context write:

在你的背景下写:

context = {
     .... ,
    'startdate' : startdate if startdate else datetime.date.today(),
    'enddate' : enddate if enddate else datetime.date.today(),
    .....
    }