Julia
Julia

Reputation: 325

Django , How can I set the default value to today of input date(not using forms)?

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)

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

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

my page

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.

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

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!

Upvotes: 3

Views: 2631

Answers (2)

Mauricio Cortazar
Mauricio Cortazar

Reputation: 4213

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

Upvotes: 1

badiya
badiya

Reputation: 2257

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

And to set the default date to today in your views file 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(),
    .....
    }

Upvotes: 0

Related Questions