skip to Main Content

My form is getting submitted by in phpmyadmin nothing get saved please help.

Models.py

from django.db import models
class Customers(models.Model):
    first_name=models.CharField(max_length=100)
   .
.
.
    address=models.CharField(max_length=100)
    mar_stat=models.CharField(max_length=100)

    class Meta:
        db_table="customers"

Views.py


def customerreg(request):
    if request.method=='POST':
        if request.POST.get('first_name') and request.POST.get('last_name') and request.POST.get('gender') and request.POST.get('dob') and request.POST.get('email') and request.POST.get('phno') and request.POST.get('designation') and request.POST.get('address') and request.POST.get('mar_stat'):
            saverecord = Customers()
            saverecord.first_name=request.POST.get('first_name')
            .
            saverecord.mar_stat=request.POST.get('mar_stat')
            saverecord.save()
            messages.success(request, "New User Registration Saved Successfully. You may close the window!")
            return render(request,'index.html')
    else:
            return render(request,'index.html')


Index.html

    <form method='POST'>
        {% csrf_token %}
    <table border="7">
        <tr>
            <td>First Name</td>
            <td><input type="text" placeholder="Enter First Name" name="first_name" required/></td>
       .
.
.
.


                </select>
            </td>
        </tr>
    </table>
    <hr>
    <input type="submit" value="Submit"/>
    {% if messages %}
    {% for result in messages %}
    <b style="color: green;">{{result}}</b>
    {% endfor %}
    {% endif %}
</form>
</body>
</html>

I have tried everything and im mostly sure that my indentation is right still it throws an error. What to do?

2

Answers


  1. I think you are doing wrong in views.

    Instead of this:

    def customerreg(request):
        if request.method=='POST':
            if request.POST.get('first_name') and request.POST.get('last_name') and request.POST.get('gender') and request.POST.get('dob') and request.POST.get('email') and request.POST.get('phno') and request.POST.get('designation') and request.POST.get('address') and request.POST.get('mar_stat'):
                saverecord = Customers()
                saverecord.first_name=request.POST.get('first_name')
                .
                saverecord.mar_stat=request.POST.get('mar_stat')
                saverecord.save()
                messages.success(request, "New User Registration Saved Successfully. You may close the window!")
                return render(request,'index.html')
        else:
                return render(request,'index.html')
    

    Do this way:

    def customerreg(request):
        if request.method=='POST':
            if request.POST.get('first_name') and request.POST.get('last_name') and request.POST.get('gender') and request.POST.get('dob') and request.POST.get('email') and request.POST.get('phno') and request.POST.get('designation') and request.POST.get('address') and request.POST.get('mar_stat'):
                saverecord = Customers()
                saverecord.first_name=request.POST.get('first_name')
                .
                saverecord.mar_stat=request.POST.get('mar_stat')
                saverecord.save()
                messages.success(request, "New User Registration Saved Successfully. You may close the window!")
                return redirect('/login/')  #made changes here
        return render(request,'index.html')
    
    Login or Signup to reply.
  2. for Add data You need to write code like this…

    def customerreg(request):
        if request.method=='POST':
            f_name= request.POST.get('first_name') 
            l_name= request.POST.get('last_name')
            gender=request.POST.get('gender') 
            dob=request.POST.get('dob') 
            email=request.POST.get('email')
            phno=request.POST.get('phno') 
            designation=request.POST.get('designation')
            address=request.POST.get('address') 
            mar_stat=request.POST.get('mar_stat')
            Customers(
                first_name=f_name,
                last_name=l_name,
                gender=gender,
                dob=dob,
                email=email,
                phno=phno,
                designation=designation,
                address=address,
                mar_stat=mar_stat
            ).save()
                
            messages.success(request, "New User Registration Saved Successfully. You may close the window!")
            return redirect('/')
        else:
            return render(request,'index.html')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search