How do i add as a formfield in django

I want to have an input field as a button in my template. Just like this.I am manually rendering form fields in my template.So, how do i create a field like that in my form.

Formfield in forms.py class DetailForm(forms.Form): owner=forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'})) views.py def getDetail(request): form=DetailForm() return render(request,'materials/addpage.html',{'form':form}) 

and template,

<div> {{form.owner}} </div> 
7

1 Answer

A minimal example of using buttons as input in Django looks like this:

Template:

<form method="POST"> {% csrf_token %} <input type="submit" name="btn" value="yes"> <input type="submit" name="btn" value="no"> </form> {{ val }} 

Form:

class Fooform(forms.Form): btn = forms.CharField() 

View:

def test_view(request): if request.method == 'POST': form = Fooform(request.POST) if form.is_valid(): val = form.cleaned_data.get("btn") else: form = Fooform() return render(request, 'template.html', locals()) 

Libraries like crispy-forms have button widgets.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like