TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Our custom Django mixins

63 点作者 kennethlove大约 13 年前

4 条评论

rlander大约 13 年前
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 未加载
andrewingram大约 13 年前
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>
jarcoal大约 13 年前
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 未加载
tkaemming大约 13 年前
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 未加载