社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

新手。。。出了错误求大神解答!

455813474 • 10 年前 • 8020 次点击  

这是views.py

#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
import datetime
import time

def hello(req):
    now= datetime.datetime.now()

    html="<html><body>hello world! It is now %s.</body></html>"% now


    return HttpResponse(html)

def a(req):
    now=time.strftime("H%:M%:S%")
    date=time.strftime("d%/m%/Y%")
    rep = "the time"+now+"the date"+date
    return HttpResponse(rep)

这是urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from hello_world.views import hello,a

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),

    url(r'^hello/$',hello),
    url(r'^a/$',a),

)

以下是输到浏览器中的错误

ValueError at /a/

Invalid format string

Request Method:     GET
Request URL:    http://127.0.0.1:8000/a/
Django Version:     1.7.1
Exception Type:     ValueError
Exception Value:

Invalid format string

Exception Location:     D:\pycharm project\mysite\hello_world\views.py in a, line 16
Python Executable:  D:\Python\python.exe
Python Version:     3.4.2
Python Path:

['D:\\pycharm project\\mysite',
 'D:\\Python\\lib\\site-packages\\django-1.7.1-py3.4.egg',
 'D:\\pycharm project\\mysite',
 'C:\\Windows\\system32\\python34.zip',
 'D:\\Python\\DLLs',
 'D:\\Python\\lib',
 'D:\\Python',
 'D:\\Python\\lib\\site-packages']

Server time:    Thu, 4 Dec 2014 19:02:34 +0800
Traceback Switch to copy-and-paste view

    D:\Python\lib\site-packages\django-1.7.1-py3.4.egg\django\core\handlers\base.py in get_response

                            response = wrapped_callback(request, *callback_args, **callback_kwargs)

        ...
    ▶ Local vars
    D:\pycharm project\mysite\hello_world\views.py in a

            now=time.strftime("H%:M%:S%")

        ...
    ▶ Local vars
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/817
 
8020 次点击  
文章 [ 11 ]  |  最新文章 10 年前
lsabcd
Reply   •   1 楼
lsabcd    10 年前

now=time.strftime("H%:M%:S%") date=time.strftime("d%/m%/Y%") 这两句写错了吧? now=time.strftime("%H:%M:%S",time.localtime()) date=time.strftime("%d/%m/%Y",time.localtime())

DamonXu
Reply   •   2 楼
DamonXu    10 年前

大哥你发帖子怎么传的照片啊

走遍北京的德福小猪-weibo
Reply   •   3 楼
走遍北京的德福小猪-weibo    10 年前

我在我的机器上测试没有问题 ,可能是 python 版本的问题吧。你看一下 time的strptime 方法如何使用 吧。我感觉是你的方法有问题。

455813474
Reply   •   4 楼
455813474    10 年前

@走遍北京的德福小猪-weibo 我复制了你的代码还是错的0.0

走遍北京的德福小猪-weibo
Reply   •   5 楼
走遍北京的德福小猪-weibo    10 年前

python 版本是 2.7.8, 刚才打错了。呵呵

走遍北京的德福小猪-weibo
Reply   •   6 楼
走遍北京的德福小猪-weibo    10 年前

忙公司的事了,你的 方法写的有问题 。 我写了一个

  • _now = str(datetime.now())
  • _ts = time.strptime(_now, '%Y-%m-%d %H:%M:%S.%f')
  • _date = time.strftime('%Y-%m-%d', _ts)
  • rep = "the time : " + _now + " , the date : " + _date return HttpResponse(rep)

我用的是 python 2.7.9 django 1.7,测试没有问题

Py站长
Reply   •   7 楼
Py站长    10 年前

应该是 rep = "the time"+now+"the date"+date 这句话有问题

走遍北京的德福小猪-weibo
Reply   •   8 楼
走遍北京的德福小猪-weibo    10 年前

路径配置没有问题。

455813474
Reply   •   9 楼
455813474    10 年前

@走遍北京的德福小猪-weibo 换了。。没用啊。。主要http://127.0.0.1:8000/hello/的话时能出来效果的,但是http://127.0.0.1:8000/a/就显示这个错误。。我就很纳闷。。我是Django1.7.1 Python3.4.2

455813474
Reply   •   10 楼
455813474    10 年前

换了。。没用啊。。主要http://127.0.0.1:8000/hello/的话时能出来效果的,但是http://127.0.0.1:8000/a/就显示这个错误。。我就很纳闷。。我是Django1.7.1 Python3.4.2

走遍北京的德福小猪-weibo
Reply   •   11 楼
走遍北京的德福小猪-weibo    10 年前

是不是传入的参数有问题 , 你将 def hello(req) 中的 req 换成 request 测试下。