ImportError: No module named App1.views (django 1.5). Any idea? - django

I have 2 Apps in myproject. When I try to import App1 views from App2 views, I get ImportError: No module named App1.views. for e.g (from myproject.App1.views import *). And I added both apps under INSTALLED_APPS in settings.py

In Django 1.5, you need to omit the project name from the imports. The release notes of Django 1.4 explains this change in the applicatiion structure and import paths. So the correct import statement is:
from App1.views import *

Related

How to import model from one app to another?

The issue I face:
Error: ModuleNotFoundError: No module named 'CVBuilderApp.cvs'
What I did: In my main app views file i.e, in the CVBuilderApp.views file
views.py: from CVBuilderApp.cvs.models import PersonalInfo
My project structure:
CVBuilderApp
- accounts
- models, urls, views
- cvs
- models, urls, views
- CVBuilderApp
- settings.py
- manage.py
How do I import the model while the main application name and the project name are the same? Please help
from csv.models import PersonalInfo
Imports are relative.
You can calling python manage.py runserver so everything is relative to manage.py
Example
CVBuilderApp (root)
- helpers (dir)
- helperofhelper (dir)
- doesthing.py <- Import this
- cvsHelper.py <- from Here
- manage.py
if wanting to import doesthing inside of cvsHelper
you would not do from helperofhelper.doesthing import dothing
you would do from helpers.helperofhelper.doesthing import dothing
edit: missed the from
you should not write project name in imports, so your imports must be something like this:
from cvs.models import PersonalInfo
Model location is relative to the location of manage.py file.
so you need to enter:
from cvs.models import PersonalInfo

Django / app import problem from submodule

I'm writing my own Django app, and trying to import submodule from my core library like that:
INSTALLED_APPS = [
'django.contrib.admin',
...
'core.login',
]
And interpreter gives me:
django.core.exceptions.ImproperlyConfigured:
Cannot import 'login'.
Check that 'core.login.apps.CustomloginConfig.name' is correct.
So login.apps looks like that
from django.apps import AppConfig
class CustomloginConfig(AppConfig):
name = 'login'
Are there any rules how I can edit this files to start Django properly?
apps.py file needs to be as so
from django.apps import AppConfig
class CustomloginConfig(AppConfig):
name = 'core.login'
This is where you tell django that I have registered this app 'core.login' and where to find it.
If login folder is in a core folder, then the above should work.
I think theres a lot of django apps out there that have organized things this way.
One being Kiwi but Im sure theres many others.

ModuleNotFoundError: No module named 'register'

I am working on a Django project. I am working on the register page. When I try to import my register/views.py to my mysite/urls.py file I get an error message. ModuleNotFoundError: No Module named 'register'. Both files are are in the same directory.
from django.contrib import admin
from django.urls import path, include
from register import views as v
Adding full exception message
Try the following:-
from . import views
Please add a blank __init__.py file in the register folder.
Only then python will understand that register is an importable package
Edit:
After seeing the exception, it looks like a working directory issue in pycharms. Please try the fix mentioned in this link

Overriding the default admin site - can't find myproject

I am trying to override the default admin site. I followed the Django Docs here:
https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#overriding-the-default-admin-site
I did everything as stated there so the files look like beneath:
admin/admin.py:
from django.contrib import admin
from django.utils.translation import gettext as _, gettext_lazy
class CustomAdminSite(admin.AdminSite):
# Text to put in each page's <h1>.
site_header = gettext_lazy('TEST administration')
admin/apps.py
from django.contrib.admin.apps import AdminConfig
class CustomAdminConfig(AdminConfig):
default_site = 'admin.CustomAdminSite'
core/urls.py
urlpatterns = [
path(r'testadmin/', admin.site.urls),
]
core/settings.py
INSTALLED_APPS = [
'admin.apps.CustomAdminConfig',
]
Now I am getting a ImportError:
ImportError: Module "admin" does not define a "CustomAdminSite" attribute/class
I think this is because django is looking for a admin module that lives inside of the virtual environment with the CustomAdminSite class. This is not the case, the admin folder with the necessary python files lives in the base path of myproject. When I state this in my settings like: myproject.admin.apps.CustomAdminConfig I am getting a ModuleNotFOundError:
ModuleNotFoundError: No module named 'myproject'
I guess the same thing as stated above. Django is looking for a module named myproject inside of the virtual environment.
Is there a way to change this in the settings or a way to bypass this some other way?
As #IainShelvington said, the name of the module is likely admin.admin, since admin is the name of the app, and then in that app you have a "submodule" named admin, so this should be:
from django.contrib.admin.apps import AdminConfig
class CustomAdminConfig(AdminConfig):
default_site = 'admin.admin.CustomAdminSite'

Scrapy project can't find django.core.management

I'm trying to follow the method here to 'Scrapy' data from the web and simultaneously save that data directly to my Django database using Scrapy's item pipeline.
However, when I try to run scrapy crawl spidername, I'm getting the error:
ImportError: No module named django.core.management
At first I thought it was because my Scrapy project was outside of my Django project folder, but even after I moved the whole project into my Django project folder I kept getting the same error. If I open a python shell inside the Scrapy project folder in its new location (inside my Django project folder), import django.core.management works fine. So what's going on?
EDIT: Some additional info: I'm doing this on a Webfaction server, and the path to my Django project is /home/gchorn/webapps/django_app/django_project. I'm using Django version 1.4.1, Scrapy 0.16 and Python2.7. The layout of the Django+Scrapy project is as follows:
django_project/
__init__.py
manage.py
settings.py
urls.py
myproject #folder containing wsgi.py
app1
app2
app3
templates
ScrapyProject/
scrapy.cfg
ScrapyProject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
my_spider.py
Try setting this in your Spider's settings.py:
import os
import sys
sys.path.append('/home/gchorn/webapps/django_app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Then you can import your model classes like:
from django_project.app1.models import some_model