When I run the server, I am getting the following error:
from music_controller.api.models import Room
ModuleNotFoundError: No module named 'music_controller.api'
I think the problem is from the import in the views.py:
from django.shortcuts import render
from rest_framework import generics
from .serializers import RoomSerializer
from .models import Room
# Create your views here.
class RoomView(generics.CreateAPIView):
queryset = Room.objects.all()
serializer_class = RoomSerializer
I made sure that the app name is included in INSTALLED_APPS in the settings.py.
The following is the tree of my project:
In the music_controller directory, add a file called __init__.py. Leave the file empty.
Related
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
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
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
I am following a tutorial online for a to-do app using Django, but I keep getting this error.
File “C:\DjangoStuff\my_app\todo_app\urls.py”, line 3, in
from todo_list import views
File “C:\DjangoStuff\my_app\todo_list\views.py”, line 3, in
from .forms import ListForm
ModuleNotFoundError: No module named ‘todo_list.forms’
I have tried deleting and adding back the forms.py file, but no success. The todo_list folder does contain all the files being referenced as far as I can tell.
I have a todo_list folder where all the below mentioned files are stored
This is the code I have imported in the urls.py file:
from django.urls import path
from . import views
in the views.py file, I have the following imported:
from django.shortcuts import render, redirect
from .models import List
from .forms import ListForm
from django.contrib import messages
from django.http import HttpResponseRedirect
I have also created the models.py file and forms.py files as part of the todo_list folder.
I'ms sure it's something simple but have been through the tutorial many times and cannot find anything wrong...
File list
Your forms.py file should be in the folder todo_list\, not under todo_list\templates\
as it is now.
from .forms import ListForm in the views.py file expect to find the forms.py file in the same directory (.)
Other solution could be to let the file where it is now, but state instead in the views.py file from todo_list.templates.forms import ListForm
So i'm trying to create an application for user interaction with stream_django. So far everything was going well until I got error:
'User model not registered. Please register model with actstream.registry'.
So after searching for some time, I modified my apps.py to be like this:
The project is called reviews
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "reviews.settings")
os.environ["DJANGO_SETTINGS_MODULE"] = "reviews.settings"
django.setup()
from django.apps import AppConfig
from django.contrib.auth.models import User
class MyappConfig(AppConfig):
name = 'myapp'
def ready(self):
from actstream import registry
registry.register(User,self.get_model('Post'),self.get_model('UserProfile'))
But now I'm getting error:
'AppRegistryNotReady("Apps aren't loaded yet")'
I'm not sure what to do anymore, any help is appreciated.
I faced the same problem but I found that the solution was to add the default_app_config setting in the __init__.py of your main project (same location as your settings.py).
For your case, that __init__.py file should look like this:
# reviews/__init__.py
default_app_config = 'myapp.apps.MyappConfig'
my project directories is:
apps/
goods/
models.py
views.py
base.py
trades/
users/
__init__.py
apps/goods/base.py
from django.views.generic.base import View
from apps.goods.models import Goods
class GoodsListView(View):
def get(self, request):
json_list = []
goods = Goods.objects.all()[:10]
for good in goods:
# json_dict = {}
# json_dict['name'] = good.name
# json_dict['category'] = good.category.name
# json_dict['market_price'] = good.market_price
# json_dict['add_time'] = good.add_time
# json_list.append(json_dict)
from django.forms.models import model_to_dict
for good in goods:
json_dict = model_to_dict(good)
json_list.append(json_dict)
from django.http import HttpResponse
import json
return HttpResponse(json.dumps(json_list), content_type='application/json')
i'm debug base.py not get data, but get the error:
from apps.goods.models import Goods
ModuleNotFoundError: No module named 'apps.goods'; 'apps' is not a package
and, i remove 'apps' in 'apps.goods.models', get the error:
from goods.models import Goods
ModuleNotFoundError: No module named 'goods'
env:
pycharm-2017.2
django-1.11.6
why get the error?
Use just from .models import Goods (look at "." before models - it means the module is from current folder ).Because base.py and models.py are in same folder (same app) so you dont need to specify from which app you want to import models. Just simply include it like this.
But if you want to import models from other apps, you should to make apps to be package.In Goods app folder add __init__.py.
Structure should look like:
apps/
goods/
__init__.py
models.py
views.py
base.py
trades/
users/
__init__.py
Than use from goods.models import Goods or from apps.goods.models import Goods
As in the others' comments, you need to create the init file in the folder that should be considered a package. It's called __init__.py however. You have one of these files in apps, make sure you have it in apps/goods as well.
If you still have the same problem, make sure your configuration in Django is correct, i.e. the folder above apps is loaded
I have a django 1.9 application using DRF with the following folder structure
- api/
- api/models.py
- api/serializers.py
- api/views.py
- manage.py
In serializers.py I have an import:
from api.models import Model1, Model2, ...
In views.py I have these imports:
from api.serializers import NotificationSerializer
from api.models import Model1, Model2, ...
Everything works fine until now. But after adding
from api.serializers import NotificationSerializer
in models.py, django starts complaining when I start the dev server:
File ".../api/serializers.py", line 3, in <module>
from api.models import Model1, Model2,...
ImportError: cannot import name Model1
I know that there must be a problem with the python import paths, but I can't figure it out.
This would cause a circular import, since serializers.py imports models.py, and vise versa.
How to resolve this depends on what NotificationSerializer does. If it does not use the models, you might consider moving it to a utils file.