繁体   English   中英

当我尝试使用Django运行服务器时出现TypeError吗?

[英]Getting TypeError when I am trying to run server using Django?

我发现的问题与我提出的问题很接近,但是我发现那里的解决方案对我没有用。 使用Django 1.10.1版

输出到

python manage.py运行服务器

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")

如下更新您的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'),
]

参考 并按照与您的django version匹配的正确文章或教程进行操作。 遵循Django官方站点总是很不错的。

在1.10中,您不再可以将导入路径传递给url(),您需要传递实际的view函数:

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'),
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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