standalone script to call a view

121 views
Skip to first unread message

Larry Martell

unread,
Jun 28, 2013, 8:41:57 AM6/28/13
to django...@googlegroups.com
I am trying to write a standalone script that calls one of my views. I
can call the view, but I'm having a problem with proving what it
wants. One of the things the view needs is request.user, and that
looks like this:

(Pdb) type(request.user)
<class 'django.utils.functional.SimpleLazyObject'>
(Pdb) type(request)
<class 'django.core.handlers.wsgi.WSGIRequest'>

I'm trying to create the objects it needs by instantiating WSGIRequest
and SimpleLazyObject, but I'm having issues proving what they need,
and I'm just going deeper and deeper down the rabbit hole. Maybe I'm
going about this the wrong way.

Anyone ever done something like this and can give me some advise on
what the best way to achieve this is?

Thanks!
-larry

Sergiy Khohlov

unread,
Jun 28, 2013, 9:12:26 AM6/28/13
to django-users
View is used for serving  web requests not  for  serving bash script .  Ok you have called a view and receive a http page. Ia it expected result?
 I think more  easy way is create a function  which is be called by view and your script.

Many thanks,

Serge


+380 636150445
skype: skhohlov


-larry

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.



Larry Martell

unread,
Jun 28, 2013, 9:21:48 AM6/28/13
to django...@googlegroups.com
On Fri, Jun 28, 2013 at 7:12 AM, Sergiy Khohlov <skho...@gmail.com> wrote:
> View is used for serving web requests not for serving bash script . Ok
> you have called a view and receive a http page. Ia it expected result?
> I think more easy way is create a function which is be called by view and
> your script.

I don't really understand what you mean by "create a function which is
be called by view and your script." (Also, it's a python script not a
bash script.)

I understand how the views are normally used. In this case the view,
is normally called from a web page. It extracts data from the db and
downloads a CSV file. I want to do that same thing, except from a
script, not from the web page. Perhaps I have to go further down the
chain, and instead of calling the view, invoke what eventually gets
called. It's just that the view returns exactly what I need and if I
go any further down I'll have to duplicate some existing code, which
I'd rather not have to do.

Javier Guerra Giraldez

unread,
Jun 28, 2013, 9:47:43 AM6/28/13
to django...@googlegroups.com
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

Sergiy Khohlov

unread,
Jun 28, 2013, 10:00:49 AM6/28/13
to django-users
for this case you can use  django shell ....

Many thanks,

Serge


+380 636150445
skype: skhohlov


Larry Martell

unread,
Jun 28, 2013, 10:53:05 AM6/28/13
to django...@googlegroups.com
Thanks for the reply. I got this going. I ended up instantiating the
class that actually does the db pull, and initializing and calling
that.
Reply all
Reply to author
Forward
0 new messages