So I have a form you can fill from the webpage which I want to save to my django database to create a new object when submitted instead of adding it in admin. I've tried it and the system works except for one thing, saving it, the function starts as I've managed to switch pages from that function when clicking submit but the object isnt found in the database after so I guess the saving is the only issue.
Here is my views.py :
def uploadform(request):
if request.method == 'POST':
name = request.POST['name']
details = request.POST['details']
littype = request.POST['littype']
image = request.POST['image']
new_object = PostForm.objects.create(name=name, details=details, littype=littype, image=image)
new_object.save()
return render(request, 'uploadform.html', {})
Here is my models.py (The one to save the form is the "PostForm" class) :
from email.mime import image
from django.db import models
from django.db.models import Model
# Create your models here.
class info(models.Model):
name = models.CharField(max_length=100)
details = models.CharField(max_length=10000)
littype = models.CharField(default="Type of Litterature", blank=True, max_length=100)
image = models.ImageField(default=None, blank=True, upload_to='app/files/covers')
class PostForm(models.Model):
name = models.CharField(max_length=200)
details = models.TextField()
littype = models.TextField()
image = models.FileField(upload_to='app/files/')
def __str__(self):
return self.name
Here is my html page :
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="templates/uploadform.css">
<link type="text/css" rel="stylesheet" href="templates/form.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fascinate&family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body style="height: 1000px;">
<div class="page-title">
<h1 class="page-title-text">Book & Movie & Series Recommendations</h1>
</div>
<div class="page-title-add-button-div">
<a class="page-title-add-button-link" href="/"> Home </a>
</div>
<div>
<form method="POST" action="{% url 'uploadform' %}" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" id="name" name="name" placeholder="Title">
<input type="text" id="details" name="details" placeholder="Summary Details">
<input type="text" id="littype" name="littype" placeholder="Type of Literature">
<input type="file" id="image" name="image" placeholder="Cover Picture">
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
Any ideas? Thanks for your time and answers! Have a good day!
This is a quick answer to anyone wondering how I fixed the issue. First off, I had a few things I had to correct which Adam helped me find in the comments but the main issue was when I was creating a new object to my database instead of saving from the "info" model which looks exactly the same and is found in my database i was saving from the "PostForm" model which cannot be found in the database.
I hope this helps someone and I'll try to stay active and answer any questions if you have any.