Advertisement
romarioagros

Untitled

Jul 6th, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. from django.shortcuts import render
  2. from django.http import  HttpResponseRedirect
  3.  
  4. from . models import Bb
  5. from .models import Rubric
  6. from django.views.generic.edit import CreateView
  7. from .forms import BbForm
  8. from django.urls import reverse_lazy,reverse
  9.  
  10. # class BbCreateViev(CreateView):
  11. #     template_name = 'bboard/create.html'
  12. #     form_class = BbForm
  13. #     success_url = reverse_lazy('index')
  14. #
  15. #
  16. #     def get_context_data(self, **kwargs):
  17. #         context = super().get_context_data(**kwargs)
  18. #         context['rubric'] = Rubric.objects.all()
  19. #         return context
  20.  
  21.  
  22.  
  23.  
  24. def index(request) :
  25.  
  26.     bbs = Bb.objects.all()
  27.     rubrics = Rubric.objects.all()
  28.     context = {'bbs' : bbs, 'rubrics':rubrics}
  29.     return  render(request, 'bboard/index.html' ,context)
  30.  
  31.     # return HttpResponse( s.encode(encoding = 'cp1251')  )
  32.  
  33.  
  34. def by_rubric(request,rubric_id):
  35.     bbs = Bb.objects.filter(rubric = rubric_id)
  36.     rubrics = Rubric.objects.all()
  37.     current_rubric = Rubric.objects.get(pk=rubric_id)
  38.     context = {'bbs': bbs, 'rubrics': rubrics,
  39.                'current_rubric': current_rubric }
  40.     return    render(request, 'bboard/by_rubric.html', context)
  41.  
  42.  
  43. def add (request):
  44.     bbf = BbForm()
  45.     context = {'form':bbf}
  46.     return render(request, 'bboard/create.html' ,context)
  47.  
  48.  
  49. def add_save(request):
  50.     bbf =BbForm(request.POST)
  51.     if bbf.is_valid():
  52.         bbf.save()
  53.         # print("save")
  54.         return HttpResponseRedirect(reverse('by_rubric',
  55.                                             kwargs={'rubriс_id':bbf.cleaned_data['rubric'].pk }))
  56.     else:
  57.         context = {'form': bbf}
  58.         return render(request, 'bboard/create.html', context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement