Binding hand written HTML(form) or forms that are not created through djnago forms to django forms validation errors

51 views
Skip to first unread message

Mahesh Thipparthi

unread,
Mar 10, 2015, 7:10:48 AM3/10/15
to django...@googlegroups.com
Hi All,
Kindly help me with following questions.
         
Use case is this:
1. i have created a form manually ( not generated through django forms)
2. I am calling a Django view function upon submission.
3. validating the field through django form as i ensured that form names are same.

question is
1. How to pass on validation errors from Djnago form to non django forms?
Please suggest better ways to do this.

Note. I see one way is to jsonify the errors and reconstruct the DOM


Let me take an example.
I do not want to use Djnago generated form hence i have written my own login page:

<div class="col-md-4 col-md-offset-4" id="id_loginform">

    <h3>Login to Application</h3>

    <form class="form-horizontal" method="POST" action={% url "auth_login" %}  > {% csrf_token %}

        <div class="form-group">

            <div class="input-group">

              <div class="input-group-addon" id="id_username" name="username" >User ID  </div>

              <input type="text" class="form-control" id="username" name ="username" placeholder="User name">

            </div>

             <div class="input-group">

              <div class="input-group-addon">Password </div>

              <input type="password" class="form-control" name="password" id="password" placeholder="Password">

              </div>

            </div>

              <div class="col-md-offset-4 col-md-8">

                <button type="submit" class="btn btn-default">Sign in</button>

            </div>

        </div>

      </form>

</div> 
      
action is passed to view where i am trying to do this.

 def login_view(request):

    if request.POST:

        form = LoginForm(request.POST or None)

        if form.is_valid():

            username = form.cleaned_data['username']

            password = form.cleaned_data['password']

            user = authenticate(username=username, password=password)

            login(request, user)

            return HttpResponseRedirect(reverse("menu"))

    else:

        form = LoginForm()

      context = {"form": form,}

    return render(request, "login.html", context)  

when ther are form eeors invalid user id or pasword......waht is the way to pass valiadtion errors to "UNBOUND Form".
I personally see only way to return jason response and process it on unbound form?....is there anyway djnago can directly pass validation errors to forms that are not created by djnago?


Vijay Khemlani

unread,
Mar 10, 2015, 7:57:05 AM3/10/15
to django...@googlegroups.com
Unbound forms have no validation errors as they haven't been filled yet by the user.

I think you are trying to solve the wrong problem. Why are you creating your form manually in the first place? Your HTML markup is easy to do with Django forms and packages suck as crispy forms

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6f74d6f9-1c29-4ce7-b2df-4f74b8fefb05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scot Hacker

unread,
Mar 10, 2015, 10:08:35 AM3/10/15
to django...@googlegroups.com


On Tuesday, March 10, 2015 at 4:10:48 AM UTC-7, Mahesh Thipparthi wrote:
Hi All,
Kindly help me with following questions.
         
Use case is this:
1. i have created a form manually ( not generated through django forms)
2. I am calling a Django view function upon submission.
3. validating the field through django form as i ensured that form names are same.

From your example code, I'm guessing the reason why you don't want to use a Django form is because you need to have  Bootstrap classes  on the form elements. A much better way to skin this cat is to use a standard Django form and then render it out with django-bootstrap-forms: https://django-bootstrap-form.readthedocs.org/en/latest/

./s
Reply all
Reply to author
Forward
0 new messages