0

I have simple code that I would like to import in each of my views:

           form = DocumentForm()
           user= request.user
           test1 = request.session.get('test1')
           ....
           context += {'form': form, 'test1':test1 ...}

I would like to know if there is a simple way to do this. I saw the doc: https://docs.djangoproject.com/en/1.2/ref/generic-views/

But I'm not sure to get how it works. Thank you for your help.

JojoL
  • 141
  • 1
  • 8
  • Personnaly, I create a model's method or create a file named `myapp/defs.py` where I write my methods. – Zulu Nov 26 '12 at 19:27

2 Answers2

0

this would be one of the simplest ways:

def run_this_in_every_view(request) :
    form = DocumentForm()
    user= request.user
    test1 = request.session.get('test1')
    ....
    return {'form': form, 'test1':test1 ...}

def my_view(request) :
    context = {}

    ...

    context += run_this_in_every_view(request)
    return render_to_response('template.html', context)

class based views would be the better way probably. this question has a good answer for that: Class views in Django

Community
  • 1
  • 1
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
0

This is exactly what a context processor is for.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895