[Twisted-web] newbiew question

arun chhetri chhetriarun84 at gmail.com
Wed Jul 23 11:41:36 EDT 2008


Hi Duncan Mcgregor,

You have guessed it right, I have posted my problem on wiki,,


bye

On Tue, Jul 22, 2008 at 7:02 PM, Duncan McGreggor <
duncan.mcgreggor at gmail.com> wrote:

> 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=3DNone,pswd=3DNone,server=3DNone))
> >
> >     def render(self,request):
> >         return"""<html><body>
> >                 <a href =3D 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 =3D user
> >         self.pswd =3D pswd
> >         self.server =3D server
> >         self.putChild('month',Month(user))
> >
> >     def render(self,request):
> >         return"""<p> The user is %s
> >     <a href=3D/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 =3D user
> >     def render(self,request):
> >         return """ %s"""%self.user
> >
> > if __name__=3D=3D"__main__":
> >     from twisted.internet import reactor
> >     root =3D HomePage()
> >     site =3D server.Site(root)
> >     reactor.listenTCP(8000,site)
> >     reactor.run()
> >
> > now if i go to http://localhost:8000/?user=3Darun&pswd=3Dtest&server=3D=
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=3D/calendar/month?user=3D%s&pswd=3D%s&server=3D%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
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20080723/6e=
5f3dcf/attachment-0001.htm


More information about the Twisted-web mailing list