Django 表单不将帖子保存到数据库
Django forms not saving posts to database
我正在尝试使用 Django 表单从我的前端创建一个课程,一切似乎都很好,但是当我点击创建按钮时,它只是刷新页面而不保存到数据库或返回任何错误,这不是我想要的期待中。
创建-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')
这是通过上传图片解决的,但我没有看到任何错误,因为我没有在创建页面中显示错误
{{ form.errors }}
我正在尝试使用 Django 表单从我的前端创建一个课程,一切似乎都很好,但是当我点击创建按钮时,它只是刷新页面而不保存到数据库或返回任何错误,这不是我想要的期待中。
创建-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')
这是通过上传图片解决的,但我没有看到任何错误,因为我没有在创建页面中显示错误
{{ form.errors }}