2017-04-19 98 views
0

所以我試圖打開在我看來,使用此代碼從這個JSON文件打印數據:AttributeError的類型的對象有沒有屬性

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

我遇到了應用程序url的問題。我收到一個錯誤,說有一個屬性錯誤,我不知道如何解決它。

因爲我從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' 
+0

可以顯示完整的錯誤.. ?? – shiva

回答

0

有很多事情不對的代碼。

首先,你已經聲明這是一個類,並在URL中使用它,就好像它是一個基於類的視圖,但是你不能從任何實際的視圖類繼承。

同樣重要的是,在一個班級中,您不要在班級上放置代碼;它需要進入一種方法。

此外,pprint將只打印到控制檯,它不會在用戶的瀏覽器中顯示任何內容;你需要創建一個響應並返回它。

+0

如何將其顯示給瀏覽器? –

+1

這個問題太廣泛了。首先回頭閱讀關於如何編寫視圖的文檔,並解決我提到的問題。 –

相關問題