import web,re,traceback
urls=(
"/","displaythreads"
"/viewthread","displaythreads",
"/viewthread/(.*)","displaythread",
"/viewthread/(.*)/Vote/(Up|Down)","vote",
"/newthread/(.*)","newthread",
"/newthread","newthread"
)
entries={}###{entryname:{property:value}}
entrylength=0
class displaythreads:
    pass
class newthread:
    template=web.template.frender("newthread.html")
    default=template("DEFAULT",0)
    def initialize():
        pass
    def GET():
        return default
    def POST():
        data = web.input()
        try:
            username,threadname = data.username,data.threadname
        except:
            return template(mode)
        if threadname in entries:
            return template("Exists")
        entries[threadname]={"creator":username,"points":0,"totvotes":0}
class displaythread:
    template=web.template.frender('Thread Viewer Template.html')
    def initialize():
        pass
    def GET(self):
        return self.template(
            entrylength,
            entries.keys(),
            [value['name'] for value in entries.values()]

                      )
class vote:
    template=web.template.frender('vote.html')
    def initialize():
        pass
    def GET(self,thread,vote):
        try:
            current=web.cookies().get("Voted:"+thread)
        except:
            current="BEING DELETED" #act like it is being deleted if its not there.
        ######Set cookies and change votes#########
        ##Removing/Adding an extra vote if it was previously voted the other way##
        if vote=="Up":
            setcookie("Voted:"+thread,"up")
            if current=="down":
                entries[thread]["votes"]+=1
            entries[thread]["votes"]+=1
            id=0
        elif vote=="Down":
            setcookie("Voted:"+thread,"down")
            if current=="up":
                entries[thread]["votes"]-=1
            entries[thread]["votes"]-=1
            id=0
        elif vote=="Remove":
            if current=="up":
                entries[thread]["votes"]-=1
            elif current=="down":
                entries[thread]["votes"]+=1
            setcookie("Voted:"+thread,"BEING DELETED",expires=1)
            id=1
            if current != "BEING DELETED":# if he hasn't already voted, increase the total vote count by 1
                totvotes+=1
        return self.template(vote,id)
#classes = [eval(item) for item,number in enumerate(urls) if number%2==1]
#for i in classes:
    #i.initialize()
app = web.application(urls, globals())
if __name__ == "__main__":
    try:
        app.run()
    except:
        traceback.print_exc()
        input()

web.py latest version from gihub.
Macintosh python 2.7.6 32 bit
It's a forum.
12a0465434da395b02201ddd0667d28f

Dani AI

Generated

Nice catch on the missing colon, . If you are still hitting a KeyError after that, it is almost certainly because you read a field that never exists in your entries records. You create each record with keys like creator, points, and totvotes, but later you try to read a name field. Either store a name when you create the record, or change the read to use an existing key. For example, persist the thread name up front so templates can safely use it and you do not depend on dict key order:

# when creating a thread
entries[threadname] = {
    "name": threadname,
    "creator": username,
    "votes": 0,
    "totvotes": 0,
}

A few other gotchas that can masquerade as unrelated errors:

  • Use web.setcookie(...), not setcookie(...). The latter is undefined and will raise at runtime.
  • Keep your field names consistent. You increment votes in the vote handler, but create points; pick one (votes is clearer) and use it everywhere.
  • totvotes += 1 modifies a local that does not exist. You likely meant entries[thread]["totvotes"] += 1.
  • Handler methods should take self. For example:
    class newthread:
        def GET(self): ...
        def POST(self): ...
  • Make sure every pair in urls is separated by a comma. A missing comma will concatenate adjacent string literals and break routing in subtle ways.
  • entrylength is never updated. If your template needs it, pass len(entries) instead of a stale global.

Tighten those up and the KeyError should disappear, and your vote flow will behave predictably.

Oops! Forgot a colon! Sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.