实训项目第二周2

xiaoxiao2021-02-28  147

以下是论坛方面的代码

# -*- coding: utf-8 -*-   from __future__ import unicode_literals      from django.shortcuts import get_object_or_404, render, render_to_response      # Create your views here.   from django.http import HttpResponseRedirect, HttpResponse   from django.urls import reverse   from django.utils import timezone   from django.template import loader, RequestContext   from django.views import generic   from django import forms      from .models import Problem, User, SubmitCode, AcceptedCode, Discussion, Response      class DiscussionForm(forms.Form):       title = forms.CharField()       text = forms.CharField(widget=forms.Textarea)       code = forms.CharField(widget=forms.Textarea)            # coding page , submit code , get result   class CodingView(generic.TemplateView):       template_name = 'judgeOL/coding.html'          def get_context_data(self, **kwargs):           context = super(CodingView, self).get_context_data(**kwargs)           context['problem_id'] = self.kwargs['problem_id']           return context         # bbs page, so many discussions   class DiscussionView(generic.ListView):       model = Discussion       template_name = 'judgeOL/discussions.html'       context_object_name = 'discussion_list'         @staticmethod       def get_discussions(problem_id):           return Discussion.objects.get(problem_id=problem_id)          def get_context_data(self, **kwargs):           context = super(DiscussionView, self).get_context_data(**kwargs)           context['problem_id'] = self.kwargs['problem_id']           context['problem_name'] = Problem.objects.get(pk=self.kwargs['problem_id']).name           return context         # every discussions have a lot of responses   class ResponseView(generic.ListView):       model = Response       template_name = 'judgeOL/responses.html'       context_object_name = 'response_list'          def get_queryset(self):           return Response.objects.all().filter(discussion_id=self.kwargs['discussion_id'])          def get_context_data(self, **kwargs):           context = super(ResponseView, self).get_context_data(**kwargs)           context['discussion'] = Discussion.objects.get(pk=self.kwargs['discussion_id'])           context['problem_id'] = self.kwargs['problem_id']           return context         class EditView(generic.TemplateView):       template_name = 'judgeOL/edit.html'          def get_context_data(self, **kwargs):           context = super(EditView, self).get_context_data(**kwargs)           context['problem_id'] = self.kwargs['problem_id']           return context      def new_discussion(request, problem_id):       if request.method == 'GET':           return HttpResponseRedirect(reverse('judgeOL:discussions', args={problem_id}))       else:           try:               name = request.POST['title']               pub_text = request.POST['text']               pub_code = request.POST['code']               user_id = request.session['user_id']               Discussion.objects.create(user_id=user_id, problem_id=problem_id, name=name, pub_text=pub_text,                                         pub_code=pub_code, pub_date=timezone.now(), vote_count=1, view_count=1)               return HttpResponseRedirect(reverse('judgeOL:discussions', args={problem_id}))           except KeyError:               return HttpResponse('key error')              """          discussion_form = DiscussionForm(request.POST)          if discussion_form.is_valid():              name = discussion_form.cleaned_data['title']              pub_text = discussion_form.cleaned_data['pub_text']              pub_code = discussion_form.cleaned_data['pub_code']              user_id = request.session['user_id']              Discussion.objects.create(user_id=user_id, problem_id=problem_id, name=name, pub_text=pub_text,                                        pub_code=pub_code, pub_date=timezone.now(), vote_count=1, view_count=1)              return HttpResponse(reverse('judgeOL:discussions', args={problem_id}))          else:              return HttpResponse('judgeOL/error.html')        

转载请注明原文地址: https://www.6miu.com/read-17740.html

最新回复(0)