16
respostas

problema para retornar a mensagem.

Quando eu tentei fazer com que retorne a mensagem "Bem-vindo ao Connectedin", lá onde está o meu servidor da esses erros:

Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\python\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\python\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python\Scripts\connectedin\connectedin\urls.py", line 21, in <module>
    path('^$', 'perfis.views.index')
  File "C:\python\lib\site-packages\django\urls\conf.py", line 73, in _path
    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().
16 respostas

oi alexandre, você pode mandar por aqui seu código (urls.py e views.py) pra ajudar a identificar o erro?

urls.py

"""connectedin URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'^$', 'perfis.views.index')
]

views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse('Bem-vindo ao Connectedin')

oi alexandre, troque esta parte do código

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'^$', 'perfis.views.index')
]

por

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

veja que é apenas trocar o 'path' por 'url' já que a versão do django utilizada é a 1.7 e avise por aqui se deu certo.

mesmo no import não tendo a url?

boa Alexandre, neste caso precisa importar 'url' também:

from django.conf.urls import url

Agora deu esse erro:

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0398CE88>
Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\python\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\python\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python\Scripts\connectedin\connectedin\urls.py", line 21, in <module>
    url(r'^$', 'perfis.views.index')
  File "C:\python\lib\site-packages\django\conf\urls\__init__.py", line 13, in url
    return re_path(regex, view, kwargs, name)
  File "C:\python\lib\site-packages\django\urls\conf.py", line 73, in _path
    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().

Deve acrescentar o include em alguns dos campos do urls.py?

oi alexandre, desculpe, vai precisar do include sim:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', 'perfis.views.index')
)

esse include seria uma pasta ou arquivo?

o 'include()' é uma função do Django que inclui o um caminho de importação para outro módulo. você pode ver mais detalhes desta função na documentação: https://docs.djangoproject.com/en/1.11/ref/urls/

no fim, os arquivos ficam assim:

connectedin/connectedin/urls.py

# arquivo connectedin/connectedin/urls.py

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

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', 'perfis.views.index')
)

connectedin/perfis/views.py:

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse('Bem-vindo ao Connectedin')

veja se assim para de acusar o erro

agora esta dando problema no patterns

qual problema exatamente, alexandre?

ta dizendo que ele não pode importar o patterns

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x040BCE88>
Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\python\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\python\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python\Scripts\connectedin\connectedin\urls.py", line 17, in <module>
    from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'

qual versão do django você está utilizando?

(2, 0, 3, 'final', 0)

Alexandre, tente mudar essa linha no urls.py:

from django.conf.urls import patterns, include, url

para:

from django.conf.urls import *

Mas, olha, para esse curso é recomendado usar a versão 1.7.4. Existem algumas diferenças dessa versão para a que você está usando que te trarão mais problemas mais pra frente durante o curso.

vou ver se baixo a versão, 1.7.4