Embarrassing Newb problem

169 views
Skip to first unread message

almacmillan

unread,
Jul 14, 2010, 1:18:24 PM7/14/10
to web.py
Hi there,

My name is AL and I'm completely new to programming and trying to
teach myself Programming & Python at the ripe old age of 35. I
downloaded the web.py basic blog app and used SQLite3 as my db

db = web.database(dbn='sqlite', db='blog.db')

I am getting what I know is probably an embarrassingly easy python
error but I can't find the solution. The error is:


<type 'exceptions.TypeError'> at /
unsupported operand type(s) for -: 'datetime.datetime' and 'unicode'

Python /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
site-packages/web/utils.py in datestr, line 704
Web GET http://0.0.0.0:8080/


Can anyone help?

Kind thanks
AL

Anand Chitipothu

unread,
Jul 14, 2010, 1:25:47 PM7/14/10
to we...@googlegroups.com
2010/7/14 almacmillan <a.w.ma...@gmail.com>:

Looks like problem with sqlite driver.

Can you check if you have sqlite3 or pysqlite2 modules available? You
can test that by running "import sqlite3" and "import pysqlite2" from
python prompt.

Anand

almacmillan

unread,
Jul 14, 2010, 1:32:13 PM7/14/10
to web.py
Hi Anand

I have tested and I have sqlite3 but not pysqlite2

Kind thanks
AL

On Jul 14, 6:25 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> 2010/7/14 almacmillan <a.w.macmil...@gmail.com>:
>
>
>
>
>
> > Hi there,
>
> > My name is AL and I'm completely new to programming and trying to
> > teach myself Programming & Python at the ripe old age of 35. I
> > downloaded the web.py basic blog app and used SQLite3 as my db
>
> > db = web.database(dbn='sqlite', db='blog.db')
>
> > I am getting what I know is probably an embarrassingly easy python
> > error but I can't find the solution. The error is:
>
> > <type 'exceptions.TypeError'> at /
> > unsupported operand type(s) for -: 'datetime.datetime' and 'unicode'
>
> > Python  /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
> > site-packages/web/utils.py in datestr, line 704
> > Web     GEThttp://0.0.0.0:8080/

Anand Chitipothu

unread,
Jul 14, 2010, 1:35:14 PM7/14/10
to we...@googlegroups.com
2010/7/14 almacmillan <a.w.ma...@gmail.com>:

> Hi Anand
>
> I have tested and I have sqlite3 but not pysqlite2

That should be alright then. Can you tell me how you are using datestr function?

almacmillan

unread,
Jul 14, 2010, 2:01:58 PM7/14/10
to web.py
Hi Anand

I'm just trying to get the blog example working: http://webpy.org/src/blog/0.3

Kind thanks
AL

On Jul 14, 6:35 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> 2010/7/14 almacmillan <a.w.macmil...@gmail.com>:
>

almacmillan

unread,
Jul 14, 2010, 3:07:28 PM7/14/10
to web.py
I meant to add. I'm trying to use Sqlite3 because I can't get MySQL
configured properly on my mac

m g

unread,
Jul 14, 2010, 4:04:37 PM7/14/10
to we...@googlegroups.com
I suggest sticking with sqlite3 for development/testing purposes.
Easy to set up. Easy to tear down.

almacmillan

unread,
Jul 14, 2010, 5:13:23 PM7/14/10
to web.py
I've been trying to get the blog example working but getting an error
under sqlite3. See above. Any thoughts?

Oscar Vill

unread,
Sep 27, 2012, 3:58:50 PM9/27/12
to we...@googlegroups.com
Hi,

I had the same problem. This is why it is not working...

>>> import datetime
>>> import web
>>>
>>> datetime.datetime.utcnow() # Returns a datetime object
datetime.datetime(2012, 9, 27, 19, 39, 5, 350000)
>>>
>>> datetime_obj = datetime.datetime.utcnow()
>>>
>>> str(datetime_obj) # What we get back when we query our database
'2012-09-27 19:39:30.756000'
>>>
>>> help(web.datestr) # What datestr expects is a datetime object
Help on function datestr in module web.utils:

datestr(then, now=None)
    Converts a (UTC) datetime object to a nice string representation.
    ...

So the code as is will never work since we are passing web.datestr a string in index.html and view.html. The answer to the problem is to pass it the datetime object it is expecting. One solution (mine...) is as follows.

Edit your blog.py and remove datestr from t_globals

    t_globals = {
        'transform_datestr': model.transform_datestr
    }

Next edit your model.py and add the following function

    def transform_datestr(posted_on):
        datetime_obj = datetime.datetime.strptime(posted_on,'%Y-%m-%d %H:%M:%S.%f')
        return web.datestr(datetime_obj)

Lastly edit index.html and view.html and replace the call to datestr with the following

    view.html
    $def with (post)

    <h1>$post.title</h1>
    <br>$transform_datestr(post.posted_on)<br/>

    $post.content

    index.html
    $def with (posts)

    <h1>Blog posts</h1>

    <ul>
    $for post in posts:
       <li>
            <a href="/view/$post.id">$post.title</a>
            from $transform_datestr(post.posted_on)
            <a href="/edit/$post.id">Edit</a>
       </li>
    </ul>

Hope that works for you!
Reply all
Reply to author
Forward
0 new messages