# my_app/forms.py
from django import forms
import datetime
class UserQueryForm(forms.Form):
date = forms.DateField(initial=datetime.date.today) # need help
username = forms.CharField(label='Username:', max_length=15)
mobilenos = forms.CharField(max_length=10, label='Mobile Nos')
<!--my_app/templates/my_app/form.html-->
<form action="" method="POST">
{% csrf_token %}
<h1>Date:</h1>
{{ form.date }}
{{ form.username.label}}
{{ form.username }}
<br />
{{ form.mobilenos.label}}
{{ form.mobilenos}}
<br />
<input type="submit" value="Submit">
</form>
How does your view looks like?
Etienne
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a5a9a34f-f21e-48fc-bc3c-7ec25c73b017%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
-- Etienne Robillard tka...@yandex.com https://www.isotopesoftware.ca/
# my_app/views.py
from django.shortcuts import render, get_object_or_404
# Create your views here.
from .models import User
from .forms import UserQueryForm
def home(request): # home page
a_list = User.objects.filter()
for item in a_list:
context = {'username': item.username,'contact_info': item.mobile_nos, 'req_date': item.request_date}
print(item.username)
return render(request, 'userinfo/information.html', context)
def query_form(request): # the concerned view
if request.method == 'POST':
form = UserQueryForm(request.POST)
if form.is_valid():
get_date = request.POST['date']
get_username = request.POST['username']
get_mobile_nos = request.POST['mobilenos']
context ={'username':get_username, 'contact_info': get_mobile_nos, 'req_date': get_datetime}
return render(request, 'userinfo/information.html', context)
else:
form = UserQueryForm()
return render(request, 'userinfo/form.html',{ 'form': form })
i think you need to replace:
form = UserQueryForm()
with
form = UserQueryForm(initial={'date': date})
See: https://docs.djangoproject.com/en/2.0/ref/forms/api/
Etienne
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e4f14993-12fc-4314-b456-155dbd876772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/28b95b8f-1142-4acb-8c69-15cf0d607359%40googlegroups.com.