Why Django doesn't find a modules which PyCharm finds well? - python

I tries to build my first Django project.
I've created 'superlist' project and 'lists' app inside. My project tree:
pycharm_project_folder
|
superlist
|
lists
superlist
manage.py
...
|
venv
My lists/views.py:
from django.shortcuts import render
def home_page():
"""home page"""
pass
My superlist/urls.py
from django.urls import path
from superlist.lists import views
urlpatterns = [
path('/', views.home_page, name='home')
# path('admin/', admin.site.urls),
]
My lists/test.py
from django.test import TestCase
from django.urls import resolve
from superlist.lists.views import home_page
class HomePageTest(TestCase):
"""тест домашней страницы"""
def test_root_url_resolves_to_home_page_view(self):
"""корневой url преобразуется в представление домашней страницы"""
found = resolve('/')
self.assertEqual(found.func, home_page)
So, when I run
python3 manage.py test
I see
ModuleNotFoundError: No module named 'superlist.lists'
I don't understad why I got it because paths were suggested by PyCharm

With Python3, you will want to use relative imports, especially when you have duplicated package names, like you do here. In superlist/urls.py try:
from .lists import views
This assumes the urls.py file is superlist/urls.py and not superlist/superlist/urls.py. If the latter is true, then it would be:
from .superlist.lists import views

So, in the end I just marked superlist folder as a 'source folder' in PyCharm. It resolved my issue

Related

Django 3.1 gives error | URLconf does not have any pattern in it, If you see valid patterns in the file then the issue is probably

Python: 3.6.0 | Django: 3.1.4
By going through the documentation, I created one app, below is my app url.py (from app) file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
My site urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('npimgapp/', include('npimgapp.urls')),
path('admin/', admin.site.urls)
]
My view.py file in app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome ...")
On executing this code I am getting below error:
django.core.exceptions.ImproperlyConfigured: The included URLconf
'npimgpro.urls' does not appear to have any patterns in it. If you see
valid patterns in the file then the issue is probably caused by a
circular i mport.
Also, below is my directory structure for ref:
Your directory structure is wonky; there shouldn't be two nested npimgpro folders.
Move the contents of that npimgpro folder up one level, so it's
npimgpro/
(etc.)
npimgapp/
(etc.)
manage.py
db.sqlite3

ModuleNotFoundError: No module named 'src'

I'm changing view of homepage with app names pages.
I've added pages to settings. this is how directory look like:
- trydjango
- src/
- pages/
- __init__
- views
- products
- trydjango/
- __init__
- settings
- urls
- manage
views' code:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello Again</h1>")
urls code
from django.contrib import admin
from django.urls import path
from src.pages.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
]
and I see this error when I run server
ModuleNotFoundError: No module named 'src'
First you need to understand what an app in Django is compared to a project.
When you register an app django will look in the project root folder when you try to import it.
Your project root is where your manage.py file is. In your case the src folder.
So when you want to import your views module you need to state
from pages.views
rather than
from src.pages.views
I suggest that you read through and follow (by coding it yourself) the Django tutorial to learn more about project structure and creating your own apps with models, urls etc.
I got the same problem, IDE may use red underline but this code is still correct:
from pages.views

django/python 3.5 imports/separating views.py into separate files

I've looked at just about everything I can here. I'm using python 3.5 and i've seen stuff about how they've changed the way imports work. My django project structure is like this:
project
--app
--views/
--__init__.py
--myFile.py
--__init__.py
--models.py
--admin.py
--urls.py
--etc....
My urls.py is such:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^api$', views.function_from_myFile_that_is_not_being_found),
otherurls()...
]
The error I'm getting is AttributeError: module 'app.views' has no attribute 'function_from_myFile_that_is_not_being_found'
I'm really lost as to why this is happening. I've tried putting imports in my __init__.py files and that hasn't worked either. Not sure what else I'm missing.
Thanks in advance.
In your project views is a package, not a single module. So you should do
from .views import myFile as views

django import error - from . import views in urls under app

I did declare like from . import views in urls.py.
To use TemplateView, this urls.py is needed.
urls.py
from django.urls import path, re_path
from . import views
app_name = 'scheduler'
urlpatterns = [
re_path(r'^service/(?P<status>\w+)', views.SchedulerView.as_view(), name='schedule-service')
]
I think nothing but normal implementation.
Error occurs like the below.
from . import views
ImportError: cannot import name 'views'
Older versions django did work. But it's not working in django 2.0
App Structure
- server
- scheduler
- templatetags
schedule_status.py
urls.py
models.py
views.py
- main
settings.py
urls.py
- manage.py
I just guess 'scheduler' app's path is incorrect to work "from . import views"
Is there anyone who solved or check more things. these problem after django 2.0.
ps. sorry, I forgot to add view.py in structure in question. SchedulerView is declared in views.py
I did found a solution. python 3.6 has changed something.
I did create directory as views and added scheduler_view.py
and
from .views import scheduler_view as view
app_name = 'scheduler'
urlpatterns = [
re_path(r'^service/(?P<status>\w+)', view.SchedulerView.as_view(), name='schedule-service')
]
it doesn't occur error and runserver as well.
plus I did error occur again in another file. it's for older python.
scheduler_view.py
from scheduler import Scheduler -> from scheduler.scheduler import Scheduler
I did change it.
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
I wish it's helpful.

Django cannot import app

Following the django-rest tutorial
app/urls.py:
from django.conf.urls import url, include
from rest_framework import routers
from app.abbr import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Directory structure:
Error:
File "..../app/app/urls.py", line 3, in
from app.abbr import views
ImportError: No module named 'app.abbr'
So, sigh...
It would have been useful if you pointed to the tutorial that showed you to do this.
You should not import from app; that refers to the inner directory containing your urls.py. Just import from abbr.
from abbr import views
And what if you change import like this?
from app.app.abbr import views?
I am considering that you are using django 1.9 +
Try this
from . import views
The root directory folder named App in your case is named after your project name by default when you start a new project via the django-admin startproject command.
you can rename your root directory folder to whatever you want and it won't affect your project.
when in your code are importing from app, it is actually looking inside the 'app' folder containig the 'settings.py' file.
the django-rest tutorial you are following contains an error when they are doing from tutorial.quickstart import views which should be from quickstart import views
so the same goes for you, you should do from abbr import views

Categories

Resources