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

  • Home
  • SEARCH
  • 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 1029295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:31:22+00:00 2026-05-16T12:31:22+00:00

I have a view that looks like this: def login(request): … # some variables

  • 0

I have a view that looks like this:

def login(request):
    ...
    # some variables has been set here
    ...
    if request.POST.get('remember_me', None):
        request.session.set_expiry(1209600) # 2 weeks
    return HttpResponseRedirect(reverse('dashboard'))

Now If I assert for the variables using context, I get the error: “AttributeError: ‘HttpResponseRedirect’ object has no attribute ‘context’ ” this is the same for sessions. How do I test it then that some variables has been set and possible session expiration has been set even if i redirect?

  • 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-16T12:31:23+00:00Added an answer on May 16, 2026 at 12:31 pm

    You cannot retrieve the context variables from a HttpResponseRedirect. It doesn’t make sense why you are setting the context variables if you are redirecting anyway.

    You certainly should be able to pick up variables from session after redirecting. I have done this in several of my test cases. How are you asserting the session data in your test case?

    This is how I go about asserting session variables after a redirect:

    response = self.client.post(reverse('foo'))
    self.assertRedirects(response, reverse('bar', args = ['baz']), 
            status_code = 302, target_status_code = 200)
    self.assertEqual('value', self.client.session.get('key'))
    

    Self.client is an instance of django.test.client.Client in this case.

    Update

    (In response to @Marconi‘s comment) Here is one way of displaying a message to the user after redirecting. This is copied almost verbatim from my answer to another question.

    Your first view can create a message for the current using auth and have the second view read and delete it. Something like this:

    def first_view(request, *args, **kwargs):
        # all goes well
        message = _("<message for user>")
        request.user.message_set.create(message = message)
        return redirect('second_view')
    
    def second_view(request, *args, **kwargs):
        # Render page
    
    # Template for second_view:
    {% for message in messages %}
       ... 
    {% endfor %}
    

    Messages are saved to the database. This means that you can access them even after a redirect. They are automatically read and deleted on rendering the template. You will have to use RequestContext for this to work.

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

Sidebar

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.