Tadej Tilinger
Tadej Tilinger

Reputation: 1

How can I get cleaned_data from html form?

I have django app with class based view and form written in html:

<form method="post" action="{% url 'personal-account' %}">
            {% csrf_token %}
            <div class="page slide-page">
              <div class="field">
                <input type="text" placeholder="First name" class="input_1" name="first_name" size="1" value="{{ data.first_name }}">
                <input type="text" placeholder="Last Name" class="input_2" name="last_name" size="1" value="{{ data.last_name }}">
              </div>
              <div class="middle">
                <input type="text" placeholder="Username" class="input_3" name="username" size="1" value="{{ data.username }}">
                <input type="text" placeholder="Email" class="input_6" name="email" size="1" value="{{ data.email }}">
              </div>
              <div class="last">
                <input type="password" placeholder="Password" class="input_4" name="password" size="1" value="{{ data.password }}">
                <input type="password" placeholder="Confirm" class="input_5" name="confirm_password" size="1"  value="{{ data.confirm_password }}">
              </div>
              <div class="field">
                <button type="submit" class="submit-btn">Submit</button>
              </div>
            </div>
          </form>

View

class PersonalSignup(View):
    def post(self, request):
        
        return render(request, 'authentication/personal_signup.html')
    def get(self, request):
        return render(request, 'authentication/personal_signup.html')

Now I want to get all values from inputs(first_name, last_name...) with cleaned_data.

Upvotes: 0

Views: 365

Answers (2)

JimmyFl0
JimmyFl0

Reputation: 116

In your views, you can grab the information from the request.POST(returns a dictionary), like this:

if request.method == 'POST':
    first_name = request.POST['first_name']

Upvotes: 0

Mohammad Reza Etaati
Mohammad Reza Etaati

Reputation: 21

First, you need to create a form in forms.py. like this:

class PersonalSignupForm(forms.Form):
    first_name=forms.CharField(max_length=255)
    last_name=forms.CharField(max_length=255)
    username=forms.CharField(max_length=255)
    password=forms.CharField(max_length=255)
    confirm_password=forms.CharField(max_length=255)

And then in viwe do like this:

from .forms impory PersonalSignupForm

class PersonalSignup(View):

     def get(self, request):
         form=PersonalSignupForm()
             return render(request, 'authentication/personal_signup.html',context={'form':form})

     def post(self, request):
        form=PersonalSignupForm(request.POST)
        if form.is_valid():
           first_name=form.cleaned_data.get('first_name')
           last_name=form.cleaned_data.get('last_name')
           username=form.cleaned_data.get('username')
           password=form.cleaned_data.get('password')
           return render(request, 'authentication/personal_signup.html')

Upvotes: 0

Related Questions