View code is the python script that is generated for each page and that is run before loading the page template. You write your own code that replaces the one generated for you. This is how this is done.

The view code editor

This editor can be reached from the code editor in the page designer. To get access to writing custom code, click the "show generated view code" icon and a new editor show the generated code for the page. From here you can change to custom code by a drop down and change the generated code to anything you like. It is important to end the view code with a return of a response object. If not the page will result in an error in the web browser.

When you have changed to the custom view code, any changes to tables, dataviews, transfers and dqls in the page properties or live views will be ignored. Now you are in control of anything presented for your page template.

Extra dependencies used by the code

Here you can add any extra libraries that is needed by your code that you want loaded globally in the top of the generated view.py file. Any libraries already loaded by default can be left out. This is just for the ones that not is already loaded by the framework. Any errors will render any view in this application folder none functioning, so make sure that your library exists and are spelled correct.

from django.conf import settings
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from debcore.templates import renderstring 
from debcore.viewtools import readquery, splitgdict, getqueryvalue
#added when authentication is activated
from django.contrib.auth.decorators import login_required, user_passes_test
#added when cache is activated
from django.views.decorators.cache import cache_page<br>

This is the default libraries imported in a view that supports caching and authentication.

Extra decorators used by the code

This is the decorators added as wrappers to you view function. It can be any valid decorator supported by python or that you have added to your extra dependencies.

Custom view code instead of the generated

Here is where you add your own code. You will automatically inherit the standard request parameter form the framework and should return a response object of a kind that is understood by the framework. If you do not want to add your libraries globally to the views.py file, you can add them to the top of your code instead. Then they will only be loaded if the view is called.

from django.core.servers.basehttp import FileWrapper
import os

ctx = {'user' : request.user,}
try:
    pathtodownload = settings.DIRNAME + "\\download\\"
except:    
    pathtodownload = u"you hardcoded test directory"
ctx['pathtodownload']=pathtodownload
try:
    gdict = request.GET
    filename = gdict['filename']
except:
    filename =  u"Client.exe"
ctx['filename']=filename
fp = pathtodownload+filename
if os.path.exists(fp):
    wrapper = FileWrapper(file(fp))
    response = HttpResponse(wrapper, content_type='application/x-msdownload')
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    response['Content-Length'] = os.path.getsize(fp)
    return response
else:
    return render_to_response('downloadfile_detail.html',
                                                  ctx,
                                                  context_instance=RequestContext(request))

Example of a custom view that loads files from a download directory not avaiable unless you are logged in.