专注Java领域技术
我们一直在努力

Django课程内容的表单类和视图类

原文始发于:Django课程内容的表单类和视图类

一 表单类

from django import forms from .models import Course, Lesson  # 课程标题表单类 class CreateCourseForm(forms.ModelForm):     class Meta:         model = Course         fields = ("title", "overview")  # 课程内容表单类 class CreateLessonForm(forms.ModelForm):     class Meta:         model = Lesson         fields = ['course', 'title', 'video', 'description', 'attach']      def __init__(self, user, *args, **kwargs):         super(CreateLessonForm, self).__init__(*args, **kwargs)         # 通过user这个参数,筛选出当前用户的course值         self.fields['course'].queryset = Course.objects.filter(user=user)

二 编写视图类

class CreateLessonView(LoginRequiredMixin, View):     model = Lesson     login_url = "/account/login/"     # 重写view的get方法     def get(self, request , *args, **kwargs):         # 创建表单类实例         form = CreateLessonForm(user=self.request.user)         return render(request, "course/manage/create_lesson.html", {"form":form})      # 重写view的post方法,响应用户提交的表单     def post(self, request, *args, **kwargs):         # 因为提交的表单中有上传的文件,所以必须传入request.FILES         form = CreateLessonForm(self.request.user, request.POST, request.FILES)         if form.is_valid():             new_lesson = form.save(commit=False)             new_lesson.user = self.request.user             new_lesson.save()             return redirect("course:manage_course")

三 编写前端模板

{% extends "article/base.html" %} {% block title %}create lesson{% endblock %} {% block content %} <div style="margin-left:200px;margin-top:10px;">   <form enctype="multipart/form-data" action="." method="post" class="form-horizontal">{% csrf_token %}     <div style="margin-top: 10px;">         <div class="col-md-2 text-left"><span>内容标题:</span></div>         <div class="col-md-10 text-left">{{form.title}}</div>     </div>     <div style="margin-top: 20px;">         <div class="col-md-2 text-left"><span>所属课程:</span></div>         <div class="col-md-10 text-left">{{form.course}}</div>     </div>     <div style="margin-top: 20px;">         <div class="col-md-2 text-left"><span>上传视频:</span></div>         <div class="col-md-10 text-left">{{form.video}}</div>     </div>     <div style="margin-top: 20px;">         <div class="col-md-2 text-left"><span>视频描述:</span></div>         <div class="col-md-10 text-left">{{form.description}}</div>     </div>     <div style="margin-top: 20px;">       <div class="col-md-2 text-left"><span>本课附件:</span></div>       <div class="col-md-10 text-left">{{form.attach}}</div>     </div>     <div style="margin-top: 20px;" class="text-center">       <input type="submit" value="Create Lesson">     </div>   </form> </div> {% endblock %}

四 编写URL

from django.conf.urls import url from django.views.generic import TemplateView from .views import AboutView, CourseListView, ManageCourseListView, CreateCourseView, DeleteCourseView, CreateLessonView, ListLessonsView, DetailLessonView from .views import StudentListLessonView  urlpatterns = [     url(r'about/$', AboutView.as_view(), name="about"),     url(r'course-list/$', CourseListView.as_view(), name="course_list"),     url(r'manage-course/$', ManageCourseListView.as_view(), name="manage_course"),     url(r'create-course/$', CreateCourseView.as_view(), name="create_course"),     # 默认情况DeleteView类接收以pk或者slug作为参数传入的值,并且通过GET方式访问一个删除的     # 确认页面,然后以POST方式提交删除表单,才能完成删除     url(r'delete-course/(?P<pk>d+)/$', DeleteCourseView.as_view(), name="delete_course"),     # 创建课程内容     url(r'create-lesson/$', CreateLessonView.as_view(), name="create_lesson"), ]

五 编写前端入口

<div class="bg-info">     <div class="text-center" style="margin-top: 5px;">         <p><h4>文章管理</h4></p>         <p><a href="{% url 'article:article_column'%}">栏目管理</a></p>         <p><a href="{% url 'article:article_post'%}">发布文章</a></p>         <p><a href="{% url 'article:article_list' %}">文章列表</a></p>         <p><a href="{% url 'article:article_tag' %}">文章标签</a></p>     </div>     <hr>     <div class="text-center" style="margin-top: 5px;">         <p><h4>图片管理</h4></p>         <p><a href="{% url 'image:list_images' %}">图片管理</a></p>     </div>     <hr>     <div class="text-center" style="margin-top: 5px;">         <p><h4>课程管理</h4></p>         <p><a href="{% url 'course:manage_course' %}">课程管理</a></p>         <p><a href="{% url 'course:create_lesson' %}">内容发布</a></p>     </div> </div>

六 测试

Django课程内容的表单类和视图类

七 数据库生成情况

Django课程内容的表单类和视图类

赞(0) 打赏
未经允许不得转载:Java小咖秀 » Django课程内容的表单类和视图类
免责声明

抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专注Java技术 100年

联系我们联系我们

你默默的关注就是最好的打赏~

支付宝扫一扫打赏

微信扫一扫打赏