Created
October 20, 2011 07:57
-
-
Save aita/1300634 to your computer and use it in GitHub Desktop.
ListFormView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListFormMixin(MultipleObjectMixin, FormMixin): | |
'''フォームを持つListViewの実装のためのMixin | |
memo: | |
get_context_dataメソッドが重複する | |
''' | |
class BaseListFormView(ListFormMixin, View): | |
def get(self, request, *args, **kwargs): | |
self.object_list = self.get_queryset() | |
allow_empty = self.get_allow_empty() | |
if not allow_empty and len(self.object_list) == 0: | |
raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.") | |
% {'class_name': self.__class__.__name__}) | |
form_class = self.get_form_class() | |
form = self.get_form(form_class) | |
context = self.get_context_data(object_list=self.object_list, form=form) | |
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) | |
# PUT is a valid HTTP verb for creating (with a known URL) or editing an | |
# object, note that browsers only support POST for now. | |
def put(self, *args, **kwargs): | |
return self.post(*args, **kwargs) | |
class ListFormView(TemplateResponseMixin, BaseListFormView): | |
'''フォームが使えるListView''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment