If anyone knows how to solve this, please don't hesitate to help. I've tried many hints, but so far this problem still remains. Too frustrating!
I just started a little project on Django because I want to test Django Rest Framework. I created a project called project and inside it an app called user.
After that, I created a directory called api inside user and then I created the files viewsets.py and serializers.py in order to test Django Rest Framework.
In urls.py I imported "UsuarioViewSet" class from viewsets.
from project.user.api.viewsets import UsuarioViewSet
When I run python manage.py runserver the problem bellow ocurrs
(I also tried only user.api.viewsets... but it says "unresolved reference")
In my settings.py I registered 'user' and 'rest_framework':
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'user',
]
OS: Linux Mint |
IDE: PyCharm (community) |
Virtualenv: venv |
Python: 3.6.6
As far as I know, this kind of thing happens because of migration file __init__.py.
The main reason for this is when you write code and make changes in the migration file and then you remove some part from code for which a migrate file has been already created.
So a solution is just delete __init__.py from migration
and make sure that there is no other import which is removed or changed.
Related
Currently I have a python project with the following directory structure.
I can run the demo.py file directly fine (without importing it in my Django project) from the outermost directory shown above using the following command:
python -m ObjectSegmentation.segmentation_api.apps.tools.demo
However, in my Django project, I need to import the demo.py file in my django views.py file, but when I do so, I get the following error:
from ObjectSegmentation.segmentation_api.apps.tools.test import *
ModuleNotFoundError: No module named 'ObjectSegmentation'
These are the first few lines from demo.py (including line 4) where the error occurs.
My installed apps in settings.py for the whole project look like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.tools.demo',
'rest_framework'
]
How can I make it so that the imports work with both the project run independently and imported in my django project?
Every time I try to run my program using python manage.py runserver, this same error comes up:
"ModuleNotFoundError: No module named 'django.contrib.staticfilesbase'"
This error typically means that while trying to run your app, either your code or your dependencies is telling django to look for django.contrib.staticfilesbase and my best guess is that it is your code.
Start with your settings.py. Open it and look under the installed apps. It should look at least something like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', #this is probably where the mistake is
]
You can have other app under the "installed apps" ofcourse, but if you find django.contrib.staticfilesbase in there, change it to django.contrib.staticfiles.
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 running Windows 10 (sorry) and I'm trying to set up the django-nonrel test app for GAE from github, https://github.com/django-nonrel/django-testapp.
I've downloaded and installed it and I've also installed the various modules from Django and NoSQL on github.
So my filestructure is dbindexer, django, djangoappengine and djangotoolbox as subdirectories of testapp. I've put these in place as I've come across install errors in the log file.
I'm now getting the message
ImproperlyConfigured: Error importing module autoload.middleware: "No
module named autoload.middleware"
This is reasonable since settings.py has autoload as an installed app.
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'djangotoolbox',
'autoload',
'dbindexer',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
)
But I don't know where I find this module. I know I must be being really thick but I cannot work out how to get this running.
EDIT - here is the requirements.txt file
-e git+http://github.com/django-nonrel/djangotoolbox#toolbox-1.4#egg=djangotoolbox
-e git+http://github.com/django-nonrel/djangoappengine#appengine-1.4#egg=djangoappengine
-e git+http://github.com/django-nonrel/django-dbindexer#dbindexer-1.4#egg=django-dbindexer
-e git+http://github.com/django-nonrel/django#nonrel-1.4#egg=django-nonrel
-e hg+http://bitbucket.org/twanschik/django-autoload#egg=django-autoload
So I've downloaded this autoload and I'm now getting the following error.
ImportError: No module named fcntl
This was solved by going to a MySQL solution rather than django-nonrel. It took some time but lots on StackOverflow to help through it.
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?