Class Based Generic Views в Django

2,646
-1

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,646
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Class Based Generic Views в Django

  1. 1. "Джанго сделал для гитары больше, чемкто-либо в джазе. Его стиль игры был не похож ни на один другой, благодаря ему джаз стал другим. Будет еще много выдающихся гитаристов, но никогда не будет другого Рейнхарда. Я просто уверен в этом". Стефан Грапелли, 1954
  2. 2. Class Based Generic Views Django meetup 3
  3. 3. Function Baseddef meetup_edit(request, pk): meetup = get_object_or_404(Meetup, pk = pk) if request.method == POST: form = MeetupForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect(/thanks/) else: return render_to_response(meetup.html, {form: form}) else: form = MeetupForm(instance = meetup) return render_to_response(meetup.html, {form: form})
  4. 4. Class Basedclass MeetupUpdateView(UpdateView): model = Meetup template_name = ‘meetup.html’ form_class = MeetupForm success_url = ‘/thanks/’
  5. 5. Class Based Views:• TemplateView• ListView• DetailView• CreateView• UpdateView• DeleteView• RedirectView• YearArchiveView, MonthArchiveView,WeekArchiveView, DayArchiveView,TodayArchiveView
  6. 6. Реализацияclass TemplateView(TemplateResponseMixin, ContextMixin, View): def get(self, request, *args, **kwargs): context = self.get_context_data(params = kwargs) return self.render_to_response(context)class ListView(MultipleObjectTemplateResponseMixin,BaseListView): passclass DetailView(SingleObjectTemplateResponseMixin, BaseDetailView): pass
  7. 7. Реализацияclass CreateView(SingleObjectTemplateResponseMixin, BaseCreateView): template_name_suffix = _formclass UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView): template_name_suffix = _formclass DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView): template_name_suffix = _confirm_delete
  8. 8. 3 основных требования к view в Django:• Должны быть callable объектом• Должны принимать НttpRequest• Должны возвращать HttpResponse
  9. 9. # urls.pyfrom django.conf.urls import patterns, urlfrom some_app.views import AboutViewurlpatterns = patterns(, url(r^/, AboutView.as_view(), name=”some_app_about”),)
  10. 10. @classonlymethoddef as_view(cls, **initkwargs): def view(request, *args, **kwargs): self = cls(**initkwargs) return self.dispatch(request, *args, **kwargs) return view
  11. 11. @method_decorator(login_required)def dispatch(self, request, *args, **kwargs): handler = getattr(self, request.method.lower()) self.request = request self.args = args self.kwargs = kwargs return handler(request, *args, **kwargs)
  12. 12. def get(self, request, *args, **kwargs): context = self.get_context_data(params=kwargs) return self.render_to_response(context)def post(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form)
  13. 13. def get_context_data(self, **kwargs): return kwargsdef render_to_response(self, context, **kw): return self.response_class( request = self.request, template = self.get_template_names(), context = context, **kw)
  14. 14. SUCCESS!
  15. 15. http://ccbv.co.uk/
  16. 16. TemplateView
  17. 17. as_view() исходный код
  18. 18. https://github.com/FZambia/django-class-based-vs-function-based
  19. 19. 1000 GET запросов, 1000 POST запросов:Class Based View GET:11.6180129051.Class Based View POST:15.6457400322.Function Based View GET:11.2678151131.Function Based View POST:15.1721260548.----------------------------------------------------------------------Ran 4 tests in 56.563s
  20. 20. Полезные ссылки:•https://github.com/django/django/tree/master/django/views/generic• http://habrahabr.ru/post/136784/• http://habrahabr.ru/post/137168/• http://softwaremaniacs.org/blog/2011/10/06/class-based-generic-views/• http://kimavr.name/blog/2011/9/2/7/• https://github.com/brack3t/django-braces• https://github.com/lig/django-supergeneric• https://code.djangoproject.com/ticket/6735

×