一 创建窗体类

from django import forms
from .models import Course

class CreateCourseForm(forms.ModelForm):
    class Meta:
        model = Course
        fields = ("title", "overview")

二 创建视图类

# 引入ListView
from django.views.generic import TemplateView,ListView
from django.views.generic.edit import CreateView
from .models import Course
from braces.views import LoginRequiredMixin
from .forms import CreateCourseForm
from django.shortcuts import redirect

class AboutView(TemplateView):
    template_name = "course/about.html"

# 继承ListView
class CourseListView(ListView):
    # 被类所使用的数据模型,能够得到数据表中的所有记录
    model = Course
    # 传入模板的变量名称
    context_object_name = "courses"
    # 模板文件
    template_name = 'course/course_list.html'

class UserMixin:
    def get_queryset(self):
        qs = super(UserMixin, self).get_queryset()
        return qs.filter(user=self.request.user)

# 增加一个继承类LoginRequiredMixin,用于判断是否登录
class UserCourseMixin(UserMixin, LoginRequiredMixin):
    model = Course
    # 声明用户登录URL
    login_url = "/account/login/"



class ManageCourseListView(UserCourseMixin, ListView):
    context_object_name = "courses"
    template_name = 'course/manage/manage_course_list.html'

# 当用户以GET方式请求时,即在页面显示表单,CreateView就是完成这个作用的类
# 只要继承它,就不需要写get()方法了。
class CreateCourseView(UserCourseMixin, CreateView):
    # 声明在表单中显示的字段
    fields = ['title', 'overview']
    template_name = 'course/manage/create_course.html'

    def post(self, request, *args, **kargs):
        form = CreateCourseForm(data=request.POST)
        if form.is_valid():
            new_course = form.save(commit=False)
            new_course.user = self.request.user
            new_course.save()
            # 当表单内容被保存后,将页面转向指定位置
            return redirect("course:manage_course")
        # 当表单数据检测不通过时,让用户重新填写
        return self.render_to_response({"form":form})

三 创建前端模板

{% extends "article/base.html" %}
{% block title %}create course{% endblock %}
{% block content %}
<div style="margin-left:100px;margin-top:10px;">
  <form action="." method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="创建课程">
  </form>
</div>
{% endblock %}

四 增加按钮添加课程

{% extends "article/base.html" %}
{% load staticfiles %}
{% block title %}管理课程{% endblock %}

{% block content %}
<div>
    <div class='text-right'><a href="{% url 'course:create_course' %}"><button type="button" class="btn btn-primary">添加课程</button></a></div>
    <table class="table table-hover" style="margin-top:10px">
        <tr>
            <td>序号</td>
            <td>课程标题</td>
            <td>发布日期</td>
            <td>操作</td>
        </tr>
        {% for course in courses %}
        <tr id={{ forloop.counter }}>
            <td>{{ forloop.counter }}</td>
            <td>{{ course.title }}</a></td>
            <td>{{ course.created|date:"Y-m-d" }}</td>
            <td>
                <a name="edit" href="#"><span class="glyphicon glyphicon-pencil"></span></a>
                <!--a nane="delete" href="#" ><span class="glyphicon glyphicon-trash" style="margin-left:20px;"></span></a-->
                <a class="delete" nane="delete" href="#" ><span class="glyphicon glyphicon-trash" style="margin-left:20px;"></span></a>
                <a href="#"><span class="glyphicon glyphicon-search" style="margin-left:20px;"></span></a>
            </td>
        </tr>
        {% endfor %}
    </table>
</div>
{% endblock %}

五 测试

Django实现创建课程

Django实现创建课程

相关文章:

  • 2021-09-24
  • 2022-01-08
  • 2022-01-02
  • 2021-12-15
  • 2021-07-19
  • 2021-08-21
  • 2021-11-23
猜你喜欢
  • 2021-10-10
  • 2021-07-15
  • 2021-12-15
  • 2021-07-10
  • 2021-06-15
  • 2021-12-24
  • 2021-11-13
相关资源
相似解决方案