TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Our custom Django mixins

63 pointsby kennethloveabout 13 years ago

4 comments

rlanderabout 13 years ago
Very nice snippets, though they're meant to work only with Django's default class views.<p>A few months ago I started using a small class-based views library called aino-utkik[1] and haven't looked back.<p>Instead of this:<p><pre><code> class ArtistLogin(FormView): form_class = ArtistLoginForm template_name = 'artists/artist_login.html' def get(self, request, *args, **kwargs): request.session.set_test_cookie() return super(ArtistLogin, self).get(request, *args, **kwargs) def form_valid(self, form): login(self.request, form.artist) return HttpResponseRedirect(reverse('artist_mypage')) def get_form_kwargs(self): kwargs = super(ArtistLogin, self).get_form_kwargs() kwargs['request'] = self.request return kwargs </code></pre> I can write this:<p><pre><code> class ArtistLogin(View): def setup(self): self.c.form = ArtistLoginForm( request=self.request, data=self.request.POST or None) def get(self): self.request.session.set_test_cookie() def post(self): if self.c.form.is_valid(): login(self.request, self.c.form.artist) return HttpResponseRedirect(reverse('artist_mypage')) </code></pre> which I find way more elegant.<p>[1] <a href="https://github.com/aino/aino-utkik" rel="nofollow">https://github.com/aino/aino-utkik</a>
评论 #3654041 未加载
评论 #3654102 未加载
andrewingramabout 13 years ago
Here are my extensions to Django's class-based views:<p><a href="https://github.com/AndrewIngram/django-extra-views" rel="nofollow">https://github.com/AndrewIngram/django-extra-views</a>
jarcoalabout 13 years ago
Django's documentation is indeed terrible for class based views, but I encourage any developers that have had trouble reading through it to just take a look at the source. I found it to be much easier to learn that way.
评论 #3654439 未加载
评论 #3654659 未加载
tkaemmingabout 13 years ago
I've found this view_class_decorator and MultipleFormView particularly helpful with class-based views: <a href="https://gist.github.com/1953579" rel="nofollow">https://gist.github.com/1953579</a>
评论 #3654346 未加载