class YourForm(ModelForm):
class Meta:
model = YourModel
fields = ['pub_date', 'headline', 'content', 'reporter']
# 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.forms import ModelForm
>>> from myapp.models import Article
# Create the form class.
>>> class ArticleForm(ModelForm):
... class Meta:
... model = Article
... fields = ['pub_date', 'headline', 'content', 'reporter']
# Creating a form to add an article.
>>> form = ArticleForm()
# Creating a form to change an existing article.
>>> article = Article.objects.get(pk=1)
>>> form = ArticleForm(instance=article)
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)