干掉我的Django观点:请帮助我!

时间:2011-05-19 09:58:50

标签: django django-views

我希望这是一个很好的第一个问题。我一直试图干掉我的Django代码,但不幸的是我一直遇到了几个错误! (请原谅我,如果我不发布有问题的代码 - 我会发布有效的代码)我尝试使用@decorators,并在视图中放置视图。请帮帮我!

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_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))

(希望格式正确)我想特别考虑的代码是

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

但当然还有任何建议值得赞赏!非常感谢!     `

2 个答案:

答案 0 :(得分:5)

  1. 使用login_required装饰器(docs
  2. 使用模型来编辑/创建模型。 (docs
  3. 如果您只需要将某些内容传递给模板,请使用direct_to_template通用视图(docs

答案 1 :(得分:1)

您可以使用 @login_required 装饰器。您可以在Django文档中找到更多信息:http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required

请描述您遇到的任何问题。