为了账号安全,请及时绑定邮箱和手机立即绑定

django的templates和url,views之间的关系怎么弄

django的templates和url,views之间的关系怎么弄

MMTTMM 2019-03-12 15:09:51
django的templates和url,views之间的关系怎么弄
查看完整描述

2 回答

?
慕少森

TA贡献2019条经验 获得超9个赞


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

from django.core.urlresolvers import reverse_lazy

from django.shortcuts import redirect, get_object_or_404

from django.forms.models import modelform_factory

from django.apps import apps

from django.views.generic.base import TemplateResponseMixin, View

from django.views.generic.list import ListView

from django.views.generic.edit import CreateView, UpdateView, DeleteView

from braces.views import LoginRequiredMixin, PermissionRequiredMixin

from braces.views import CsrfExemptMixin, JsonRequestResponseMixin

from .models import Course, Module, Content

from .forms import ModuleFormSet

from django.db.models import Count

from .models import Subject

from django.views.generic.detail import DetailView

from students.forms import CourseEnrollForm

# Create your views here.

 

 

class OwnerMixin(object):

    def get_queryset(self):

        qs = super(OwnerMixin, self).get_queryset()

        return qs.filter(owner=self.request.user)

 

 

class OwnerEditMixin(object):

    def form_valid(self, form):

        form.instance.owner = self.request.user

        return super(OwnerEditMixin, self).form_valid(form)

 

 

class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin):

    model = Course

    fields = ['subject', 'title', 'slug', 'overview']

    success_url = reverse_lazy('manage_course_list')

 

 

class OwnerCourseEditMinxin(OwnerCourseMixin, OwnerEditMixin):

    fields = ['subject', 'title', 'slug', 'overview']

    success_url = reverse_lazy('manage_course_list')

    template_name = 'courses/manage/course/form.html'

 

 

class ManageCourseListView(OwnerCourseMixin, ListView):

 

    template_name = 'courses/manage/course/list.html'

 

 

class CourseCreateView(PermissionRequiredMixin,

                       OwnerCourseEditMinxin,

                       CreateView):

    permission_required = 'courses.add_course'

 

 

class CourseUpdateView(PermissionRequiredMixin,

                       OwnerCourseEditMinxin,

                       UpdateView):

    permission_required = 'courses.change_course'

 

 

class CourseDeleteView(PermissionRequiredMixin,

                       OwnerCourseMixin,

                       DeleteView):

    template_name = 'courses/manage/course/delete.html'

    success_url = reverse_lazy('manage_course_list')

    permission_required = 'courses.delete_course'

 

 

class CourseModuleUpdateView(TemplateResponseMixin, View):

    template_name = 'courses/manage/module/formset.html'

    course = None

 

    def get_formset(self, data=None):

        return ModuleFormSet(instance=self.course,

                             data=data)

 

    def dispatch(self, request, pk):

        self.course = get_object_or_404(Course, id=pk, owner=request.user)

        return super(CourseModuleUpdateView, self).dispatch(request, pk)

 

    def get(self, request, *args, **kwargs):

        formset = self.get_formset()

        return self.render_to_response({'course': self.course,

                                        'formset': formset})

 

    def post(self, request, *args, **kwargs):

        formset = self.get_formset(data=request.POST)

        if formset.is_valid():

            formset.save()

            return redirect('manage_course_list')

        return self.render_to_response({'course': self.course,

                                        'formset': formset})

 

 

class ContentCreateUpdateView(TemplateResponseMixin, View):

    module = None

    model = None

    obj = None

    template_name = 'courses/manage/content/form.html'

 

    def get_model(self, model_name):

        if model_name in ['text', 'video', 'image', 'file']:

            return apps.get_model(app_label='courses',

                                  model_name=model_name)

        return None

 

    def get_form(self, model, *args, **kwargs):

        Form = modelform_factory(model, exclude=['owner',

                                                 'order',

                                                 'created',

                                                 'updated'])

        return Form(*args, **kwargs)

 

    def dispatch(self, request, module_id, model_name, id=None):

        self.module = get_object_or_404(Module,

                                        id=module_id,

                                        course__owner=request.user)

        self.model = self.get_model(model_name)

        if id:

            self.obj = get_object_or_404(self.model, id=id, owner=request.user)

        return super(ContentCreateUpdateView, self).dispatch(request, module_id, model_name, id)

 

    def get(self, request, module_id, model_name, id=None):

        form = self.get_form(self.model, instance=self.obj)

        return self.render_to_response({'form': form, 'object': self.obj})

 

    def post(self, request, module_id, model_name, id=None):

        form = self.get_form(self.model,

                             instance=self.obj,

                             data=request.POST,

                             files=request.FILES)

        if form.is_valid():

 

            obj = form.save(commit=False)

            obj.owner = request.user

            obj.save()

            print id, '&&&&&&&&&&&'

            if not id:

                print '****************'

                Content.objects.create(module=self.module,

                                       item=obj)

                print '****************'

            return redirect('module_content_list', self.module.id)

        return self.render_to_response({'form': form, 'object': self.obj})

 

 

class ContentDeleteView(View):

    def post(self, request, id):

        content = get_object_or_404(Content,

                                    id=id,

                                    module__course__owner=request.user)

        module = content.module

        content.item.delete()

        content.delete()

        return redirect('module_content_list', module.id)

 

 

class ModuleContentListView(TemplateResponseMixin, View):

    template_name = 'courses/manage/module/content_list.html'

 

    def get(self, request, module_id):

        module = get_object_or_404(Module,

                                   id=module_id,

                                   course__owner=request.user)

        return self.render_to_response({'module': module})

 

 

class ModuleOrderView(CsrfExemptMixin,

                      JsonRequestResponseMixin,

                      View):

    def post(self, request):

        for id, order in self.request_json.items():

            Module.objects.filter(id=id,

                                  course__owner=request.user).update(order=order)

        return self.render_json_response({'saved': 'OK'})

 

 

class ContentOrderView(CsrfExemptMixin,

                       JsonRequestResponseMixin,

                       View):

    def post(self, request):

        for id, order in self.request_json.items():

            Content.objects.filter(id=id,

                                   module__course__owner=request.user)\

                .update(order=order)

        return self.render_json_response({'saved': 'OK'})

 

 

class CourseListView(TemplateResponseMixin, View):

    model = Course

    template_name = 'courses/course/list.html'

 

    def get(self, request, subject=None):

        subjects = Subject.objects.annotate(

            total_courses=Count('courses')

        )

        courses = Course.objects.annotate(

            total_modules=Count('modules')

        )

        if subject:

            subject = get_object_or_404(Subject, slug=subject)

            courses = courses.filter(subject=subject)

        print '***********', subjects, courses, subject

        return self.render_to_response({'subjets': subjects,

                                        'subject': subject,

                                        'courses': courses})

 

 

class CourseDetailView(DetailView):

    model = Course

    template_name = 'courses/course/detail.html'

 

    def get_context_data(self, **kwargs):

        context = super(CourseDetailView,

                        self).get_context_data(**kwargs)

        context['enroll_form'] = CourseEnrollForm(

            initial={'course': self.object})

        return context


 


查看完整回答
反对 回复 2019-03-22
  • 2 回答
  • 0 关注
  • 989 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信