from django import forms
class ProductForm(forms.ModelForm):
### here date is the field name in the model ###
date = forms.DateTimeField(widget=forms.DateInput(attrs={'class': 'form-control'}))
class Meta:
model = Product
fields = "__all__"
############## or ##############
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = "__all__"
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.fields['date'].widget.attrs["class"] = "form-control"
############## or ##############
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = "__all__"
widgets = {
'date': forms.DateInput(attrs={'class': 'form-control'})
}
### you can use the attrs to style the fields ###
# application/forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(max_length=1000)
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
from django import forms
# creating a form
class SampleForm(forms.Form):
name = forms.CharField()
description = forms.CharField()
blog/views.py
def third(request):
return render(request,'third.html', {'name': request.method})
blog/urls.py
path('', views.index, name='index'),
path('second', views.second, name='index'),
path('hello', views.third, name='index')
blog/template/mypage.html
<form action="/blog/hello" method="POST">
{% csrf_token %}
<input type="submit" value="submit"/>
</form>
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
# Create your forms here.
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
from django import forms
class FormName(forms.Form):
# each field would be mapped as an input field in HTML
field_name = forms.Field(**options)