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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:03:43+00:00 2026-06-11T23:03:43+00:00

I’m trying to test the content of messages while processing ModelForms with Django. I’ve

  • 0

I’m trying to test the content of messages while processing ModelForms with Django. I’ve got the following View (assume there’s a Thing model with a required name field):

@login_required
def update(request, thing_id):
    thing = Thing.objects.get(id=thing_id)  # assume this works

    if request.method == "POST":
        form = ThingModelForm(request.POST, instance=thing)
        if form.is_valid():
            form.save()
            messages.success(request, "Success!")
            return redirect("/wherever")
        else:
            messages.error(request, "Oops!")
    else:
        form = ThingModelForm(instance=thing)

    args = ("myapp/update.html", {"form": form})
    kwargs = {"context_instance": RequestContext(request)}
    return render_to_response(*args, **kwargs)

Now, I’ve got two unit tests. The first tests valid data, while the second tests invalid data. (Note that client login happens during setUp):

def test_update_account(self):
    url = reverse('update', args=[1])  # assume that's a valid id
    resp = self.client.post(url, {"name": "foo"})
    self.assertEqual(resp.status_code, 302)

    m = resp.cookies.get('messages', '')
    self.assertTrue("Success!" in m.output())

And now to test invalid data:

def test_update_account_failure(self):
    url = reverse('update', args=[1])  # assume that's a valid id
    resp = self.client.post(url, {"name": ""})  # name is required
    self.assertEqual(resp.status_code, 200)

    # This works:
    self.assertTrue("Oops!" in resp.content)

    # This fails:
    m = resp.cookies.get('messages', '')
    self.assertTrue("Oops!" in m.output())

Why would accessing the message’s content through the cookie work in one instance but fail in another?

  • 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-11T23:03:44+00:00Added an answer on June 11, 2026 at 11:03 pm

    Two things you could check:

    1. When you create the request self.client.post(url, {"name": ""}) is a Thing instance returned here: thing = Thing.objects.get(id=thing_id) If not it will not reach the line of code where you set your error message: messages.error(request, "Oops!") as Thing.objects.get will throw an error.

    If there are no results that match the query, get() will raise a
    DoesNotExist exception.

    1. If the first thing does return a Thing instance, you could check whether a redirect return redirect("/wherever") after setting the error message messages.error(request, "Oops!") does change anything.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this

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.