Friday, June 28, 2013

Re: standalone script to call a view

On Fri, Jun 28, 2013 at 8:21 AM, Larry Martell <> 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?)
# 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.


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment