开发者

DRYing out my Django view: Please help me!

I hope this is a good first question. I've been trying to DRY out my Django code, but unfortunately I keep getting hit with several errors! (Forgive me if I don't post the code with problems - I'll just post the code that works) I've tried using @decorators, and also putting a view within a view. Please help me!

from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from blog.models import Post, User, Blog, Comment
from blog.forms import CommentForm, PostForm, BlogForm
from django.core.urlresolvers import reverse
import datetime
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django import forms

def limiter(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))

def postindex(request):
    posts = get_list_or_404(Post.objects.all())
    return render_to_response('index.html', {'posts':posts})

def onepost(request, postid):
    post = get_object_or_404(Post, pk=postid)
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseRedirect(reverse('accounts.views.login_view'))
        form = CommentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            c = Comment(owner=request.user,
                post=Post.objects.get(pk=postid),
                time=datetime.datetime.now(),
                text=cd['text'])
            c.save()
            return HttpResponseRedirect(reverse('blog.views.onepost', 
                args=[postid]))
    else:
        form = CommentForm()
    return render_to_response('single.html',
        {'post':post,'comments':post.comment_set.all(),
        'form':form},
        context_instance=RequestContext(request))

def userlist(request):
    users = get_list_or_404(User.objects.all())
    return render_to_response('userlist.html', {'users':users})

def bloglist(request, userid):
    blogs = get_list_or_404(Blog.objects.filter(owner__pk=userid))
    return render_to_response('bloglist.html', {'blogs':blogs})

def postlist(request, blogid):
    posts = get_list_or_404(Post.objects.filter(blog__pk=blogid))
    return render_to_response('postlist.html', {'posts':posts})

def landing(request):
    return render_to_response('landing.html', {})

def dash(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    user = request.user
    blogs = Blog.objects.filter(owner=request.user)
    comments = Comment.objects.filter(owner=request.user)
    posts = Post.objects.filter(blog__owner=request.user)
    return render_to_response('dash.html',
        {'user':user, 'blogs':blogs, 'comments':comments, 'posts':posts})

def newpost(request, blogid):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    blog = Blog.objects.get(pk=blogid)
    if not request.user == blog.owner:
        return HttpResponseRedirect(reverse('blog.views.dash'))
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            p = Post(title=cd['title'],
                blog=Blog.objects.get(pk=blogid),
                date=datetime.datetime.now(),
                content=cd['content'])
            p.save()
            return HttpResponseRedirect(reverse('blog.views.postlist', 
                args=[blogid]))
    else:
        form = PostForm()
    return render_to_response('chngpost.html',
        {'blog':blog,
        'form':form},
        context_instance=RequestContext(request))

def editpost(request, postid):
    if not request.user.is开发者_JS百科_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    post = Post.objects.get(pk=postid)
    if not request.user == post.blog.owner:
        return HttpResponseRedirect(reverse('blog.views.dash'))
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            post.title=cd['title']
            post.content=cd['content']
            post.save()
            return HttpResponseRedirect(reverse('blog.views.onepost', 
                args=[postid]))
    else:
        form = PostForm(initial={'title':post.title,'content':post.content})
    return render_to_response('chngpost.html',
        {'post':post,
        'form':form},
        context_instance=RequestContext(request))

def delpost(request, postid):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    post = Post.objects.get(pk=postid)
    if not request.user == post.blog.owner:
        return HttpResponseRedirect(reverse('blog.views.dash'))
    if request.method == 'POST':
        post.delete()
        return HttpResponseRedirect(reverse('blog.views.dash'))
    return render_to_response('delpost.html',
        {'post':post},
        context_instance=RequestContext(request))

def newblog(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    if request.method == 'POST':
        form = BlogForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            b = Blog(title=cd['title'],
                owner=request.user)
            b.save()
            return HttpResponseRedirect(reverse('blog.views.bloglist', 
                args=[request.user.pk]))
    else:
        form = BlogForm()
    return render_to_response('chngpost.html',
        {'form':form},
        context_instance=RequestContext(request))

def delcomment(request, commentid):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))
    comment = Comment.objects.get(pk=commentid)
    if not (request.user == comment.post.blog.owner) | (request.user == comment.owner):
        return HttpResponseRedirect(reverse('blog.views.dash'))
    if request.method == 'POST':
        comment.delete()
        return HttpResponseRedirect(reverse('blog.views.dash'))
    return render_to_response('delpost.html',
        {},
        context_instance=RequestContext(request))

(Hope that's formatted properly) The code I'd like to factor out especially is

if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('accounts.views.login_view'))

But of course any more suggestions would be appreciated! Thank you so much! `


  1. Use login_required decorator (docs)
  2. Use modelforms for editing/creating models. (docs)
  3. If you need to just pass something to template, use direct_to_template generic view (docs)


You can use @login_required decorator for that. You can find out more at Django documentation: http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required.

Please describe any problems that you are experiencing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