1

I am working through the following YT vid on Django and Python as I am looking to make my first app and trying to learn how Django works:

https://www.youtube.com/watch?v=F5mRW0jo-U4

I am working on adding new views to an app called Pages and trying to import the views from that app, but for some reason keep getting:

NameError: 'views' is not defined. I am sure this is something small that I am missing due to my newness with python in general and importing. Here is my current code:

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

#from Pages import views
from Pages.views import homepage_view, toybox_view, lounge_view, gym_view

urlpatterns = [
    path('', views.homepage_view, name='home'),
    path('toybox/', views.toybox_view, name='toybox'),
    path('lounge/', views.lounge_view, name='lounge'),
    path('gym/', views.gym_view, name='gym'),
    path('admin/', admin.site.urls),

]

The second 'from' statement is the one that does not work - the commented out statement does work, I was just trying to be more specific with my imports. I have the 'src' folder that has my project - DogtopiaWeb and two apps, Dogs, and Pages. Inside of the Pages app is the views.py that I am trying to import with the above from statement.

Any idea why it can't identify the views.py inside the Pages app directory? I am importing it into the urls.py that is inside of the DogopiaWeb project.

The first screenshot is my root directory with manage.py and the two apps and main project. Second screenshot is inside the "Pages" app showing the views.py file that the import is having trouble identifying.

enter image description hereenter image description here

Thanks!

1 Answer 1

3

The problem is that you do not need views from your app Pages, you are already importing the specific views in:

from Pages.views import homepage_view, toybox_view, lounge_view, gym_view

so use the following:

urlpatterns = [
    path('', homepage_view, name='home'),
    path('toybox/', toybox_view, name='toybox'),
    path('lounge/', lounge_view, name='lounge'),
    path('gym/', gym_view, name='gym'),
    path('admin/', admin.site.urls),

]

or if you want to use views.name_of_your_view, you need to import the line that you have commented from Pages import views and remove the other.

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.