Home > Net >  Issue importing application in Django in urls.html
Issue importing application in Django in urls.html

Time:02-05

My src directory's layout is the following:

  • Learning
    • innit.py
    • settings.py
    • urls.py
    • wsgi.py
  • pages
    • innit.py
    • admin.py
    • apps.py
    • models.py
    • tests.py
    • views.py

Views.py has this code

from django.shortcuts import render
from django.http import HttpResponse

def home_view(*args,**kwargs):
    return HttpResponse("<h1>Hello World, (again)!</h1>")

urls.py has this code

from django.contrib import admin
from django.urls import path
from pages.views import home_view


urlpatterns = [
    path("", home_view, name = "home"),
    path('admin/', admin.site.urls),
]

The part where it says 'pages.views' in 'from pages.views import home_view' has a yellow/orange squiggle underneath it meaning that it is having problems importing the file and it just doesn't see the package/application called 'pages' and doesn't let me import it even though the package has a folder called 'innit.py'. Even worse is the fact that the tutorial I am currently following receives no such error and I can't see anyone else who has encountered this error.

As you probably expect I am a beginner so I don't have experience and this is my first time editing views.html in Django so I may have made an obvious mistake if so, just point it out.

I tried doing

from ..pages.views import home_view

However it failed and gave me an error

I have also tried changing the project root however this now causes issues with the imports in 'views.py'.

CodePudding user response:

The part where it says 'pages.views' in 'from pages.views import home_view' has a yellow/orange squiggle underneath it meaning that it is having problems importing the file and it just doesn't see.

You need to mark the correct "source root". This is for Django the project directory, which is the directory that contains the apps.

For example in PyCharm you click right on that directory, and use Mark Directory as… ⟩ Sources Root.

  • Related