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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:18:49+00:00 2026-05-28T07:18:49+00:00

I am new to python and django and i have a question regarding the

  • 0

I am new to python and django and i have a question regarding the redirect function.

This is a reduced version of my views.py file.

def page_index(request, error_message=''):
    print error_message

def add_page(request):
    return redirect('page_index') # this work fine
    return redirect('page_index', error_message='test') # this does not work

And here is a short version of my urls.py

urlpatterns = patterns(
    'x.views',
    url(r'^$', 'page_index', {'error_message': 't'}, name='page_index'),
    url(r'^add/$', 'add_page', name='add_page'),
)

When i try redirecting to page_index without the keyword argument everything works fine, but when i use the kwag i get the following error message:

NoReverseMatch at /pages/add/

Reverse for ‘page_index’ with arguments ‘()’ and keyword arguments ‘{‘error_message’: ‘test’}’ not found.

What am i doing wrong?

  • 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-28T07:18:50+00:00Added an answer on May 28, 2026 at 7:18 am

    Short answer: There is no place in your url scheme for the ‘error_message’ keyword.

    Longer answer:

    The redirect() function is calling reverse() to build a URL; it is going to send the user’s browser to that URL by returning an HTTP response with a 302 redirect status code, and the new url. Any keyword arguments that you supply to reverse() are supposed to end up as part of the url — that’s how they get communicated to the user.

    In your case, though, the url for ‘page_index` is just defined as ‘^$’ — this is the root url, which looks like ‘http://yoursite.com/’ in the browser.

    If you want to be able to issue a redirect that contains other information, you will need to define a place for it in the url, or add it in a different way.

    TwoThree ways are fairly common for this:

    1. Use a query parameter — this sends the message to the client explicitly; if you aren’t careful, people can craft urls to make your index page say whatever they want it to.

      return redirect(reverse('page-index')+"?error_message=test"))
      
    2. Stash the message in the session and pull it out when the next page loads — this requires that you have sessions configured, and keeps track of everything on the server side, rather than relying on the client to send you back the error message:

      def add_page(request):
          request.session['error_message'] = 'test'
          return redirect('page-index')
      
      def page_index(request):
          print request.session.get('error_message','')
      
    3. Use the messages framework for this — this is preferred over ad-hoc session attributes, as long as you don’t need too many ‘types’ of message on the same page. If all you have is a space in your template for error message, though, then this is really easy:

      from django.contrib.messages import error
      
      def add_page(request):
          error(request, 'test')
          return redirect('page-index')
      

      And then in your base template, have a block like this somewhere (probably more complex than this; styled, even):

      {% for message in messages %}
          <p>{{ message }}</p>
      {% endfor %}
      

    In bothall cases, though, you can remove the arguments from your urls.py — the message itself is not going to be part of the path component of the URL.

     urlpatterns = patterns(
        'x.views',
         url(r'^$', 'page_index', name='page_index'),
         url(r'^add/$', 'add_page', name='add_page'),
     )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python and to django so this question will probably be easy
Alright, this is probably a really silly question but I am new to Python/Django
I'm fairly new to both Django and Python. This is my first time using
I have new to python and django I am creating poll application and in
Still new to python and django, though learning ;-) I have a view that
Apologies, I am completely new to Django and Python. I have 2 questions. First,
I'm new to python, django and google app engine. All great tools and have
I am new to Django/Python, so excuse me if my question is straightforwardly obvious!
Apologies for the noobish question, I am completely new to both Python and Django
I am not 100% sure whether this is a Django or a Python question,

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.