0

I wanted to import my views.py file from my app "users" and the urls.py of the project. However, I'm getting an error that the "Import "users" could not be resolved".

Picture of the problem

The urls.py file:

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from users import views as user_views       # Why is there an error?


urlpatterns = [
# Admin Page
path('admin/', admin.site.urls),

# Homepage
path('', include('blog.urls')),

path('register/', user_views.register, name = 'register'),
path('profile/', user_views.profile, name = 'profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name = 'logout'),
]

The app is also in "INSTALLED_APPS" in the settings.py file:

INSTALLED_APPS = [
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

Folder structure

This is probably an easy-to-solve problem, however, I'm fairly new to Django and don't really understand why it says this is an error. In a similar project, everything works fine.

No error in a similar project, where I did everything the same way as far as I remember.

vasadia
  • 366
  • 5
  • 8
Drax
  • 23
  • 1
  • 1
  • 4

2 Answers2

6

In your root directory in the vscode left pane, there is a folder .vscode. click on that and the following key:value pair of your directories ex:["./users", "./blog"] it will resolve relative imports. It worked for me.

{
    "python.analysis.extraPaths": ["./users", "./blog"]
}
0

This error has come because you have app users and blog which you have not written in settings.py file in INSTALLED_APPS

INSTALLED_APPS = [
'blog',
'users',
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

and include urls also in project urls.py

Ankit Tiwari
  • 4,438
  • 4
  • 14
  • 41
  • 1
    This didn't help and gave me another error: django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: users django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: blog – Drax Dec 12 '20 at 15:53
  • see this solution https://stackoverflow.com/a/24319562/14457833 – Ankit Tiwari Dec 12 '20 at 16:40