AttributeError类型对象没有属性

时间:2017-04-19 11:31:20

标签: python django

所以我试图在我的视图中使用此代码打开并打印此JSON文件中的数据:

    from django.shortcuts import render
import json

class OnlicarView():
    import json
    from pprint import pprint

    with open('C:/Python34/Scripts/onlicar/mytest/onlicar/static/json/vehicles.json') as data_file:
        data = json.load(data_file)

    pprint(data)

这是我的应用网址:

from django.conf.urls import url
from onlicar import views

urlpatterns = [
    url(r'^onlicar/$', views.OnlicarView.as_view(), name='onlicar_signup'
       )
]

这是我的根网址:

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^onlicar/', include('onlicar.urls'))

我在使用应用网址时遇到问题。我收到一条错误消息,指出存在属性错误,我不知道如何修复它。

因为我从JSON文件获取数据,所以我的模型目前没有任何内容。我的意思是在模型中有什么东西吗?

我的JSON文件现在正在打印,但是如何将其呈现为HTML?

这是错误:

Unhandled exception in thread started by <function check_errors.<locals>.
 at 0x03F9C4B0>
Traceback (most recent call last):
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\autore
", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
mands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
e.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
e.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
y.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\functi
", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\urls\resolve
 line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_m
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\functi
", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\urls\resolve
 line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python34\Scripts\onlicar\lib\importlib\__init__.py", line 109,
ort_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlo
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_re
  File "C:\Python34\Scripts\onlicar\mytest\mytest\urls.py", line 21, in <

    url(r'^onlicar/', include('onlicar.urls'))
  File "C:\Python34\Scripts\onlicar\lib\site-packages\django\conf\urls\__
py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Python34\Scripts\onlicar\lib\importlib\__init__.py", line 109,
ort_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlo
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_re
  File "C:\Python34\Scripts\onlicar\mytest\onlicar\urls.py", line 5, in <

    url(r'^onlicar/$', views.OnlicarView.as_view(), name='onlicar_signup'
AttributeError: type object 'OnlicarView' has no attribute 'as_view'

1 个答案:

答案 0 :(得分:0)

这段代码有很多问题。

首先,您已将此声明为一个类,并在URL中将其用作基于类的视图,但您不会从任何实际的视图类继承。

同样严肃地说,在课堂上你不要把代码放在班级;它需要进入方法。

此外,pprint只会打印到控制台,它不会在用户的浏览器中显示任何内容;你需要创建一个响应并返回它。