Re: Getting the path of a handler method for a redirect

Jonathan Ludwig <[email protected]> Fri, 20 Dec 2019 16:46:38 -0800 (PST)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
------=_Part_367_450312090.1576889198882
Content-Type: multipart/alternative; 
	boundary="----=_Part_368_3116610.1576889198883"

------=_Part_368_3116610.1576889198883
Content-Type: text/plain; charset="UTF-8"

Thanks. That's what I was looking for.

On Friday, December 20, 2019 at 4:54:41 PM UTC-7, Joel Rivera wrote:
>
> You can use the cherrypy.url 
> <https://docs.cherrypy.org/en/latest/pkg/cherrypy.html#cherrypy.url>function, 
> this should do it:
>
> raise cherrypy.HTTPRedirect(cherrypy.url('.')) 
>
>
> --
> Joel Rivera
>
>
> On Fri, Dec 20, 2019, at 2:05 PM, Jonathan Ludwig wrote:
>
> It seems like I should be able to find my answer on the Internet, but 
> maybe I don't what to search for. I have a simple form and I want to 
> redirect to another page after is has been submitted. I want to be able to 
> use a path relative to my handler object. This is an example:
>
> #!/usr/bin/env python3
>
> import os
> import os.path
>
> import cherrypy
> from cherrypy.lib import static
>
> localDir = os.path.dirname(__file__)
> absDir = os.path.join(os.getcwd(), localDir)
>
>
> class PhotoUploader(object):
> UPLOAD_MSG_KEY = 'upload_message'
> def __init__(self, upload_dir):
> self._upload_dir = upload_dir
>
> @cherrypy.expose
> def index(self):
> upload_message = cherrypy.session[self.UPLOAD_MSG_KEY] if self.UPLOAD_MSG_KEY 
> in cherrypy.session else ''
> msg = """
> <html>
> <body>
> <p>%s</p>
> <h2>Upload New Photos</h2>
> <form action="upload" method="post" enctype="multipart/form-data">
> <input value="Select Photos" type="file" name="myFiles" multiple/><br />
> <input value="Upload" type="submit" />
> </form>
> </body>
> </html>
> """ % upload_message
> if self.UPLOAD_MSG_KEY in cherrypy.session:
> del cherrypy.session[self.UPLOAD_MSG_KEY]
> return msg
>
> @cherrypy.expose
> def upload(self, myFiles):
> files_sent = len(myFiles)
> files_uploaded = 0
>
> for myFile in myFiles:
> upload_path = os.path.join(self._upload_dir, myFile.filename)
> with open(upload_path, 'wb') as output_file:
> size = 0
> while True:
> data = myFile.file.read(8192)
> if not data:
> break
> size += len(data)
> output_file.write(data)
>
> files_uploaded += 1
>
> cherrypy.session[self.UPLOAD_MSG_KEY] = '%u of %u files successfully 
> uploaded' % (files_sent, files_uploaded)
> print(dir(cherrypy))
> raise cherrypy.HTTPRedirect('/files') <<== I don't want to have to 
> specify this path. I want it to be relative to the
> PhotoUploader instance so I can change it's mountpoint without having to 
> change this
>
> if __name__ == '__main__':
> # CherryPy always starts with app.root when trying to map request URIs
> # to objects, so we need to mount a request handler root. A request
> # to '/' will be mapped to HelloWorld().index().
> conf = {
> '/': {
> 'tools.sessions.on': True
> }
> }
> cherrypy.quickstart(PhotoUploader(absDir), '/files', conf)
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "cherrypy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to cherryp...-/[email protected] <javascript:>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com 
> <https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/29c41462-9ded-4c37-8925-c87ca3af3530%40googlegroups.com.

