2

Hello I'm learning Django framework for Web applications

I'm trying to build basic blog from tutorial

and I'm stuck with the error:

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

I don't know where I have problem in my files so I will include here some code of my files.

There is my 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'),
}

There is my views.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]
    })

There is my 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 })

And there is my tracback error

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'
6
  • can you share the stack error pls Commented Mar 22, 2017 at 16:01
  • I think this is a issue of the urls.py, normaly i import the views directly through from . import views and then just call views.view_post Commented Mar 22, 2017 at 16:03
  • Yes, Richy is right. Remove WarblerTest.blog. before views in url, and update your import. Commented Mar 22, 2017 at 16:12
  • 2
    Please include the full traceback in your question, so that we don't need to guess what line is causing the problem. Your urlpatterns should be a list, urlpatterns = [...], not a set urlpatterns = {...}. It looks like your urls.py is your main urls.py, not your blog/urls.py. In that case, you need to do from blog import views, instead of from . import views as @Richy suggested. Commented Mar 22, 2017 at 16:25
  • Updated my post you can check now traceback in my question. Commented Mar 22, 2017 at 17:09

1 Answer 1

1

In your main project urls.py, remove the import WarblerTest import and replace it with from blogs import views.

Then update your url patterns to use the views module that you imported. Note that urlpatterns should be a list (square brackets [...] not curly braces {...} as you have. Every item should be a url(...) rather than a tuple (...) to avoid problems in 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'),
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.