Search code examples
pythonmysqldjangophpmyadminxampp

Django - Form is getting submitted but nothing saves in database


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?


Solution

  • 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')