sidgupta234 sidgupta234 - 3 months ago 106
Python Question

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

I found questions which were close to the question asked by me, but the solutions I found there were not useful to me. Using django version 1.10.1

Output to


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 file

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 file

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

Answer

Update your urls.py file like below

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

for your reference. And also follow the correct article or tutorial that match with your django version. Its always good to follow Django official site.

Comments