AttributeError:“函数”对象没有属性“视图”

Posted

技术标签:

【中文标题】AttributeError:“函数”对象没有属性“视图”【英文标题】:AttributeError: 'function' object has no attribute 'views' 【发布时间】:2017-08-14 21:00:30 【问题描述】:

你好,我正在学习用于 Web 应用程序的 Django 框架

我正在尝试从教程构建基本博客

我被错误困住了:

AttributeError is telling me that  'function' object has no attribute 'views'.

我不知道我的文件哪里有问题,所以我将在此处包含我的文件的一些代码。

这是我的 urls.py

from django.conf.urls import url
from django.contrib import admin
import WarblerTest


urlpatterns = 
    url(r'^admin/', admin.site.urls),
    (r'^$', WarblerTest.blog.views.index),
    url(
        r'^blog/view/(?P<slug>[^\.]+).html',
        WarblerTest.blog.views.view_post,
        name='view_blog_post'),
    url(
        r'^blog/category/(?P<slug>[^\.]+).html',
        WarblerTest.blog.views.view_category,
        name='view_blog_category'),

有我的意见.py

from django.shortcuts import render
from blog.models import Blog, Category
from django.shortcuts import render_to_response, get_object_or_404


def index(request):
    return render_to_response('index.html', 
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:5]
    )


def view_post(request, slug):
    return render_to_response('view_post.html', 
        'post': get_object_or_404(Blog, slug=slug)
    )


def view_category(request, slug):
    category = get_object_or_404(Category, slug=slug)
    return render_to_response('view_category.html', 
        'category': category,
        'posts': Blog.objects.filter(category=category)[:5]
    )

这是我的models.py

from django.db import models
from django.db.models import permalink

# Create your models here.

class Blog(models.Model):
    title = models.CharField(max_length = 100,unique = True)
    slug = models.CharField(max_length = 100,unique = True)
    body = models.TextField()
    posted = models.DateField(db_index = True,auto_now_add = True)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view-blog-post',None, 'slug' : self.slug)

class Category(models.Model):
    title = models.CharField(max_length = 100,db_index = True)
    slug = models.SlugField(max_length = 100,db_index = True)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_category', None,  'slug': self.slug )

还有我的回溯错误

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03853B28>
Traceback (most recent call last):
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 2
26, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\run
server.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", li
ne 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", li
ne 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\registry.py", li
ne 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 1
4, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 2
4, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\functional.py", line 3
5, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 313
, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\functional.py", line 3
5, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 306
, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Tornike\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_modul
e
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\Tornike\Desktop\WarblerTest\WarblerTest\urls.py", line 8, in <module>
    (r'^$', WarblerTest.blog.views.index),
AttributeError: 'function' object has no attribute 'views'

【问题讨论】:

可以分享一下堆栈错误吗 我认为这是 urls.py 的问题,通常我直接通过from . import views 导入视图,然后调用views.view_post 是的,里奇是对的。在 url 中删除 views 之前的 WarblerTest.blog.,并更新您的导入。 请在您的问题中包含完整的回溯,这样我们就无需猜测是哪一行导致了问题。您的 urlpatterns 应该是一个列表,urlpatterns = [...],而不是一组 urlpatterns = ...。看起来您的urls.py 是您的主要urls.py,而不是您的blog/urls.py。在这种情况下,您需要使用from blog import views,而不是@Richy 建议的from . import views 更新了我的帖子,您现在可以在我的问题中查看回溯。 【参考方案1】:

在您的主项目 urls.py 中,删除 import WarblerTest 导入并将其替换为 from blogs import views

然后更新您的 url 模式以使用您导入的 views 模块。请注意,urlpatterns 应该是 list(方括号 [...] 而不是花括号 ...,因为你有。每个项目都应该是 url(...) 而不是元组 (...) 以避免在 Django 1.10+ 中出现问题。

from blogs import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index),
    url(r'^blog/view/(?P<slug>[^\.]+).html', views.view_post, name='view_blog_post'),
    url(r'^blog/category/(?P<slug>[^\.]+).html', views.view_category, name='view_blog_category'),
]

【讨论】:

以上是关于AttributeError:“函数”对象没有属性“视图”的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:将 sql 函数作为默认参数传递时,“NoneType”对象没有属性“_jvm”

AttributeError:“Tensor”对象在自定义损失函数中没有属性“numpy”(Tensorflow 2.1.0)

AttributeError:“按钮”对象没有属性

AttributeError:“会话”对象没有属性“会话”

AttributeError:“模块”对象没有属性“百分位”

AttributeError:“numpy.ndarray”对象没有属性“列”