Issue
I have created models.py as well as views.py, wanted to submit data through the front page and those need to store in the database but failed to sent data. when I click to submit after fillup the form it goes to the else block which is mentioned in views.py but not checking if statement. help appricated.
models.py
from django.db import models
# Create your models here.
# creating modeles here and storing in database acts as mysql
class Contact(models.Model):
usn = models.CharField(max_length=50, primary_key=True)
name = models.CharField(max_length=50, default="")
sem = models.CharField(max_length=50, default="")
phone = models.CharField(max_length=50, default="")
email = models.CharField(max_length=50, default="")
def __str__(self):
return self.name
views.py
from django.shortcuts import render, redirect, HttpResponse
from django.conf import settings
from .models import Contact
# Create your views here.
def index(request):
if request.method=="POST":
usn = request.POST.get('usn','')
name = request.POST.get('name','')
sem = request.POST.get('sem','')
phone = request.POST.get('phone','')
email = request.POST.get('email', '')
if usn and name and sem and phone and email:
contact = Contact(usn = usn, name = name, sem = sem, phone = phone, email = email)
contact.save()
else:
return HttpResponse("Enter all details")
return render (request, 'index.html')
index.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>formpage</title>
</head>
<body>
<div class="container mt-3">
<div class="alert alert-warning" role="alert">
MINI PROJECT
</div>
<br>
<!--THE FORM STARTS HERE AND ITS IMPORTANT -->
<form method="POST" action="/">
{% csrf_token %}
<div class="form-group">
<div class="form-group">
<label>USN</label>
<input type="text" class="form-control" id="usn" name="usn" placeholder="ENTER USN" required>
</div>
<div class="form-group">
<label>NAME</label>
<input type="text" class="form-control" id="name" name="name" placeholder="ENTER NAME">
</div>
<div class="form-group">
<label>SEM</label>
<input type="text" class="form-control" id="sem" name="sem" placeholder="ENTER SEM">
</div>
<div class="form-group">
<label>PHONE</label>
<input type="number" class="form-control" id="phone" name="phone" placeholder="ENTER PHONE">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" id="email" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<button type="submit" class="btn btn-danger">Submit</button>
</form>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
Solution
Check if all names are match in your index.html
If all are match, Then:
Print all of the following to see which field is giving you the problem
print('1', request.POST[usn])
print('2', request.POST[sem])
....
Answered By - Hello World
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.