Django从UI中添加用户学习管理系统应用程序的新课程
在我的应用程序上,我只能在Django Admin面板中使用该模型添加Django Admin面板中的新课程。
图片1- django管理面板
图2-从管理面板中添加新课程
我想自动化整个过程。我想创建一个表格,用户可以填写表格并使用UI提交课程。该课程将自动添加到课程中。因此,为了实现我的目标,我应该采取的必要步骤是什么?为了更好地了解我的问题,这是指向我项目的GitHub回购链接。
我的项目的github repo
tecah.html表格,用于添加新课程:
{% extends 'layouts/base.html' %}
{% load static %}
{% load humanize %}
{% block content %}
<div class="container">
<br>
<h1>Teach on Hogwarts</h1>
<br>
{% if user.is_authenticated %}
<h3 align="center">
<i class="fa-solid fa-door-open"></i>
Welcome
<i class="fa-solid fa-door-open"></i>
</h3><br>
<img src="{% static 'images/info_banner.png' %}" alt="info" width="770" height="200" style="vertical-align:middle;margin:auto auto"/>
<br>
<div class="container">
<form class="form-horizontal" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="mb-3">
<label for="formGroupExampleInput" class="form-label">Course Title</label>
<input name="title" type="text" class="form-control" id="f1" placeholder="Enter the course title" required>
</div>
<div class="mb-3">
<label>CATEGORY</label>
<select name="category" class="form-control" id="f2" required>
<option value="GD">Graphis & Design</option>
<option value="WD">Web & Mobile Development</option>
<option value="BE">Basic Education</option>
<option value="CS">Computer Science</option>
<option value="PT">Programming & Tech</option>
</select>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label" >Short Description</label>
<input name="short_description" required type="text" class="form-control" id="f3" placeholder="Write a short Description">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1" >Description</label>
<textarea name="description" class="form-control" id="f4" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Outcome</label>
<input name="outcome" type="text" class="form-control" id="f5" placeholder="">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Requirements</label>
<input name="requirements" type="text" class="form-control" id="f6" placeholder="Write about prerequisites">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label" >Language</label>
<input name="language" type="text" class="form-control" required id="f7" placeholder="Which Language is used for this course?">
</div>
<div class="mb-3">
<label>PRICE ($)</label>
<input type="number" id="f8" required class="form-control" value="19.99" name="price">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Level</label>
<input name="level" type="text" class="form-control" id="f9" placeholder="Difficulty of this course?">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Video URL</label>
<input name="url" type="text" required class="form-control" id="f10" placeholder="Enter the URL of this course.">
</div>
<div class="mb-3">
<label>Thumbnail</label>
<input type="file" required class="form-control" name="photo">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
{% else %}
<p align="center">
<i class="fa-solid fa-circle-info" align="center"></i><br>
Please login first to access this feature!!!
</p><br>
{% endif %}
<br>
</div>
{% endblock %}
< << forms.py(我手动创建此):
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.forms import UserCreationForm
from .models import Category,Course,Lesson
models.py:
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.text import slugify
from django.utils.timezone import now
from accounts.models import User
class Category(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(max_length=200, unique=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Category, self).save(*args, **kwargs)
class Course(models.Model):
title = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
slug = models.SlugField(max_length=200, unique=True, primary_key=True, auto_created=False)
short_description = models.TextField(blank=False, max_length=60)
description = models.TextField(blank=False)
outcome = models.CharField(max_length=200)
requirements = models.CharField(max_length=200)
language = models.CharField(max_length=200)
price = models.FloatField(validators=[MinValueValidator(9.99)])
level = models.CharField(max_length=20)
thumbnail = models.ImageField(upload_to='thumbnails/')
video_url = models.CharField(max_length=100)
is_published = models.BooleanField(default=True)
created_at = models.DateTimeField(default=now)
updated_at = models.DateTimeField(default=now)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Course, self).save(*args, **kwargs)
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='lessons')
title = models.CharField(max_length=100)
duration = models.FloatField(validators=[MinValueValidator(0.30), MaxValueValidator(30.00)])
video_url = models.CharField(max_length=100)
created_at = models.DateTimeField(default=now)
updated_at = models.DateTimeField(default=now)
def __str__(self):
return self.title
urls.py:
from django.urls import path
from .views import *
app_name = 'courses'
urlpatterns = [
path('courses/<slug:slug>', CourseDetailView.as_view(), name='course-details'),
path('courses/<slug:slug>/category', CoursesByCategoryListView.as_view(), name='course-by-category'),
]
On my app, I can add new courses from Django Admin Panel Only by using the model in Django Admin Panel.
Picture 1 - Django Admin Panel
Picture 2 - Adding New Course from Admin Panel
I want to automate this whole process. I want to create a form where users can fill out the form and submit their Course using UI. And That course will automatically be added to the Courses. So, to achieve my goal, what are the necessary steps I should take? For a better understanding of my problem here is the GitHub repo link to my project.
GitHub Repo of My Project
Tecah.html form for adding new Course:
{% extends 'layouts/base.html' %}
{% load static %}
{% load humanize %}
{% block content %}
<div class="container">
<br>
<h1>Teach on Hogwarts</h1>
<br>
{% if user.is_authenticated %}
<h3 align="center">
<i class="fa-solid fa-door-open"></i>
Welcome
<i class="fa-solid fa-door-open"></i>
</h3><br>
<img src="{% static 'images/info_banner.png' %}" alt="info" width="770" height="200" style="vertical-align:middle;margin:auto auto"/>
<br>
<div class="container">
<form class="form-horizontal" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="mb-3">
<label for="formGroupExampleInput" class="form-label">Course Title</label>
<input name="title" type="text" class="form-control" id="f1" placeholder="Enter the course title" required>
</div>
<div class="mb-3">
<label>CATEGORY</label>
<select name="category" class="form-control" id="f2" required>
<option value="GD">Graphis & Design</option>
<option value="WD">Web & Mobile Development</option>
<option value="BE">Basic Education</option>
<option value="CS">Computer Science</option>
<option value="PT">Programming & Tech</option>
</select>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label" >Short Description</label>
<input name="short_description" required type="text" class="form-control" id="f3" placeholder="Write a short Description">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1" >Description</label>
<textarea name="description" class="form-control" id="f4" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Outcome</label>
<input name="outcome" type="text" class="form-control" id="f5" placeholder="">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Requirements</label>
<input name="requirements" type="text" class="form-control" id="f6" placeholder="Write about prerequisites">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label" >Language</label>
<input name="language" type="text" class="form-control" required id="f7" placeholder="Which Language is used for this course?">
</div>
<div class="mb-3">
<label>PRICE ($)</label>
<input type="number" id="f8" required class="form-control" value="19.99" name="price">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Level</label>
<input name="level" type="text" class="form-control" id="f9" placeholder="Difficulty of this course?">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Video URL</label>
<input name="url" type="text" required class="form-control" id="f10" placeholder="Enter the URL of this course.">
</div>
<div class="mb-3">
<label>Thumbnail</label>
<input type="file" required class="form-control" name="photo">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
{% else %}
<p align="center">
<i class="fa-solid fa-circle-info" align="center"></i><br>
Please login first to access this feature!!!
</p><br>
{% endif %}
<br>
</div>
{% endblock %}
Forms.py (I created this manually):
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.forms import UserCreationForm
from .models import Category,Course,Lesson
Models.py:
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.text import slugify
from django.utils.timezone import now
from accounts.models import User
class Category(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(max_length=200, unique=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Category, self).save(*args, **kwargs)
class Course(models.Model):
title = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
slug = models.SlugField(max_length=200, unique=True, primary_key=True, auto_created=False)
short_description = models.TextField(blank=False, max_length=60)
description = models.TextField(blank=False)
outcome = models.CharField(max_length=200)
requirements = models.CharField(max_length=200)
language = models.CharField(max_length=200)
price = models.FloatField(validators=[MinValueValidator(9.99)])
level = models.CharField(max_length=20)
thumbnail = models.ImageField(upload_to='thumbnails/')
video_url = models.CharField(max_length=100)
is_published = models.BooleanField(default=True)
created_at = models.DateTimeField(default=now)
updated_at = models.DateTimeField(default=now)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Course, self).save(*args, **kwargs)
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='lessons')
title = models.CharField(max_length=100)
duration = models.FloatField(validators=[MinValueValidator(0.30), MaxValueValidator(30.00)])
video_url = models.CharField(max_length=100)
created_at = models.DateTimeField(default=now)
updated_at = models.DateTimeField(default=now)
def __str__(self):
return self.title
urls.py:
from django.urls import path
from .views import *
app_name = 'courses'
urlpatterns = [
path('courses/<slug:slug>', CourseDetailView.as_view(), name='course-details'),
path('courses/<slug:slug>/category', CoursesByCategoryListView.as_view(), name='course-by-category'),
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
modelform
,保存课程
模型实例的以下方法。tecah.html
forms.py
views.py
添加一个block
internalstyle
inlayouts/base.html
。布局/base.html
You should use
ModelForm
, in the following way for saving theCourse
model instances.tecah.html
forms.py
views.py
Add a block
internalStyle
inlayouts/base.html
.layouts/base.html