Tamir Hen
Tamir Hen

Reputation: 113

Can't import 'views' from 'urls' - Django

I'm trying to import 'views' file to my 'urls' so I could map the path to this 'vehicle_validation' function. For some reason, pycharm can't find this file. can someone help me understand what's the problem?

urls file:

from django.urls import path
from vehicule_approver import views # error here

urlpatterns = [
    path('admin/', admin.site.urls),
    path('vehicle_validation/', views.vehicle_validation)
]

views file:

import requests
from django.http import HttpResponse
import json
from pyapi.vehicule_approver.models import Vehicle

def vehicle_validation(request): 
...

project structure: structure image

Upvotes: 0

Views: 3417

Answers (2)

Alasdair
Alasdair

Reputation: 308879

The problem is:

from pyapi.vehicule_approver.models import Vehicle

Imports should be relative to the project directory (the one that contains manage.py), so you should remove the pyapi and change the import to:

from vehicule_approver.models import Vehicle

Alternatively, since you are importing from the same app, you can use a relative import.

from .models import Vehicle

Upvotes: 1

bastantoine
bastantoine

Reputation: 592

I think the problem cames from the import of vehicule_approver you used in views.py. With from vehicule_approver import views Python tries to find a module vehicule_approver inside your sys.path.

Perhaps use one of those imports:

from ..vehicule_approver import views # Relative import
from pyapi.vehicule_approver import views # Absoute import

Upvotes: 2

Related Questions