Reputation: 165
I want to take data from user and save it to database by using django. Have tried to solve it. But I am not able to solve this problem and didn't find any working solution on internet.
My views.py file is:-
from django.shortcuts import render, HttpResponse
from home.models import addBooks
def index(request):
return render(request, "index.html")
def checkBooks(request):
return render(request, "checkBooks.html")
def contactUs(request):
return render(request, "contactUs.html")
def addBooks(request):
name = request.POST.get('name', False)
email = request.POST.get('email', False)
bookName = request.POST.get('bookName', False)
authorName = request.POST.get('authorName', False)
desc = request.POST.get('desc', False)
book = addBooks(name=name, email=email,bookName = bookName, authorName = authorName, desc = desc)
book.save()
return render(request, "addBooks.html")
And my models.py file is:-
from django.db import models
class addBooks(models.Model):
name = models.CharField(max_length=122)
email = models.CharField(max_length=122)
bookName = models.CharField(max_length=122)
authorName = models.CharField(max_length = 122)
desc = models.TextField()
This is addBooks.html(Here I am getting the data from user by using from):-
<form method="POST" action = "/addBooks">
{% csrf_token %}
<div class = "container">
<h1 class = "text-center"> Add Books </h1>
<div class="mb-3">
<label class="form-label">Your Name</label>
<input type="text" name = "name" class="form-control">
<div class="form-text"></div>
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email" name = "email" class="form-control">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
</div>
<div class="mb-3">
<label class="form-label">Book Name</label>
<input type="text" name = "bookName" class="form-control">
<div class="form-text"></div>
</div>
<div class="mb-3">
<label class="form-label">Author Name</label>
<input type="text" name = "authorName" class="form-control">
<div class="form-text"></div>
</div>
<div class="mb-3">
<label class="form-label">Description Of The Book</label>
<input type="text" name = "desc" class="form-control">
<div class="form-text"></div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Here is the full error traceback:-
Upvotes: 0
Views: 1216
Reputation: 5854
The issue is with your function name it is the same as the model name.
addBooks is your model name also your function name. both can't be the same
def addBooks_add(request): #change function name
name = request.POST.get('name', False)
email = request.POST.get('email', False)
bookName = request.POST.get('bookName', False)
authorName = request.POST.get('authorName', False)
desc = request.POST.get('desc', False)
book = addBooks(name=name, email=email,bookName = bookName, authorName = authorName, desc = desc)
book.save()
return render(request, "addBooks.html")
or try this
from home.models import addBooks as addBooksCreate
def addBooks(request):
name = request.POST.get('name', False)
email = request.POST.get('email', False)
bookName = request.POST.get('bookName', False)
authorName = request.POST.get('authorName', False)
desc = request.POST.get('desc', False)
book = addBooksCreate(name=name, email=email,bookName = bookName, authorName = authorName, desc = desc)
book.save()
Upvotes: 1
Reputation: 21
In addBooks function, you are taking value of name in the wrong way You have to try this
name = request.POST['name']
And Do this for all input fields.
Upvotes: -1