Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django form widget

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 ###
Comment

Django forms

# 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)
Comment

create forms in django

from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)
Comment

Django form example

from django import forms

# creating a form
class SampleForm(forms.Form):
	name = forms.CharField()
	description = forms.CharField()
Comment

Django Form

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>
Comment

Django Forms

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})
Comment

Django forms

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)
Comment

django forms

from django import forms
        
class FormName(forms.Form):
         # each field would be mapped as an input field in HTML
        field_name = forms.Field(**options)
Comment

PREVIOUS NEXT
Code Example
Python :: read a csv file in pandas 
Python :: # enumerate 
Python :: how to write a comment in python 
Python :: numpy copy a array vertical 
Python :: pytube python 
Python :: python is not clickable at point (434, 682). Other element would receive the click: 
Python :: hide turtle 
Python :: how to declare private attribute in python 
Python :: how to strip whitespace in python 
Python :: iterate over a list python 
Python :: matplotlib pie move percent 
Python :: python boolean operators 
Python :: float64 python 
Python :: python run bat in new cmd window 
Python :: modify a list with for loop function in python 
Python :: pandas series 
Python :: python pickle module 
Python :: python not equal to symbol 
Python :: ubuntu python3 as python 
Python :: python ceiling division 
Python :: check word in list 
Python :: PyPip pygame 
Python :: mysql store numpy array 
Python :: black code formatter 
Python :: get unique words from pandas dataframe 
Python :: how to sort the order in multiple index pandas 
Python :: blur an image in python 
Python :: switch case dictionary python 
Python :: how to concatenate in python 
Python :: fill zeros left python 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =