Skip to content
Advertisement

Django forms not saving posts to database

i am trying to create a course from my frontend using django form, everything seems fine but when i hit the create button it just refreshes the page without saving to database or throwing back any error, and that is not what i am expecting.

create-course.html

def create_course(request):
    if request.method == "POST":
        form = CreateCourse(request.POST, request.FILES)
        if form.is_valid():
            new_form = form.save(commit=False)
            new_form.slug = slugify(new_form.course_title)
            new_form.course_creator = request.user
            new_form.save()
            messages.success(request, f'Your Course Was Successfully Created, Now in Review')
            return redirect('dashboard:dashboard')
    else:
        form = CreateCourse()
    context = {
        'form': form
    }
    return render(request, 'dashboard/create_course.html', context)

models.py

class Course(models.Model):
    course_title = models.CharField(max_length=10000)
    slug = models.SlugField(unique=True)
    course_thumbnail = models.ImageField(upload_to=user_directory_path)
    short_description = models.CharField(max_length=10000)
    course_description = models.TextField()
    price = models.IntegerField(default=0)
    discount = models.IntegerField(default=0)
    requirements = models.CharField(max_length=10000)
    course_level = models.CharField(max_length=10000, choices=COURSE_LEVEL)
    # course_rating = models.CharField(max_length=10000, choices=COURSE_RATING)
    # course_rating = models.ForeignKey(CourseRating, on_delete=models.SET_NULL, null=True, blank=True)
    course_language = models.CharField(max_length=10000, choices=COURSE_LANGUAGE)
    # course_category = models.CharField(max_length=10000, choices=COURSE_CATEGORY)
    course_category = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
    course_tag = models.ManyToManyField(Tag)
    course_cost_status = models.CharField(max_length=10000, choices=COURSE_COST_STATUS, default="Free")
    course_intro_video = models.FileField(upload_to=user_directory_path)
    course_intro_video_url = models.URLField(default="https://")
    course_intro_video_embedded = models.URLField(default="https://")
    course_creator = models.ForeignKey(User, on_delete=models.CASCADE)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    course_duration = models.IntegerField(default=0)
    course_publish_status = models.CharField(max_length=10000, choices=COURSE_PUBLISH_STATUS)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    featured = models.BooleanField(default=False)
    digital = models.BooleanField(default=True)
    best_seller = models.BooleanField(default=False)

forms.py

from course.models import Course
from django import forms

class CreateCourse(forms.ModelForm):

    class Meta:
        model = Course
        exclude = ('slug', 'course_cost_status', 'course_creator', 'date_created', 'course_publish_status', 'views', 'likes', 'featured' , 'digital' , 'best_seller')

Advertisement

Answer

this was fixed by just uploading an image, but i wasnt seeing any error because i was not displaying the error in the create page

{{ form.errors }}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement