Re: Getting the path of a handler method for a redirect
"Joel Rivera" <[email protected]> Fri, 20 Dec 2019 17:54:18 -0600
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
--34c22d02d95b4209ab278c445c1b09ef
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
You can use the cherrypy.url <https://docs.cherrypy.org/en/latest/pkg/cher=
rypy.html#cherrypy.url>function, this should do it:
> raise cherrypy.HTTPRedirect(cherrypy.url('.'))=20
--
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 may=
be 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 pat=
h relative to my handler object. This is an example:
>=20
> #!/usr/bin/env python3
>=20
> import os
> import os.path
>=20
> import cherrypy
> from cherrypy.lib import static
>=20
> localDir =3D os.path.dirname(__file__)
> absDir =3D os.path.join(os.getcwd(), localDir)
>=20
>=20
> class PhotoUploader(object):
> UPLOAD_MSG_KEY =3D 'upload_message'
> def __init__(self, upload_dir):
> self._upload_dir =3D upload_dir
>=20
> @cherrypy.expose
> def index(self):
> upload_message =3D cherrypy.session[self.UPLOAD_MSG_KEY] if self.UPLOAD_M=
SG_KEY in cherrypy.session else ''
> msg =3D """
> <html>
> <body>
> <p>%s</p>
> <h2>Upload New Photos</h2>
> <form action=3D"upload" method=3D"post" enctype=3D"multipart/form-data">
> <input value=3D"Select Photos" type=3D"file" name=3D"myFiles" multiple/><=
br />
> <input value=3D"Upload" type=3D"submit" />
> </form>
> </body>
> </html>
> """ % upload_message
> if self.UPLOAD_MSG_KEY in cherrypy.session:
> del cherrypy.session[self.UPLOAD_MSG_KEY]
> return msg
>=20
> @cherrypy.expose
> def upload(self, myFiles):
> files_sent =3D len(myFiles)
> files_uploaded =3D 0
>=20
> for myFile in myFiles:
> upload_path =3D os.path.join(self._upload_dir, myFile.filename)
> with open(upload_path, 'wb') as output_file:
> size =3D 0
> while True:
> data =3D myFile.file.read(8192)
> if not data:
> break
> size +=3D len(data)
> output_file.write(data)
>=20
> files_uploaded +=3D 1
>=20
> cherrypy.session[self.UPLOAD_MSG_KEY] =3D '%u of %u files successfully up=
loaded' % (files_sent, files_uploaded)
> print(dir(cherrypy))
> raise cherrypy.HTTPRedirect('/files') <<=3D=3D I don't want to have to sp=
ecify this path. I want it to be relative to the
> PhotoUploader instance so I can change it's mountpoint without having to =
change this
>=20
> if __name__ =3D=3D '__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 =3D {
> '/': {
> 'tools.sessions.on': True
> }
> }
> cherrypy.quickstart(PhotoUploader(absDir), '/files', conf)
>=20
> --
> 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/msgi=
d/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com <h=
ttps://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064=
356bc8655%40googlegroups.com?utm_medium=3Demail&utm_source=3Dfooter>.
--=20
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 e=
mail to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion on the web visit https://groups.google.com/d/msgid/=
cherrypy-users/2200bce5-a309-408b-88a4-0d1f40a1e9ad%40www.fastmail.com.
--34c22d02d95b4209ab278c445c1b09ef
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html><html><head><title></title><style type=3D"text/css">p.MsoNor=
mal,p.MsoNoSpacing{margin:0}
p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>You can use t=
he<a href=3D"https://docs.cherrypy.org/en/latest/pkg/cherrypy.html#cherrypy=
.url"> cherrypy.url </a>function, this should do it:<br></div><div><br></di=
v><blockquote id=3D"qt" type=3D"cite"><div dir=3D"ltr"><div style=3D"color:=
rgb(212, 212, 212);background-color:rgb(30, 30, 30);font-family:"Droid=
Sans Mono", monospace, monospace, "Droid Sans Fallback";fon=
t-size:14px;line-height:19px;white-space:pre;"><div><span style=3D"color:rg=
b(197, 134, 192)" class=3D"colour">raise</span> cherrypy.HTTPRedirect(cherr=
ypy.url('.')) <br></div></div></div></blockquote><div id=3D"sig94469888"><d=
iv class=3D"signature"><br></div><div class=3D"signature">--<br></div><div =
class=3D"signature">Joel Rivera<br></div><div><br></div></div><div><br></di=
v><div>On Fri, Dec 20, 2019, at 2:05 PM, Jonathan Ludwig wrote:<br></div><b=
lockquote id=3D"qt" type=3D"cite"><div dir=3D"ltr"><div>It seems like I sho=
uld be able to find my answer on the Internet, but maybe I don't what to se=
arch 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 hand=
ler 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:"Droi=
d Sans Mono", monospace, monospace, "Droid Sans Fallback";fo=
nt-size:14px;line-height:19px;white-space:pre;"><div><span style=3D"color:r=
gb(106, 153, 85)" class=3D"colour">#!/usr/bin/env python3</span><br></div><=
div><br></div><div><span style=3D"color:rgb(197, 134, 192)" class=3D"colour=
">import</span> os<br></div><div><span style=3D"color:rgb(197, 134, 192)" c=
lass=3D"colour">import</span> os.path<br></div><div><br></div><div><span st=
yle=3D"color:rgb(197, 134, 192)" class=3D"colour">import</span> cherrypy<br=
></div><div><span style=3D"color:rgb(197, 134, 192)" class=3D"colour">from<=
/span> cherrypy.lib <span style=3D"color:rgb(197, 134, 192)" class=3D"colou=
r">import</span> static<br></div><div><br></div><div>localDir =3D os.path.d=
irname(<span style=3D"color:rgb(156, 220, 254)" class=3D"colour">__file__</=
span>)<br></div><div>absDir =3D os.path.join(os.getcwd(), localDir)<br></di=
v><div><br></div><div><br></div><div><span style=3D"color:rgb(86, 156, 214)=
" class=3D"colour">class</span> <span style=3D"color:rgb(78, 201, 176)" cla=
ss=3D"colour">PhotoUploader</span>(<span style=3D"color:rgb(78, 201, 176)" =
class=3D"colour">object</span>):<br></div><div>UPLOAD_MSG_KEY =3D <span sty=
le=3D"color:rgb(206, 145, 120)" class=3D"colour">'upload_message'</span><br=
></div><div><span style=3D"color:rgb(86, 156, 214)" class=3D"colour">def</s=
pan> <span style=3D"color:rgb(220, 220, 170)" class=3D"colour">__init__</sp=
an>(<span style=3D"color:rgb(156, 220, 254)" class=3D"colour">self</span>, =
<span style=3D"color:rgb(156, 220, 254)" class=3D"colour">upload_dir</span>=
):<br></div><div><span style=3D"color:rgb(86, 156, 214)" class=3D"colour">s=
elf</span>._upload_dir =3D upload_dir<br></div><div><br></div><div><span st=
yle=3D"color:rgb(220, 220, 170)" class=3D"colour">@cherrypy.expose</span><b=
r></div><div><span style=3D"color:rgb(86, 156, 214)" class=3D"colour">def</=
span> <span style=3D"color:rgb(220, 220, 170)" class=3D"colour">index</span=
>(<span style=3D"color:rgb(156, 220, 254)" class=3D"colour">self</span>):<b=
r></div><div>upload_message =3D cherrypy.session[<span style=3D"color:rgb(8=
6, 156, 214)" class=3D"colour">self</span>.UPLOAD_MSG_KEY] <span style=3D"c=
olor:rgb(197, 134, 192)" class=3D"colour">if</span> <span style=3D"color:rg=
b(86, 156, 214)" class=3D"colour">self</span>.UPLOAD_MSG_KEY <span style=3D=
"color:rgb(86, 156, 214)" class=3D"colour">in</span> cherrypy.session <span=
style=3D"color:rgb(197, 134, 192)" class=3D"colour">else</span> <span styl=
e=3D"color:rgb(206, 145, 120)" class=3D"colour">''</span><br></div><div>msg=
=3D <span style=3D"color:rgb(206, 145, 120)" class=3D"colour">"""</span><b=
r></div><div><span style=3D"color:rgb(206, 145, 120)" class=3D"colour"><=
html></span><br></div><div><span style=3D"color:rgb(206, 145, 120)" clas=
s=3D"colour"><body></span><br></div><div><span style=3D"color:rgb(206=
, 145, 120)" class=3D"colour"><p></span><span style=3D"color:rgb(86, =
156, 214)" class=3D"colour">%s</span><span style=3D"color:rgb(206, 145, 120=
)" class=3D"colour"></p></span><br></div><div><span style=3D"color:rg=
b(206, 145, 120)" class=3D"colour"><h2>Upload New Photos</h2></=
span><br></div><div><span style=3D"color:rgb(206, 145, 120)" class=3D"colou=
r"><form action=3D"upload" method=3D"post" enctype=3D"multipart/form-dat=
a"></span><br></div><div><span style=3D"color:rgb(206, 145, 120)" class=
=3D"colour"><input value=3D"Select Photos" type=3D"file" name=3D"myFiles=
" multiple/><br /></span><br></div><div><span style=3D"color:rgb(2=
06, 145, 120)" class=3D"colour"><input value=3D"Upload" type=3D"submit" =
/></span><br></div><div><span style=3D"color:rgb(206, 145, 120)" class=
=3D"colour"></form></span><br></div><div><span style=3D"color:rgb(206=
, 145, 120)" class=3D"colour"></body></span><br></div><div><span styl=
e=3D"color:rgb(206, 145, 120)" class=3D"colour"></html></span><br></d=
iv><div><span style=3D"color:rgb(206, 145, 120)" class=3D"colour">"""</span=
> % upload_message<br></div><div><span style=3D"color:rgb(197, 134, 192)" c=
lass=3D"colour">if</span> <span style=3D"color:rgb(86, 156, 214)" class=3D"=
colour">self</span>.UPLOAD_MSG_KEY <span style=3D"color:rgb(86, 156, 214)" =
class=3D"colour">in</span> cherrypy.session:<br></div><div><span style=3D"c=
olor:rgb(197, 134, 192)" class=3D"colour">del</span> cherrypy.session[<span=
style=3D"color:rgb(86, 156, 214)" class=3D"colour">self</span>.UPLOAD_MSG_=
KEY]<br></div><div><span style=3D"color:rgb(197, 134, 192)" class=3D"colour=
">return</span> msg<br></div><div><br></div><div><span style=3D"color:rgb(2=
20, 220, 170)" class=3D"colour">@cherrypy.expose</span><br></div><div><span=
style=3D"color:rgb(86, 156, 214)" class=3D"colour">def</span> <span style=
=3D"color:rgb(220, 220, 170)" class=3D"colour">upload</span>(<span style=3D=
"color:rgb(156, 220, 254)" class=3D"colour">self</span>, <span style=3D"col=
or:rgb(156, 220, 254)" class=3D"colour">myFiles</span>):<br></div><div>file=
s_sent =3D <span style=3D"color:rgb(220, 220, 170)" class=3D"colour">len</s=
pan>(myFiles)<br></div><div>files_uploaded =3D <span style=3D"color:rgb(181=
, 206, 168)" class=3D"colour">0</span><br></div><div><br></div><div><span s=
tyle=3D"color:rgb(197, 134, 192)" class=3D"colour">for</span> myFile <span =
style=3D"color:rgb(86, 156, 214)" class=3D"colour">in</span> myFiles:<br></=
div><div>upload_path =3D os.path.join(<span style=3D"color:rgb(86, 156, 214=
)" class=3D"colour">self</span>._upload_dir, myFile.filename)<br></div><div=
><span style=3D"color:rgb(197, 134, 192)" class=3D"colour">with</span> <spa=
n style=3D"color:rgb(220, 220, 170)" class=3D"colour">open</span>(upload_pa=
th, <span style=3D"color:rgb(206, 145, 120)" class=3D"colour">'wb'</span>) =
<span style=3D"color:rgb(197, 134, 192)" class=3D"colour">as</span> output_=
file:<br></div><div>size =3D <span style=3D"color:rgb(181, 206, 168)" class=
=3D"colour">0</span><br></div><div><span style=3D"color:rgb(197, 134, 192)"=
class=3D"colour">while</span> <span style=3D"color:rgb(86, 156, 214)" clas=
s=3D"colour">True</span>:<br></div><div>data =3D myFile.file.read(<span sty=
le=3D"color:rgb(181, 206, 168)" class=3D"colour">8192</span>)<br></div><div=
><span style=3D"color:rgb(197, 134, 192)" class=3D"colour">if</span> <span =
style=3D"color:rgb(86, 156, 214)" class=3D"colour">not</span> data:<br></di=
v><div><span style=3D"color:rgb(197, 134, 192)" class=3D"colour">break</spa=
n><br></div><div>size +=3D <span style=3D"color:rgb(220, 220, 170)" class=
=3D"colour">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)" class=3D"colour">1</span><br></div><div><br></div><div>cherrypy.sess=
ion[<span style=3D"color:rgb(86, 156, 214)" class=3D"colour">self</span>.UP=
LOAD_MSG_KEY] =3D <span style=3D"color:rgb(206, 145, 120)" class=3D"colour"=
>'</span><span style=3D"color:rgb(86, 156, 214)" class=3D"colour">%u</span>=
<span style=3D"color:rgb(206, 145, 120)" class=3D"colour"> of </span><span =
style=3D"color:rgb(86, 156, 214)" class=3D"colour">%u</span><span style=3D"=
color:rgb(206, 145, 120)" class=3D"colour"> files successfully uploaded'</s=
pan> % (files_sent, files_uploaded)<br></div><div><span style=3D"color:rgb(=
220, 220, 170)" class=3D"colour">print</span>(<span style=3D"color:rgb(220,=
220, 170)" class=3D"colour">dir</span>(cherrypy))<br></div><div><span styl=
e=3D"color:rgb(197, 134, 192)" class=3D"colour">raise</span> cherrypy.HTTPR=
edirect(<span style=3D"color:rgb(206, 145, 120)" class=3D"colour">'/files'<=
/span>) <<=3D=3D I don't want to have to specify this path. I want i=
t to be relative to the<br></div><div>P<span style=3D"color:rgb(78, 201, 17=
6)" class=3D"colour">hotoUploader instance so I can change it's mountpoint =
without having to change this</span><br></div><div><br></div><div><span sty=
le=3D"color:rgb(197, 134, 192)" class=3D"colour">if</span> <span style=3D"c=
olor:rgb(156, 220, 254)" class=3D"colour">__name__</span> =3D=3D <span styl=
e=3D"color:rgb(206, 145, 120)" class=3D"colour">'__main__'</span>:<br></div=
><div><span style=3D"color:rgb(106, 153, 85)" class=3D"colour"># CherryPy a=
lways starts with app.root when trying to map request URIs</span><br></div>=
<div><span style=3D"color:rgb(106, 153, 85)" class=3D"colour"># to objects,=
so we need to mount a request handler root. A request</span><br></div><div=
><span style=3D"color:rgb(106, 153, 85)" class=3D"colour"># to '/' will be =
mapped to HelloWorld().index().</span><br></div><div>conf =3D {<br></div><d=
iv><span style=3D"color:rgb(206, 145, 120)" class=3D"colour">'/'</span>: {<=
br></div><div><span style=3D"color:rgb(206, 145, 120)" class=3D"colour">'to=
ols.sessions.on'</span>: <span style=3D"color:rgb(86, 156, 214)" class=3D"c=
olour">True</span><br></div><div>}<br></div><div>}<br></div><div>cherrypy.q=
uickstart(PhotoUploader(absDir), <span style=3D"color:rgb(206, 145, 120)" c=
lass=3D"colour">'/files'</span>, conf)<br></div></div></div><p><br></p><div=
>--<br></div><div>You received this message because you are subscribed to t=
he Google Groups "cherrypy-users" group.<br></div><div>To unsubscribe from =
this group and stop receiving emails from it, send an email to <a href=3D"m=
ailto:cherrypy-users+unsubscribe-/[email protected]">cherrypy-users+unsubscri=
be-/[email protected]</a>.<br></div><div>To view this discussion on the web v=
isit <a href=3D"https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8=
ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium=3Demail&utm_so=
urce=3Dfooter">https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8a=
e8-4c0f-bab6-064356bc8655%40googlegroups.com</a>.<br></div></blockquote><di=
v><br></div></body></html>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users" 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/2200bce5-a309-408b-88a4-0d1f40a1e9ad%40www.fastma=
il.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/2200bce5-a309-408b-88a4-0d1f40a1e9ad%40www.fastmail.co=
m</a>.<br />
--34c22d02d95b4209ab278c445c1b09ef--