• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Class Based Generic Views в Django
 

Class Based Generic Views в Django

on

  • 2,703 views

 

Statistics

Views

Total Views
2,703
Views on SlideShare
2,553
Embed Views
150

Actions

Likes
4
Downloads
20
Comments
0

5 Embeds 150

http://moscowdjango.ru 108
http://127.0.0.1 28
http://localhost 10
http://moscowdjango.herokuapp.com 3
http://0.0.0.0 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Class Based Generic Views в Django Class Based Generic Views в Django Presentation Transcript

    • "Джанго сделал для гитары больше, чемкто-либо в джазе. Его стиль игры был не похож ни на один другой, благодаря ему джаз стал другим. Будет еще много выдающихся гитаристов, но никогда не будет другого Рейнхарда. Я просто уверен в этом". Стефан Грапелли, 1954
    • Class Based Generic Views Django meetup 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})
    • Class Basedclass MeetupUpdateView(UpdateView): model = Meetup template_name = ‘meetup.html’ form_class = MeetupForm success_url = ‘/thanks/’
    • Class Based Views:• TemplateView• ListView• DetailView• CreateView• UpdateView• DeleteView• RedirectView• YearArchiveView, MonthArchiveView,WeekArchiveView, DayArchiveView,TodayArchiveView
    • Реализация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
    • Реализацияclass CreateView(SingleObjectTemplateResponseMixin, BaseCreateView): template_name_suffix = _formclass UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView): template_name_suffix = _formclass DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView): template_name_suffix = _confirm_delete
    • 3 основных требования к view в Django:• Должны быть callable объектом• Должны принимать НttpRequest• Должны возвращать HttpResponse
    • # urls.pyfrom django.conf.urls import patterns, urlfrom some_app.views import AboutViewurlpatterns = patterns(, url(r^/, AboutView.as_view(), name=”some_app_about”),)
    • @classonlymethoddef as_view(cls, **initkwargs): def view(request, *args, **kwargs): self = cls(**initkwargs) return self.dispatch(request, *args, **kwargs) return view
    • @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)
    • 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)
    • 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)
    • SUCCESS!
    • http://ccbv.co.uk/
    • TemplateView
    • as_view() исходный код
    • https://github.com/FZambia/django-class-based-vs-function-based
    • 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
    • Полезные ссылки:•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