Reputation: 41
***SOLVED: I was using Sublime Text 3 as an IDE and it didn't like the dot in 'from . import views'. I switched to Visual Studio Code and everything is working fine. Thanks to everyone that responded with suggestions and helped!
I'm learning Django by using the Writing your first Django app documentation from the site. I have edited three files.
In the file urls.py in the polls folder:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
In the urls.py in the mysite folder:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
In the views.py in the polls folder:
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello once again.')
I'm using Python 3.7 and Django 2.1.2, could an updated version of either affect this?
Here is error I get from trying to run the server:
File "D:\DjangoPractice\mysite\polls.urls.py", line5, in <module>
path('', views.index, name='index'),
AttributeError: module 'polls.views' has no attribute 'index'
Any help would be greatly appreciated!
Upvotes: 4
Views: 6739
Reputation: 11
I doubt if it was an editor issue like you mentioned. It's not a sublime or a VS Code problem. The problem came up because of the way you called the index function.
I just feel it's better to make sure we got it all right for posterity's sake. Many will come this way for solutions in future.
Upvotes: 0
Reputation: 1024
from polls import views
add your app in settings.py INSTALLED_APPS.
Upvotes: 0
Reputation: 3286
Your problem is that you are adding this line path('', views.index, name='index')
, in your project urls.py
, but not on the app urls.py
.
When you do from . import views
, you are looking for a views file in your project folder, but the views file is inside polls folder.
You can fix this by doing this in your urls.py from polls import views
Upvotes: 3