Aditya Aryan
Aditya Aryan

Reputation: 59

AttributeError: module 'main.views' has no attribute 'home'

I'm making a django project and whenever I run "python manage.py runserver". I see the above error.

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item

# Create your views here.

def index(response, id):
    ls = ToDoList.objects.get(id=id)
    return render(response, "main/base.html", {})

def home(response):
    return render(response, "main/home.html", {})

main/url.py

from django.urls import path
from main import views
from . import views


urlpatterns = [
path("<int:id>", views.index, name="index"),
path("", views.home, name="home")
]

mysite/url.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("main.urls")),
]

Upvotes: 1

Views: 14625

Answers (4)

ishaj
ishaj

Reputation: 101

have you added the app in the INSTALLED_APPS list in settings.py ?

Upvotes: 0

abduljalil
abduljalil

Reputation: 153

There must be a configuration issue but if you want to see if website is working or def Home(): is working. Change your code to

def home(request):
return HttpResponse('<h1>Blog Home</h1>')

Upvotes: 0

John Gordon
John Gordon

Reputation: 33335

main/urls.py

from django.urls import path
from main import views
from . import views

Those last two imports both import the name views, so the first one is overwritten by the second one.

Upvotes: 1

Safwan Samsudeen
Safwan Samsudeen

Reputation: 1707

Your home function is indented wrongly, it's currently a function inside the index function, not in the global scope.

Change it to the following

views.py

def index(response, id):
      ls = ToDoList.objects.get(id=id)
      return render(response, "main/base.html", {})

def home(response): # Don't indent this!
    return render(response, "main/home.html", {})

Upvotes: 0

Related Questions