class UserForm(forms.Form): username = forms.CharField(max_length=100, label=_("Username:"******"Password:"******"Type Again"), max_length=100, widget=forms.widgets.PasswordInput) email = forms.EmailField() time_zone = MyTimeZoneField() def clean(self): cleaned_data = self.cleaned_data pass1 = cleaned_data.get("password1", None) pass2 = cleaned_data.get("password2", None) if pass1 != pass2: self._errors["password2"]=_("Passwords don't match") if "password2" in cleaned_data: del cleaned_data["password2"] return cleaned_data
class OrganizationMemberForm(UserCreationForm): orgcode = forms.IntField(label="User Name", max_lenght=200) subscriberid = forms.CharField(label="Password", max_lenght=200) membershipstartdate = forms.CharField(label="Fist Name", max_lenght=200) membershipenddate = forms.CharField(label="Last Name", max_lenght=200) country = forms.CharField(label="Middle Name", max_lenght=200) citizenship= forms.CharField(label="Gender", max_lenght=100) isdelegate = forms.CharField(label="Address", max_lenght=200) class Meta: model = Myapps fields = ("orgcode","subscriberid","membershipstartdate","membershipenddate","country","citizenship","isdelegate")
class AuthorForm(forms.Model): name = forms.CharField(max_length=20) title = forms.CharField(max_length=3, widget=forms.Select(choices=TITLE_CHOICE)) birth_date = forms.DateField(required=False)
class BookForm(forms.Model): name = forms.CharField(max_length=30) authors = forms.ModelMultipleChoiceField(queryset=Author.object.all())
class Busca_Form(forms.Form): codigo = forms.CharField(label='Codigo', max_length=100)
url(r'^formpage/',views.form_name_view, name='form_name') +add in html <div class="container"> <h1>Fill out the Form!</h1> <form method="post"> {{ form.as_p }} //align the inputs in paragraph form {% csrf_token %} //security certificate <input type="submit" class="btn btn-primary" name="" value="Submit"> </form> </div> #form validation from django.core import validators Bot catcher botcatcher = forms.CharField(required=False, widget=forms.HiddenInput, validators=[validators.MaxLengthValidator(0)]) #How to recreate the db python manage.py flush #create a template tagging #Template tagging in urls.py of app app_name = 'basic_app' #this is to tell the template which app its from urlpatterns = [
class FormName(forms.Form): FirstName = forms.CharField(max_length=30, required=True) LastName = forms.CharField(max_length=20, required=True) email = forms.EmailField(max_length=60, required=True) pas = forms.CharField(widget=PasswordInput(), required=False) confPas = forms.CharField(widget=PasswordInput(), required=False)
class ContactForm(forms.Form): name = forms.CharField() message = forms.CharField(widget=forms.Textarea)
class formname(forms.Form): name = forms.CharField(validators=[check_for_z])#custom validators value checking
class formname(forms.Form): name = forms.CharField()#fields
<form action="" method="POST"> {{ form.as_p }}#templates tag of the django form {% csrf_token %}#token for security <input type="submit" class="btn btn-primary" value="Submit"> </form> </div> #forms.py this is the django form from django import forms class formname(forms.Form): name = forms.CharField()#fields email = forms.EmailField() varfiy_email = forms.EmailField(label="enter your email again") text = forms.CharField(widget = forms.Textarea) #views.py from basicapp import forms def index(request):#home fun return render (request,"basicapp/index.html") def form_fun(request):#form function form = forms.formname() if request.method == "POST":#security check
class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField()
class ExampleForm(forms.Form): name = forms.CharField() password = forms.CharField(widget=forms.PasswordInput()) class Meta: fields = ('name', 'password')
class AddOrdemFabricacao(forms.Form): nome = forms.CharField("Nome do cara")
class UserForm(forms.ModelForm): password = forms.CharField(widget=PasswordInput()) class Meta(): model = user fields = ('username','email','password')
# ==================================================================== validating the forms 1 = داخل خود فرم که در کلاس فرم نوشتیم تمامی متدهایی که با clean_ شروع میشوند و بعد از آنها هر اسمی میاد به عنوان ولیدیتور به کار می روند def clean_bot_catch(self): if len(self.cleaned_data['botcatcher'])>0: then raie forms.validationError ( ' هز متنی دز اینجا میشه نوشت') forms. یعنی از کلاس اصلی در حال استفاده هستیم ############################################################################# bot = forms.CharField(required=False,widget =forms.HiddenInput,validators=[ validators.MaxLengthValidator(0) ]) # def clean_bot(self): # bot = self.cleaned_data['bot'] # if len(bot)>0 : # raise forms.ValidationError(' hi man error ') # return bot # این راه حل برای من raise error نداد اصلن # ولی راه دوم با vlidators هست # البته توی راه دوم هم ارورر فقط توی کنسول میده 2= یک راه دیگه برای ولید کردن دیتای توی سای هم هست یک تابع بسازیم با یک دستور رایز ارور وسپس به ولیدیتو ر پاش بدیم