0

I've already got a huge index.html that draws from things defined in my index controller. I have another page that does some processing and if it is successful, I would like to render index.html again, but with an added data letting my view know that status was successful. However, I also need all the information of context to show up in the view to show up. Without repeating context into def process(request): what's a good way to let my data dictionary pass into index.html? Thanks

def index(request):
    context = RequestContext(request)
    context['something'] = 'something'
    # much much more
    return render_to_response('index.html', context)

def process(request):
    data['status'] = 'success'
    return ??? ('index.html', context, data?)
pyramidface
  • 1,207
  • 2
  • 17
  • 39

2 Answers2

0

One simple solution could be to add some data to your session and then redirect back to your index view:

from django.shortcuts import redirect

def index(request):
    context = RequestContext(request)
    context['something'] = 'something'
    # much much more

    if 'success' in request.session:
        context['success'] = request.session['success']
        del(request.session['success'])

    return render_to_response('index.html', context)

def process(request):
    request.session['status'] = 'success'
    return redirect("name-of-index-view")

EDIT FROM OP: In my case, I only want the status to be saved once instead of lasting throughout the entire session, so inside def index I put:

try:
    context["status"] = request.session['status']
    del request.session['status']
except KeyError:
    context["status"] = 'fail'
pyramidface
  • 1,207
  • 2
  • 17
  • 39
Scott Harris
  • 319
  • 1
  • 5
0

Extract the data generation to a function and call this function from both views:

def _get_context(request):
    context = RequestContext(request)
    context['something'] = 'something'
    # much much more
    return context

def index(request):
    return render_to_response('index.html', {}, _get_context(request))

def process(request):
    data['status'] = 'success'
    return render_to_response('index.html', {'status': 'success'},
                                            _get_context(request))

Side note: consider to use render instead of render_to_response.

catavaran
  • 44,703
  • 8
  • 98
  • 85