-3

I'm just starting to work with Django in python, and whenever I try to import the views module I get this import error :

ImportError: attempted relative import with no known parent package

from django.urls import path
from . import views
urlpatterns = [
    path('', views.home, name='home'),
] 
Bussller
  • 1,961
  • 6
  • 36
  • 50
  • This assumes that there is a `views` module available to import, which we have no evidence of – DavidW Aug 19 '20 at 09:56
  • 2
    Probably a duplicate of https://stackoverflow.com/questions/55084977/attempted-relative-import-with-no-known-parent-package – DavidW Aug 19 '20 at 09:57
  • 2
    Does this answer your question? [Attempted relative import with no known parent package](https://stackoverflow.com/questions/55084977/attempted-relative-import-with-no-known-parent-package) – toydarian Aug 19 '20 at 10:55
  • `from .app_name import func_name` Please import like this syntax. Here we assume app_name is 'my_app' and function name is 'home' then: `import .my_app import home' – Riyas Ac Aug 19 '20 at 15:48

2 Answers2

0

Please try this: Here app_name is name of the app you written home function.

from django.urls import path
from .app_name import home
urlpatterns = [
    path('',home, name='home'),
]

Function views

  1. Add an import:from my_app import views
  2. Add a URL to urlpatterns:path('', views.home, name='home')

Class-based views 3. Add an import: from other_app.views import Home 4. Add a URL to urlpatterns: path('', Home.as_view(), name='home')

Including another URLconf 5. Import the include() function: from django.urls import include, path 6. Add a URL to urlpatterns: path('blog/', include('blog.urls'))

The urlpatterns list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/

Riyas Ac
  • 1,553
  • 1
  • 9
  • 23
0
from django.contrib import admin
from django.urls import path
from app_name.views import home

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

Try doing this. app_name indicates your app name.