Data not uploading in Django

40 views
Skip to first unread message

Sona Sivasundari

unread,
Jun 25, 2022, 12:03:24 PM6/25/22
to Django users
Hello!
I've tried uploading the data I'm entering in the webpage to the database, but the data is not entering. Since I am new to django, I don't know what the error is. Can anyone please help me? I am using Mysql database.

My views.py:

from django.views.generic import FormView, TemplateView
from .forms import EmailForm
from django.urls import reverse_lazy
from mailapp.models import Email
from django.shortcuts import render

class MailView(FormView):
    template_name = 'index.html'
    form_class = EmailForm
    success_url = reverse_lazy('mailapp:success')

    def form_valid(self, form):
        # Calls the custom send method
        form.send()
        return super().form_valid(form)
   
    def registered(request):
        if request.method == 'POST':
            if request.POST.get('name') and request.POST.get('email') and request.POST.get('inquiry') and request.POST.get('message'):
                mail = Email()
                mail.name = request.POST.get('name')
                mail.email = request.POST.get('email')
                mail.inquiry = request.POST.get('inquiry')
                mail.message = request.POST.get('message')
                mail.save()
                return render(request, 'success.html')
        else:
            return render(request, 'success.html')
       
class MailSuccessView(TemplateView):
    template_name = 'success.html'

models.py

from django.db import models
from django.contrib.auth.models import User

class Email(models.Model):

    name = models.CharField(max_length=120)
    email = models.EmailField()
    inquiry = models.CharField(max_length=70)
    message = models.CharField(max_length=300)
   
    def __str__(self):
        return self.name

admin.py

from django.contrib import admin
from .models import Email
admin.site.register(Email)


Thank you in advance!!

K Wong

unread,
Jul 4, 2022, 6:04:42 PM7/4/22
to Django users
FormView does not save your data in form_valid it just does the success redirect. You need to either save it yourself or use CreateView rather than FormView.

-k

Reply all
Reply to author
Forward
0 new messages