------=_Part_368_3116610.1576889198883
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thanks. That&#39;s what I was looking for.<br><br>On Frida=
y, December 20, 2019 at 4:54:41 PM UTC-7, Joel Rivera wrote:<blockquote cla=
ss=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #=
ccc solid;padding-left: 1ex;"><div><div>You can use the<a href=3D"https://d=
ocs.cherrypy.org/en/latest/pkg/cherrypy.html#cherrypy.url" target=3D"_blank=
" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;https://www.google.com/u=
rl?q\x3dhttps%3A%2F%2Fdocs.cherrypy.org%2Fen%2Flatest%2Fpkg%2Fcherrypy.html=
%23cherrypy.url\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG053Stwn8cCQ-ULQXnX=
4LGoawzkw&#39;;return true;" onclick=3D"this.href=3D&#39;https://www.google=
.com/url?q\x3dhttps%3A%2F%2Fdocs.cherrypy.org%2Fen%2Flatest%2Fpkg%2Fcherryp=
y.html%23cherrypy.url\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG053Stwn8cCQ-=
ULQXnX4LGoawzkw&#39;;return true;"> cherrypy.url </a>function, this should =
do it:<br></div><div><br></div><blockquote type=3D"cite"><div dir=3D"ltr"><=
div style=3D"color:rgb(212,212,212);background-color:rgb(30,30,30);font-fam=
ily:&quot;Droid Sans Mono&quot;,monospace,monospace,&quot;Droid Sans Fallba=
ck&quot;;font-size:14px;line-height:19px;white-space:pre"><div><span style=
=3D"color:rgb(197,134,192)">raise</span> cherrypy.HTTPRedirect(<wbr>cherryp=
y.url(&#39;.&#39;)) <br></div></div></div></blockquote><div><div><br></div>=
<div>--<br></div><div>Joel Rivera<br></div><div><br></div></div><div><br></=
div><div>On Fri, Dec 20, 2019, at 2:05 PM, Jonathan Ludwig wrote:<br></div>=
<blockquote type=3D"cite"><div dir=3D"ltr"><div>It seems like I should be a=
ble to find my answer on the Internet, but maybe I don&#39;t what to search=
 for. I have a simple form and I want to redirect to another page after is =
has been submitted. I want to be able to use a path relative to my handler =
object. This is an example:<br></div><div><br></div><div style=3D"color:rgb=
(212,212,212);background-color:rgb(30,30,30);font-family:&quot;Droid Sans M=
ono&quot;,monospace,monospace,&quot;Droid Sans Fallback&quot;;font-size:14p=
x;line-height:19px;white-space:pre"><div><span style=3D"color:rgb(106,153,8=
5)">#!/usr/bin/env python3</span><br></div><div><br></div><div><span style=
=3D"color:rgb(197,134,192)">import</span> os<br></div><div><span style=3D"c=
olor:rgb(197,134,192)">import</span> os.path<br></div><div><br></div><div><=
span style=3D"color:rgb(197,134,192)">import</span> cherrypy<br></div><div>=
<span style=3D"color:rgb(197,134,192)">from</span> cherrypy.lib <span style=
=3D"color:rgb(197,134,192)">import</span> static<br></div><div><br></div><d=
iv>localDir =3D os.path.dirname(<span style=3D"color:rgb(156,220,254)">__fi=
le__</span>)<br></div><div>absDir =3D os.path.join(os.getcwd(), localDir)<b=
r></div><div><br></div><div><br></div><div><span style=3D"color:rgb(86,156,=
214)">class</span> <span style=3D"color:rgb(78,201,176)">PhotoUploader</spa=
n>(<span style=3D"color:rgb(78,201,176)">object</span>):<br></div><div>UPLO=
AD_MSG_KEY =3D <span style=3D"color:rgb(206,145,120)">&#39;upload_message&#=
39;</span><br></div><div><span style=3D"color:rgb(86,156,214)">def</span> <=
span style=3D"color:rgb(220,220,170)">__init__</span>(<span style=3D"color:=
rgb(156,220,254)">self</span>, <span style=3D"color:rgb(156,220,254)">uploa=
d_dir</span>):<br></div><div><span style=3D"color:rgb(86,156,214)">self</sp=
an>._upload_dir =3D upload_dir<br></div><div><br></div><div><span style=3D"=
color:rgb(220,220,170)">@cherrypy.expose</span><br></div><div><span style=
=3D"color:rgb(86,156,214)">def</span> <span style=3D"color:rgb(220,220,170)=
">index</span>(<span style=3D"color:rgb(156,220,254)">self</span>):<br></di=
v><div>upload_message =3D cherrypy.session[<span style=3D"color:rgb(86,156,=
214)">self</span>.UPLOAD_<wbr>MSG_KEY] <span style=3D"color:rgb(197,134,192=
)">if</span> <span style=3D"color:rgb(86,156,214)">self</span>.UPLOAD_MSG_K=
EY <span style=3D"color:rgb(86,156,214)">in</span> cherrypy.session <span s=
tyle=3D"color:rgb(197,134,192)">else</span> <span style=3D"color:rgb(206,14=
5,120)">&#39;&#39;</span><br></div><div>msg =3D <span style=3D"color:rgb(20=
6,145,120)">&quot;&quot;&quot;</span><br></div><div><span style=3D"color:rg=
b(206,145,120)">&lt;html&gt;</span><br></div><div><span style=3D"color:rgb(=
206,145,120)">&lt;body&gt;</span><br></div><div><span style=3D"color:rgb(20=
6,145,120)">&lt;p&gt;</span><span style=3D"color:rgb(86,156,214)">%s</span>=
<span style=3D"color:rgb(206,145,120)">&lt;/p&gt;</span><br></div><div><spa=
n style=3D"color:rgb(206,145,120)">&lt;h2&gt;Upload New Photos&lt;/h2&gt;</=
span><br></div><div><span style=3D"color:rgb(206,145,120)">&lt;form action=
=3D&quot;upload&quot; method=3D&quot;post&quot; enctype=3D&quot;multipart/f=
orm-data&quot;&gt;</span><br></div><div><span style=3D"color:rgb(206,145,12=
0)">&lt;input value=3D&quot;Select Photos&quot; type=3D&quot;file&quot; nam=
e=3D&quot;myFiles&quot; multiple/&gt;&lt;br /&gt;</span><br></div><div><spa=
n style=3D"color:rgb(206,145,120)">&lt;input value=3D&quot;Upload&quot; typ=
e=3D&quot;submit&quot; /&gt;</span><br></div><div><span style=3D"color:rgb(=
206,145,120)">&lt;/form&gt;</span><br></div><div><span style=3D"color:rgb(2=
06,145,120)">&lt;/body&gt;</span><br></div><div><span style=3D"color:rgb(20=
6,145,120)">&lt;/html&gt;</span><br></div><div><span style=3D"color:rgb(206=
,145,120)">&quot;&quot;&quot;</span> % upload_message<br></div><div><span s=
tyle=3D"color:rgb(197,134,192)">if</span> <span style=3D"color:rgb(86,156,2=
14)">self</span>.UPLOAD_MSG_KEY <span style=3D"color:rgb(86,156,214)">in</s=
pan> cherrypy.session:<br></div><div><span style=3D"color:rgb(197,134,192)"=
>del</span> cherrypy.session[<span style=3D"color:rgb(86,156,214)">self</sp=
an>.UPLOAD_<wbr>MSG_KEY]<br></div><div><span style=3D"color:rgb(197,134,192=
)">return</span> msg<br></div><div><br></div><div><span style=3D"color:rgb(=
220,220,170)">@cherrypy.expose</span><br></div><div><span style=3D"color:rg=
b(86,156,214)">def</span> <span style=3D"color:rgb(220,220,170)">upload</sp=
an>(<span style=3D"color:rgb(156,220,254)">self</span>, <span style=3D"colo=
r:rgb(156,220,254)">myFiles</span>):<br></div><div>files_sent =3D <span sty=
le=3D"color:rgb(220,220,170)">len</span>(myFiles)<br></div><div>files_uploa=
ded =3D <span style=3D"color:rgb(181,206,168)">0</span><br></div><div><br><=
/div><div><span style=3D"color:rgb(197,134,192)">for</span> myFile <span st=
yle=3D"color:rgb(86,156,214)">in</span> myFiles:<br></div><div>upload_path =
=3D os.path.join(<span style=3D"color:rgb(86,156,214)">self</span>._upload_=
dir, myFile.filename)<br></div><div><span style=3D"color:rgb(197,134,192)">=
with</span> <span style=3D"color:rgb(220,220,170)">open</span>(upload_path,=
 <span style=3D"color:rgb(206,145,120)">&#39;wb&#39;</span>) <span style=3D=
"color:rgb(197,134,192)">as</span> output_file:<br></div><div>size =3D <spa=
n style=3D"color:rgb(181,206,168)">0</span><br></div><div><span style=3D"co=
lor:rgb(197,134,192)">while</span> <span style=3D"color:rgb(86,156,214)">Tr=
ue</span>:<br></div><div>data =3D myFile.file.read(<span style=3D"color:rgb=
(181,206,168)">8192</span>)<br></div><div><span style=3D"color:rgb(197,134,=
192)">if</span> <span style=3D"color:rgb(86,156,214)">not</span> data:<br><=
/div><div><span style=3D"color:rgb(197,134,192)">break</span><br></div><div=
>size +=3D <span style=3D"color:rgb(220,220,170)">len</span>(data)<br></div=
><div>output_file.write(data)<br></div><div><br></div><div>files_uploaded +=
=3D <span style=3D"color:rgb(181,206,168)">1</span><br></div><div><br></div=
><div>cherrypy.session[<span style=3D"color:rgb(86,156,214)">self</span>.UP=
LOAD_<wbr>MSG_KEY] =3D <span style=3D"color:rgb(206,145,120)">&#39;</span><=
span style=3D"color:rgb(86,156,214)">%u</span><span style=3D"color:rgb(206,=
145,120)"> of </span><span style=3D"color:rgb(86,156,214)">%u</span><span s=
tyle=3D"color:rgb(206,145,120)"> files successfully uploaded&#39;</span> % =
(files_sent, files_uploaded)<br></div><div><span style=3D"color:rgb(220,220=
,170)">print</span>(<span style=3D"color:rgb(220,220,170)">dir</span>(cherr=
ypy))<br></div><div><span style=3D"color:rgb(197,134,192)">raise</span> che=
rrypy.HTTPRedirect(<span style=3D"color:rgb(206,145,120)">&#39;/files&#39;<=
/span><wbr>)  &lt;&lt;=3D=3D I don&#39;t want to have to specify this path.=
 I want it to be relative to the<br></div><div>P<span style=3D"color:rgb(78=
,201,176)">hotoUploader instance so I can change it&#39;s mountpoint withou=
t having to change this</span><br></div><div><br></div><div><span style=3D"=
color:rgb(197,134,192)">if</span> <span style=3D"color:rgb(156,220,254)">__=
name__</span> =3D=3D <span style=3D"color:rgb(206,145,120)">&#39;__main__&#=
39;</span>:<br></div><div><span style=3D"color:rgb(106,153,85)"># CherryPy =
always starts with app.root when trying to map request URIs</span><br></div=
><div><span style=3D"color:rgb(106,153,85)"># to objects, so we need to mou=
nt a request handler root. A request</span><br></div><div><span style=3D"co=
lor:rgb(106,153,85)"># to &#39;/&#39; will be mapped to HelloWorld().index(=
).</span><br></div><div>conf =3D {<br></div><div><span style=3D"color:rgb(2=
06,145,120)">&#39;/&#39;</span>: {<br></div><div><span style=3D"color:rgb(2=
06,145,120)">&#39;tools.sessions.on&#39;</span>: <span style=3D"color:rgb(8=
6,156,214)">True</span><br></div><div>}<br></div><div>}<br></div><div>cherr=
ypy.quickstart(<wbr>PhotoUploader(absDir), <span style=3D"color:rgb(206,145=
,120)">&#39;/files&#39;</span>, conf)<br></div></div></div><p><br></p><div>=
--<br></div><div>You received this message because you are subscribed to th=
e Google Groups &quot;cherrypy-users&quot; group.<br></div><div>To unsubscr=
ibe from this group and stop receiving emails from it, send an email to <a =
href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"F7pNkfxPBgA=
J" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;javascript:&#39;;return=
 true;" onclick=3D"this.href=3D&#39;javascript:&#39;;return true;">cherryp.=
..@<wbr>googlegroups.com</a>.<br></div><div>To view this discussion on the =
web visit <a href=3D"https://groups.google.com/d/msgid/cherrypy-users/48c2b=
911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium=3Demail&amp;u=
tm_source=3Dfooter" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.=
href=3D&#39;https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-=
4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium\x3demail\x26utm_source=
\x3dfooter&#39;;return true;" onclick=3D"this.href=3D&#39;https://groups.go=
ogle.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40goog=
legroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"=
>https://groups.google.com/d/<wbr>msgid/cherrypy-users/48c2b911-<wbr>8ae8-4=
c0f-bab6-064356bc8655%<wbr>40googlegroups.com</a>.<br></div></blockquote><d=
iv><br></div></div></blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/29c41462-9ded-4c37-8925-c87ca3af3530%40googlegrou=
ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/29c41462-9ded-4c37-8925-c87ca3af3530%40googlegroups.co=
m</a>.<br />

------=_Part_368_3116610.1576889198883--

------=_Part_367_450312090.1576889198882--