"Attempted relative import beyond top-level package"

When i want to save this django file:

from django.urls import path from . import views    urlpatterns = [     path('', views.index) ] 

I get an error :

"Attempted relative import beyond top-level package"

What should I do with it?

Add Comment
2 Answer(s)

Change your assignment to urlpatterns to this:

urlpatterns = [path('index/', views.index, name='main-view')] 
Answered on July 16, 2020.
Add Comment

One possibility is that some import syntaxes are Django-specific, which the interpreter (like pylint) cannot recognize unless they are installed along with Django and that require some extra work. If the server works but the compiler doesn’t let you run it, simply disable whatever interpreter you have or switch to another IDE.

But always remember, in a Django application, it should work out like this (NOT the main folder where the settings.py is located in):

|-app_name |__ __init__.py |__ admin.py |__ apps.py |__ models.py |__ urls.py |__ views.py 

As long as your files are structured like this, from . import views should work.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.