Hi,

I took a look around the django website and the user group here, but
didn't really find an answer to this.

I my application (which does administrative things with a stock) I
have a view that allows creation of items that are stored in the
stock.

The view does different things on different actions: when accessed
without POST data it just shows a form to input the new data. This
form requires some objects (the new item has foreign keys, these need
to be passed to the form) and thus I use a
render_to_response(objects).

If the form is entered and submitted, a post is done to the same view.
When the view detects the post it will read from it and try to create
the items, ending with a httpresponseredirect to this view. (called
new())

There are two arrays that I use throughout the project (included in
the base template from which all inherit) called messages and errors
(passed as error_list and message_list). I use these to notify the
user how many objects were created succesfully, what errors occured
(most common is except error, e: errors.append("error:"%e), etc..

The return_to_response can easily handle this, but
httpresponseredirect can't. However, I have added the arguments to my
view as follows:

def new(request, errors = [], messages = []):
...

This somehow gets the errors and messages to my template after a
httpresponseredirect.
(and also with a render_to_response). That's good news, unfortunatly,
django somehow remembers this and the arrays don't get cleaned up. I
tried several things to clear the arrays, but when used with
httpresponseredirect, they need to reach the render_to_response
statement before being cleared or I can't view the message. (which was
the intention in the first place.)
Also, from what I know, this shouldn't even work as no where I'm
passing arguments to new(), not in the urls.py, not in
render_to_response and certainly not using httpresponseredirect.
So, normal python rules say these should get default values [] and
thus be cleared.

On to the question, what is going on here?
Why is it that the arrays don't get their default value ([]) and how
is it possible that they do contain the errors and messages generated?
All this just doesn't feel right to me and I haven't been able to
figure it out.
Any comments greatly appreciated!

(running postgresql on a seperate linux server and running the code on
my computer (winxp).
Browser = opera, same problems with IE though, django version 0.96,
python 2.5, psycopg2

The code:

def new(request, errors = [], messages = []):
    messages.append("Current total: %d"%len(Stock.objects.all()))
    if(request.POST):
        textarea = request.POST['serial_boxid']
        po_nr = request.POST['po_no']
        if(po_nr!="POnumber"):#use nothing
            npo = PO.objects.get(id=request.POST['po_no'])
            try:
                ndp, exist_dp =
DP.objects.get_or_create(dp_no=int(request.POST['dispatch']),
defaults={'pub_date':date.today()})
            except (TypeError, ValueError), e:
                errors.append("error: %s"%e)
                return HttpResponseRedirect('/stock/new/')
            lines = textarea.splitlines()
            for units in lines:
                line = units.split()
                if(len(line)==2):
                    nstock, exists =
Stock.objects.get_or_create(serialno = line[0], boxid = line[1],
defaults={'location':request.POST['location'], 'po':npo, 'dp':ndp})
                    if(exists):
                        messages.append("created %s"%nstock)
                    else:
                        errors.append("%s already exists!"%nstock)
                else:
                    errors.append("wrong syntax in entry: %s"%line)
            return HttpResponseRedirect('/stock/new/')
        else:
            errors.append("Please specify a PO")
            return render_to_response('stock/stock_new.html',
{'error_list':errors, 'po_list':PO.objects.all(),
'message_list':messages})
    else:
        return render_to_response('stock/stock_new.html',
{'po_list':PO.objects.all(), 'error_list':errors,
'message_list':messages})

Regards,
Maarten

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to