On Fri, Jun 28, 2013 at 8:21 AM, Larry Martell <
larry....@gmail.com> wrote:
> I don't really understand what you mean by "create a function which is
> be called by view and your script."
AFAICT, your situation looks like this:
you have this:
def myview(request, maybesomeparams...):
.... does lots of things....
return HttpResponse(...templates,...)
now you want that "lot of things" from the command line, but to call
view you must create a request and interpret the response, because the
view function works only within a web request/response cycle.
you can refactor it like this:
def utilityfunction(user, otherparams...):
... does some things (with database? no matter)...
return somepythondata
def myview(request, .....):
# get data
data = utilityfunction(request.user, .....)
# use data to create response
return HttpResponse(... templates...(data)...)
def mycommandlinefunc(...):
# find user (with username?)
user=User.objects.get(username=....)
# get data
data = utilityfunction(user, .....)
# use data to create output
# maybe using a different template
print RenderToString(template, data)
the point is not to call the view function, because it does things in
a very web-specific way, but instead encapsulate whatever is common to
both the view and the commandline operation into a separate function
and use that in both cases.
--
Javier