0

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.

2 Answers2

0

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')
  • It still doesn't work. I want to keep my html form. Or is there a way I can style form from forms.py? I insert your code in my view but form is never valid... – Tadej Tilinger Aug 20 '22 at 05:58
0

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']
JimmyFl0
  • 96
  • 1
  • 8
  • I used to do that, but I read that cleaned_data is better approach – Tadej Tilinger Aug 20 '22 at 09:26
  • Could you send me the link to why that is? Thanks man. – JimmyFl0 Aug 20 '22 at 13:32
  • @JimmyFI0 https://stackoverflow.com/questions/10877580/should-i-use-the-text-from-request-post-or-form-cleaned-data#:~:text=You%20never%20should%20use%20request,data%20safe%20to%20deal%20with. – Tadej Tilinger Aug 23 '22 at 10:39
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jasie Aug 24 '22 at 10:26