On Jan 20, 2008 11:58 PM, Dennis <[EMAIL PROTECTED]> wrote: > > This works from the standpoint of getting back to the "process.html" > template with the new Context object selected on the original form. > However, since this is using render_to_response instead of a redirect, > the URL on the user's browser still holds the "create_context" > location. So, when the user hits "submit" on the process.html page, > the form gets sent back to the "create_context" view and things don't > operate as expected (another context gets created rather than > submitting to the "process_task" view) > > The ideal way to do this would be to HttpResponseRedirect in place of > the "render_to_response" but this doesn't allow me to pass the form > object or any kind of dictionary representing the initial values. Is > there a way to do this? Or am I going about this in the wrong manner? >
Hi Dennis, You are right that the ideal way would be to return an HttpResponseRedirect, and that's what I would do too. You can pass the id of the created context object as a parameter in the request back to the 'process_task' view. For example, your create_context view would look like this: def create_context( request ): if request.method == 'POST': form = CreateContextForm(request.POST) if form.is_valid(): # Do form processing here... # todo: clean_data is legacy, will need to be changed # to cleaned_data in next release. data = form.clean_data c = Context.objects.create( name=data['name'] ) # Pass the context id in the 'newcontext' parameter return HttpResponseRedirect('/process/?newcontext=%s' % c.id) else: form = CreateContextForm() return render_to_response('create_projctx.html', {'form':form}) Then your process_task view would be changed to check if there is a 'newcontext' parameter available in your request, in which case you use it to set the initial data of the form. def process_task(request): if request.method == 'POST': form = ProcessForm(request.POST) if form.is_valid(): # Do form processing here... # todo: clean_data is legacy, will need to be changed # to cleaned_data in next release. data = form.clean_data t = Task.objects.create( context=data['context'] , due_date=data['due_date'] ) return HttpResponseRedirect( request.META['HTTP_REFERER'] ) else: form = ProcessForm(initial={ 'context': request.GET.get('newcontext', None) }) return render_to_response('process.html', {'form': form}) The important part here is this: form = ProcessForm(initial={ 'context': request.GET.get('newcontext', None) }) You are setting an initial context depending on the value of the 'newcontext' request parameter, but because that parameter may not always be present you need to use the .get() method, the first parameter is the key to get from the dictionary and the second parameter is a value to return if the key is not found. In this case, when 'newcontext' is not found you'll get a None value back. After you create a new context on the other view and get redirected to process_task, there should be a value in the 'newcontext' request parameter, and the form will render with that context selected. Also note that this line: return HttpResponseRedirect( request.META['HTTP_REFERER'] ) will cause you problems because after creating a context, the referrer will be the /create_context/ page. Hope this helps. Regards, - Jorge --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---