[Twisted-web] newbiew question

Duncan McGreggor duncan.mcgreggor at gmail.com
Tue Jul 22 20:02:38 EDT 2008


On Tue, Jul 22, 2008 at 3:47 PM, arun chhetri <chhetriarun84 at gmail.com> wrote:
> from twisted.web import resource, server
> from twisted.web import http
>
> class HomePage(resource.Resource):
>     def __init__(self):
>         resource.Resource.__init__(self)
>         self.putChild('calendar',Calendar(user=None,pswd=None,server=None))
>
>     def render(self,request):
>         return"""<html><body>
>                 <a href = calendar>Here is the solution
>                 <body>
>             </html>"""
>     def getChild(self,path,request):
>         return
> Calendar(request.args["user"],request.args["pswd"],request.args["server"])
>
> class Calendar(resource.Resource):
>     def __init__(self,user,pswd,server):
>         resource.Resource.__init__(self)
>         self.user = user
>         self.pswd = pswd
>         self.server = server
>         self.putChild('month',Month(user))
>
>     def render(self,request):
>         return"""<p> The user is %s
>     <a href=/calendar/month> The link to the month is this
>     """%self.user[0]
>
>     def getChild(self,path,request):
>         return Month(self.user[0])
>
> class Month(resource.Resource):
>     def __init__(self,user):
>         resource.Resource.__init__(self)
>         self.user = user
>     def render(self,request):
>         return """ %s"""%self.user
>
> if __name__=="__main__":
>     from twisted.internet import reactor
>     root = HomePage()
>     site = server.Site(root)
>     reactor.listenTCP(8000,site)
>     reactor.run()
>
> now if i go to http://localhost:8000/?user=arun&pswd=test&server=test
> i get this   The user is arun The link to the month is this
> and if I click the link then I get    None
>
> Can anybody help me how I can redirect the value self.user to Month so that
> instead of None it shows the user name provided in query string
>
> Thanks in advance

Hey Arun,

The problem is that you're mixing two different patterns: an object
store (pre-created hierarchy of parents/children) and dynamically
generated children.

The latter is the easiest case to talk about, since it's the classic
HTTP GET where you pass all the arguments you need for any given
request. If that's what you want to do, then your application needs to
add query parameters to every link that it renders.

The quickest way to do that with your example code and without
changing anything would be to change Calendar.render to the following:

    def render(self,request):
        return """
            <p> The user is %s
            <a href=/calendar/month?user=%s&pswd=%s&server=%s>The
            link to the month is this """ % (
            self.user[0], self.user[0], self.pswd[0], self.server[0])

and remove your HomePage.__init__ method.

The first option is what I'd guess you were originally aiming for.
This is a very different pattern: you want an object to be created
with appropriate data stored on attributes, and you want to be able to
refer to those objects and the values of their attributes in the
future, not create them on the fly. There are probably some quick
hacks that could demonstrate how to do this, but as soon as you tried
to do anything remotely real with those hacks, you'd be in trouble. If
this is really what you want, then you should spend some time getting
to know Axiom.

Hope that helps. If not, feel free to ask more questions,

d



More information about the Twisted-web mailing list