1

First: My database is Mysql,and the table has exists,it been used for php,all the time fileds'type is unix_timestamp.

the query result return an unix timestamp string. how to convert the unix timestamp to datetime in django's templete?

second:

about regex,

my code here:

import re
pattern=re.compile("^\d+$")
if pattern.match(50):
  print 1
else:
  print 0

but it show me "TypeError" why ?

Thanks!

My english is very pool~! I'm sorry

0

1 Answer 1

8

Second:

import re
pattern=re.compile(r"^\d+$")
if pattern.match(u"50"):
    print 1
else:
    print 0

First:

I can offer a custom template filter, which converts timestamp to python datetime object:

@register.filter(name='fromunix')
def fromunix(value):
    return datetime.datetime.fromtimestamp(int(value))

Template:

{{ obj.unixtime|fromunix|date:"F" }}}

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much~! I love you !o(∩_∩)o!
if like this cid=request.GET.get("cid",1) if pattern.match(cid): print 1 else: print 0 and what should i do ?thanks
Do not validate url in views, instead write correct regexp in urls.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.