Issue in form validation

Submitted 4 years, 10 months ago
Ticket #20
Views 588
Language/Framework Django
Priority High
Status Closed

def update(request,id):
    user=Login.objects.get(id=id)
    print(user.user_id)
    print(user.user_pass)
    form=LoginForm(request.POST)
    if form.is_valid():
        print("Form is valid.")
        #form.save()
        return redirect("/show")
    else:
        print(form.errors)
    return render(request,"edit.html",{"qs":user})

form is not valid i dont know whats the matter the errors that i got from form.errors are:

<ul class="errorlist"><li>user_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>user_pass<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

Submitted on Jun 10, 20
add a comment

1 Answer

Acknowledged

Submitted 4 years, 10 months ago

I see you have raised the ticket as HIGH as 2nd time, but please make sure to response at the earliest, try this,

def update(request,id):
    form=LoginForm(request.POST or None)
    if form.is_valid():
        user=Login.objects.get(id=id)
        print("Form is valid.")
        #form.save()
        return redirect("/show")
    else:
        print(form.errors)
    return render(request,"edit.html",{"qs":user})
- scott 4 years, 10 months ago

On it

- moazamrana22 4 years, 10 months ago

still get this error:- <ul class="errorlist"><li>user_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>user_pass<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

- moazamrana22 4 years, 10 months ago

then paste your entire code.. something wrong in your .html file, also share the forms.py code and model.py code

- scott 4 years, 10 months ago

ok

- moazamrana22 4 years, 10 months ago

Models.py

from django.db import models

Create your models here.

class Login(models.Model): user_id=models.CharField(max_length=50) user_pass=models.CharField(max_length=50) class Meta: db_table="login"

- moazamrana22 4 years, 10 months ago

edit.html

<html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> {% load static %} <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/> </head> <body> <form method="POST" class="post-form" action="/crudapp2/update/{{qs.id}}/"> {% csrf_token %}

<div class="container">
    <br>
    <div class="form-group row">
        <label class="col-sm-1 col-form-label"></label>
        <div class="col-sm-4">
        <h3>Update Details</h3>
        </div>
    </div>
    <br>
    <div class="form-group row">
        <label class="col-sm-1 col-form-label">Id:</label>
        <div class="col-sm-4">
            &lt;input type="text" name="uid" id="user_id" required maxlength='50' value={{qs.user_id}} />
        </div>
    </div>
    <div class="form-group row">
        <label class="col-sm-1 col-form-label">Password:</label>
        <div class="col-sm-4">
            <input type="text" name="upass" id="user_pass" required maxlength='50' value={{qs.user_pass}} "/>
        </div>
    </div>
    {% if form.error %}
        {% for field in form %}
            {% for error in field.errors %}
                <div class="alert alert-danger">
                    <strong>{{ error|escape }}</strong>
                </div>
            {% endfor %}
        {% endfor %}
        {% for error in form.non_field_errors %}
            <div class="alert alert-danger">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endif %}
    <div class="form-group row">
        <label class="col-sm-1 col-form-label"></label>
        <div class="col-sm-4">
            <button type="submit" class="btn btn-primary">Update</button> 
        </div>
    </div>
</div>
</form>

</body>

- moazamrana22 4 years, 10 months ago

forms.py

from django import forms from crudapp2.models import Login

class LoginForm(forms.ModelForm): class Meta: model=Login fields="__all__"

- moazamrana22 4 years, 10 months ago

Looks like you have used forms in your views.py but in html you have used direct tag element. It will not work. Let say, you have used forms in views.py then you have to do like this,

def index(request, id):
    form = LoginForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect(/)
    context = {
        'form':form
    }
    return render(request, 'tutorial/post_form.html',context)

And in your html file, you need to render this form just like below, <form method="POST" action='/'> {{ form.as_p }} </form>

If you use direct tag element like input box then you have to use request.POST.get option in your views.py to handle it. i hope you understand this.

- scott 4 years, 10 months ago

Then how will i send the values entered by user in the form to the update function ? Can you give me an example so that i can understand more about this ?

- moazamrana22 4 years, 10 months ago

Example below, in html page <form method="POST"> <input type="text" name="user_id"> <button type="submit" class="btn btn-default">Submit</button> </form>

In views.py

def view(request):
    if request.method == 'POST':
        user_id = request.POST['user_id']
        //Logic here
- scott 4 years, 10 months ago

I was asking about if i use this logic <form> {{form}}</form> in html then?

- moazamrana22 4 years, 10 months ago

that i already shared with you . please read the message carefully. def index(request, id): form = LoginForm(request.POST or None) if form.is_valid(): form.save() return redirect(/) context = { 'form':form } return render(request, 'tutorial/post_form.html',context)

- scott 4 years, 10 months ago

ok got it :)

- moazamrana22 4 years, 10 months ago

Cool.

- scott 4 years, 10 months ago


Latest Blogs