Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:19:33+00:00 2026-05-22T14:19:33+00:00

I hope this is a good first question. I’ve been trying to DRY out

  • 0

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_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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:19:34+00:00Added an answer on May 22, 2026 at 2:19 pm
    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)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question here. I hope dont make any mistake, be good
(Since this is my first SO question, let me just say I hope it's
My first post on this site with huge hope:: I am trying to understand
this my first question on here. I hope someone can help. It is to
I am trying to figure out what this compile error means, and I hope
hope this will be a quick easy question, I've just tried my app on
I hope this is allowed but I have a number of questions regarding Facebook
I hope this is a quick question I hope. I need to write some
(I hope this is a valid question) As I stated in my title, I'm
I hope this I can explain what I am trying to achieve: I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.