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 8439469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:00:48+00:00 2026-06-10T08:00:48+00:00

I am writing a view that drives the submit button on a blog post

  • 0

I am writing a view that drives the “submit” button on a blog post editing page. This view will be capturing POST data from the form on the blog post edit page and updating the Post object associated with that blog post.

I am fairly new to Django, and just a little bit less new to Python, so I am unsure of the best way to simplify this view.

Just to make this a little easier to understand, draft is what I am calling blog posts when in the context of editing. The code I have now (reproduced in full below) has a series of 3 of blocks like this:

try:
    new_title = request.POST['post_title']
except (KeyError):
    return render_to_response('blog/edit.html', {
        'draft': draft,
        'error_msg': "That's weird. You didn't provide a" +
                        "post title in the POST arguments.",
        }, context_instance=RequestContext(request))
draft.title = new_title
# Here goes some code specific to handling updating the title

As you can see, basically, this block just tries to get POST data, and if it can’t, it redirects back to the edit page with an error message.

Since this view essentially does the same thing 3 times, it’s in violation of the DRY principle. I am trying to find a way to fix this. The problem with separating this code into another function is that the error handling process needs to return something to the caller of the view. Should I just separate it anyway and check the type of the return value? Is that clean? Won’t I have to pass a large number of arguments to the function? Is that considered bad form?

Also, if you have any other tips for improving the style or design of this code, I would greatly appreciate them, since I am very much a Python/Django newbie.

Thanks so much!


Full view code right now:

def save_changes(request, draft_id):
    draft = get_object_or_404(Post, pk=draft_id)
    # Get the new title
    try:
        new_title = request.POST['post_title']
    except (KeyError):
       return render_to_response('blog/edit.html', {
            'draft': draft,
            'error_msg': "That's weird. You didn't provide a" +
                            "post title in the POST arguments.",
            }, context_instance=RequestContext(request))
    draft.title = new_title
    if draft.slug = None:
        draft.slug = unique_slugify(draft.title)
    # Get the new text
    try:
        new_text = request.POST['post_text']
    except (KeyError):
        return render_to_response('blog/edit.html', {
            'draft': draft,
            'error_msg': "That's weird. You didn't provide" +
                            "post text in the POST arguments.",
            }, context_instance=RequestContext(request))
    draft.text = new_text
    # Get the new publication status
    try:
        new_published_status = request.POST['publish']
    except (KeyError):
        return render_to_response('blog/edit.html', {
            'draft': draft,
            'error_msg': "That's weird. You didn't provide a" +
                            "publication status in the POST arguments."
    if new_published_status != draft.published:
        if draft.published:
            draft.published = False
            draft.pub_date = None
        else:
            draft.published = True
            draft.pub_date = timezone.now()
    draft.save()
    return HttpResponseRedirect(reverse('blog.views.edit', args=draft.id))
  • 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-06-10T08:00:49+00:00Added an answer on June 10, 2026 at 8:00 am

    A simple list comprehension will solve your issues.

    error_messages = {'post_title':'post title','post_text':'post text','publish':'publication status'}
    errors = [error_messages[key] for key in ('post_title','post_text','publish') if not request.POST.has_key(key)]
    

    This will give you a list of the error message names. You can then write one error section taking the errors list and decide what to do with it (such as display all of them, display the first, or even do some fancy logic with the grammar of the message based on how many are missing).

    May I recommend the Forms object in Django to do the validation for you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Django view that sometimes gets data from the database, and sometimes
I'm writing a view that will have daily/weekly/monthly report data. I'm thinking it makes
I'm writing a view model which derives from ReactiveValidatedObject . This allows me to
I'm writing a view helper that autoloads assets. I'd like it to autoload the
I'm writing an app that has a view with nested UITableViews. The UITableViews in
I am writing an app that allows user to view the list of installed
EDIT: Note that due to the way hard drives actually write data, none of
I am writing an iPhone application and I have a table view that is
I have a app that send messages for Twitter. I'm writing a view settings
I'm writing a custom view that needs to accept folder drops. The condition is:

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.