当我尝试使用Django运行服务器时获取TypeError?

时间:2016-09-06 05:13:38

标签: python django

我发现的问题与我提出的问题很接近,但我找到的解决方案对我没用。使用django版本1.10.1

输出到

  

python manage.py runserver

Performing system checks...

Unhandled exception in thread started by <function wrapper at 0xb6838064>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/sidgupta234/Desktop/Hack/guide/guide/urls.py", line 21, in <module>
    url(r'^$', "hacko.views.home", name='home'),
  File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 85, in url
    raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

urls.py文件

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', "hacko.views.home", name='home'),
    url(r'^index/$', "hacko.views.index", name='index'),
]

views.py文件

from django.shortcuts import render
import requests
from django.http import HttpResponse
import unicodedata
# Create your views here.
def home(request):

    return render(request,"sample.html");

def index(request):
     print "i was called"
     if(request.GET.get('q', '')):
        theUrl="http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&titles="+request.GET.get('q', '')
        r = requests.get(theUrl, auth=('user', 'pass'));
        print r.text
        return HttpResponse(r.text, content_type='hacko/json')
     elif(request.GET.get('p', '')):
        theUrl="https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles="+request.GET.get('p', '')
        r = requests.get(theUrl, auth=('user', 'pass'));
        print r.text
        return HttpResponse(r.text, content_type='hacko/json')
     else:
        key="f7ee811c-3f3d-47b4-ad77-b31fe1831b2f"
        r="http://www.dictionaryapi.com/api/v1/references/thesaurus/xml/"+request.GET.get('r', '')+"?key="+key
        r=requests.get(r);
        r=r.text
        r=unicodedata.normalize('NFKD', r).encode('ascii','ignore')
        return HttpResponse(r,content_type="text/plain")

2 个答案:

答案 0 :(得分:3)

更新您的 urls.py文件,如下所示

from django.conf.urls import url
from django.contrib import admin
from hacko import views #importing views from app

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^index/$', views.index, name='index'),
]

代表reference。并遵循与您的django version匹配的正确文章或教程。它总是很好地遵循Django官方网站。

答案 1 :(得分:0)

在1.10中,你不能再将导入路径传递给url(),你需要传递实际的视图函数:

from django.conf.urls import url
from django.contrib import admin
from hacko import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^index/$', views.index, name='index'),
]