简体   繁体   中英

Django Cannot Import Name

Whenever I run in terminal server, I'm always getting "cannot import name hours-ahead" even though I defined it and called it.

Here is my views.py

 from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is nose %s. </body></html>" % now
    return HttpResponse(html)

def hours_ahead(request, offset):
    offset = int(offset)
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

and here is my urls.py that is supposed to call it but getting error for hours_ahead

from django.http import HttpResponse
from django.contrib import admin
from mysite.views import current_datetime, hours_ahead

urlpatterns = patterns('',
        (r'^time/$', current_datetime),
        (r'^time/plus/(\d{1,2})/$', hours_ahead),
)

Try this.

from django.conf.urls import include, url
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^time/$', views.current_datetime),
    url(r'^time/plus/(\d{1,2})/$', views.hours_ahead)
]

If that doesn't work. Please add the error stack to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM