Lightster
Lightster

Reputation: 119

error 404 on web.py subapps. How to handle urls?

last time I wrote the answer was fast and efficient, so here I go again.

I got these 3 files, 2 .py and an html. The thing is that for some reason I get an error 404. It's clearly my url handing the issue. Here is my code

# encoding: utf-8

import web
import os
import gettext
import signup
import verify_email

urls = (
        "/", 'Index',
        "/sign-up", signup.app_signup,
        "/verify_email", verify_email.app_verify_email
        )

# Internacionalización:
current_directory = os.path.abspath(os.path.dirname(__file__))
locales_directory = current_directory + '/i18n'
gettext.install('messages', locales_directory, unicode = True)
gettext.translation('messages', locales_directory, languages = ['es']).install(True)

web.config.debug = False
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'))  
db = web.database(dbn='postgres', user='zoosalud', pw='pbF8zCxd4gZUmhRxd8AsjfhN', db='zoosalud')

def session_hook():
    web.ctx.session = session
    web.ctx.db = db
    web.ctx.render = render

app.add_processor(web.loadhook(session_hook))

render = web.template.render('views/', globals = {'session': session, 'db': db, '_': _})

class Index:
    def GET(self):
        user_input = web.input()
        user_input.names = ''
        user_input.surnames = ''
        user_input.nin = ''
        user_input.address = ''
        user_input.municipality = 0
        user_input.phone = ''
        user_input.email = ''
        session.user_input = user_input
        session.errors = []
        return render.access()

if __name__ == "__main__":
    app.run()

here is my sub app

# encoding: utf-8

import bcrypt
import email_setup
import uuid
import web


urls = (
    "/", "SignUp"
    )

class SignUp:

    def POST(self):
        user_input = web.input()

        web.ctx.session.errors = []
        user = web.ctx.db.select('role', where="email='" + user_input.email + "'")
        if user:
            web.ctx.session.errors.append('email_taken')
        user = web.ctx.db.select('role', where="nin='" + user_input.nin + "'")
        if user:
            web.ctx.session.errors.append('nin_taken')
        if user_input.password != user_input.confirm_password:
            web.ctx.session.errors.append('passwords_not_match')
        if user_input.municipality == '0':
            web.ctx.session.errors.append('unselected_municipality')
        if web.ctx.session.errors:
            web.ctx.session.user_input = user_input
            return web.ctx.render.access()


app_signup = web.application(urls, locals())

and my html

<form method=post action="sign-up">
        <ul>
            <li><input name=names required maxlength=24 placeholder=$_('name') value="$session.user_input.names"></li>
        <li><input name=surnames required maxlength=24 placeholder=$_('surname') value="$session.user_input.surnames"></li>
        <li><input name=nin requiered maxlength=12 placeholder=$_('nin') value="$session.user_input.nin"></li>
        <li><input name=address required maxlenght=64 placeholder=$_('address') value="$session.user_input.address"></li>
        <li><select id=municipality name=municipality>
            <option value=0>$_('municipality'):</option>
            $for municipality in municipalities:
                <option value=$municipality.id>$municipality.name</option>
        </select></li>
        <li><input name=phone required maxlength=10 placeholder=$_('phone') value=$session.user_input.phone></li>
        <li><input name=email type=email required maxlenght=254 placeholder=$_('email') value=$session.user_input.email></li>
        <li><input name=password type=password required placeholder=$_('password')></li>
        <li><input name=confirm_password type=password required placeholder=$_('confirm_password')></li>
        <li><input type=submit value="$_('sign_up')"></li>
</ul>

The problem as I mention is the error404 when I map to "/" in the subapp, which is weird because when I map to "" it does work, but now I need to map it so I can get the path with GET, and this solution does not work in the long term.

Thanks in advance guys.

Upvotes: 0

Views: 226

Answers (1)

Andrey Kuzmin
Andrey Kuzmin

Reputation: 4479

You may change subapp mapping to "/?", "SignUp" so it should work with and without trailing slash.

Upvotes: 1

Related Questions