I have a project with a structure like this:
my-django-project/
venv/
.gitignore
README.md
my_django_project/
db.sqlite3
manage.py
my_django_project/
settings.py
my_first_app/
__init__.py
inside_app/
__init__.py
apps.py
serializers.py
models.py
views.py
...
In settings.py i loaded the app like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_first_app.inside_app',
]
Running py manage.py runserver gives the following error:
File "C:\Users\\Documents\GitHub\tin_api_v2_test\venv\lib\site-packages\django\apps\config.py", line 246, in create
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'inside_app'. Check that 'my_first_app.inside_app.apps.InsideAppConfig.name' is correct.
However, it works fine when I don't use the Virtualenv. I've also tried putting the venv inside the django project folder to see if that would change anything but it didn't work.
Solved it..
In apps.py of inside_app i had to change from:
from django.apps import AppConfig
class InsideAppConfig(AppConfig):
name = 'inside_app'
to
from django.apps import AppConfig
class InsideAppConfig(AppConfig):
name = 'my_first_app.inside_app'
Related
I was following the django documentation tutorial to make my first app, and then I reached the admin.py part and was trying to register my models, but then I got this error:
Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
My Folder Structure:
├───Poll
│ └───__pycache__
└───polls
├───migrations
│ └───__pycache__
└───__pycache__
Visual Representation:
My Code in admin.py:
from django.contrib import admin
from models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
Settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'members.apps.MembersConfig',
]
I could see one problem i.e. import should be either from current location, using . or specify some app name.
Through current location:
from .models import Question, Choice
Through app name:
from some_app_name.models import Question, Choice
Figured it out after many frustrating hours. Basically in admin.py you register the model and instead of running the file, type py manage.py runserver.
Here's an example:
Admin.py:
from django.contrib import admin
from .models import Person
admin.site.register(Person)
Instead of running the file separately, do this:
CMD:
cd yourproject
py manage.py runserver
And It works.
EDIT
So this was a Docker issue, not a Python issue. Thank you everyone for your help!
I am working on a Django project. I have several apps within the project and everything has been working fine. I recently added a new app called files through django-admin and for some reason, Django cannot find this module. This is my project structure
core/
__init__.py
settings.py
urls.py
wsgi.py
api/
migrations/
__init__.py
admin.py
apps.py
models.py
urls.py
views.py
files/
__init__.py
apps.py
forms.py
init_s3.py
urls.py
views.py
The apps.py for the files module reads:
from django.apps import AppConfig
class FilesConfig(AppConfig):
name = 'files'
Which is almost word-for-word how the AppConfig for the api module is written:
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
I have the api and files app installed in the INSTALLED_APPS setting:
INSTALLED_APPS = [
...other apps...
'api.apps.ApiConfig',
'files.apps.FilesConfig'
]
Django has no problems locating the api app, but cannot find the files app. When I run the server, I get the following error:
ModuleNotFoundError: No module named 'files'
What's going on here? What am I missing?
Any help is appreciated! Thanks!
Try putting the app name to INSTALLED_APPS:
INSTALLED_APPS = [
...other apps...
'files'
]
Have you checked your project's urls.py file? it should read:
urlpatterns = [
....
path('files/', include('files.urls')),
]
I am quite new to Django so bear with me, I have read a few Stack Overflow answers on similar but they seem to be much more complicated than what I am trying to do. Any help would be appreciated.
I have followed a tutorial on Udemy which built a website with some models, and then populated them with data using a python script.
From this course; https://www.udemy.com/course/python-and-django-full-stack-web-developer-bootcamp
I am trying to do the same and have followed the steps as analogously as I can. I have;
declared the models in app_name/models.py
imported them into app_name/admin.py and registered them
included app_name in INSTALLED_APPS in settings.py
Currently when I log onto the admin of my server I can see the models and add to them. What I cannot seem to do is import the models into my populate.py script which is located in the top level folder.
The script is as follows;
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
import django
django.setup()
import pandas as pd
import glob
from app_name.models import Model_Name
I am getting the following error;
RuntimeError: Model class app_name.models.Topic doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
I am not sure whether I have posted all of the relevant information, so do let me know if I can add anything.
File structure is a follows;
project_name
__pycache__
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
app_name
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
db.sqlite3
manage.py
populate.py
The contents of INSTALLED_APPS is the following
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name'
]
Did you try to populate with ORM I mean django shell? In your case I would create custom command instead of populate.py file. It would be pretty easier for you to use.
I'm using django on vscode, and typed this on terminal:
(impassion) Subinui-MacBook-Pro:impassion_community subin$ python3 manage.py makemigrations
but can't use makemigrations
got this error message
ModuleNotFoundError: No module named 'impassionuser'
how can I solve this problem?
I'm using MacOSX, VSCode, django
and setup virtualenv
I expected to see follow messages
Migrations for 'impassionuser':
impassionuser/migrations/0001_initial.py
-Create model impassionuser
In settings.py, I already added 'impassionuser'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'impassionuser',
'board'
]
Did you register your model in admin.py.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import mymodel
admin.site.register(mymodel)
Hopefully this will help.
Also check this Django Tutorial
I was trying to connect my database application to project in django. Succeded on connecting psycopg and database. Then I started to build application for it by
python manage.py startapp books
and wrote some code in models.py.
But when I tried to put application in INSTALLED_APPS, it caused on error.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tut1.books',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
So, I'm in project's folder and type in Command Line:
%>python manage.py syncdb
ImportError: No module named 'tut1.books'
If I remove my app from INSTALLED_APPS, everything goes fine.
I suspect, there gotta be different syntax for linking application.
Tried to change 'tut1.books' to just 'books'. Causes another error.
%>python manage.py syncdb
AttributeError:'module' object has no attribute 'URLFIELD'
Files and folders in project /tut1/
manage.py
views.py
/tut1/
/templates/
/books/
Files in /books/
__init__.py
modules.py
views.py
tests.py
Files in /tut1/tut1/
__init__.py
settings.py
urls.py
views.py
wsgi.py
init.py is empty by default.
Checked PYTHONPATH for this project. Nothing odd...
>>> import sys
>>> for i in sys.path:
print(i)
C:\study\django\tut1
C:\Python33\Lib\idlelib
C:\Python33\lib\site-packages\setuptools-1.1.6-py3.3.egg
C:\Python33\python33.zip
C:\Python33\DLLs
C:\Python33\lib
C:\Python33
C:\Python33\lib\site-packages
It can be a problem with models.py.
You seem to write code as follows.
foo = models.URLFIELD()
"models" module has no URLFIELD().
You should change it to URLField().
You'll need to add an __init__.py file (it can be empty) to the folder tut1.books before python can import it. See this answer: What is __init__.py for?