I am following the Tango with Django book's tutorial.
I tried to do:
from rango import views
in tango_with_django_project/urls.py where rango is the application. The IDE I use is pycharm and it cannot find or doesn't recognize rango.
The folder hierarchy is as follow:
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
I already added 'rango' in settings.py
INSTALLED_APPS = [
.......
'rango',
]
Any help or comment is greatly appreciated. Thank you.
You said
pycharm ... cannot find or doesn't recognize rango
but you don't say if you have actually tried running the application. It could be just that pycharm doesn't know where to look for the code.
Try setting the top level folder of your project (the one you have the tango_with_django_project folder in) and mark it as a "Sources Root" by right-clicking on the folder and selecting the option from the "Mark Directory As" menu.
If that doesn't work, run your django app and add any error messages to your question.
include rango in INSTALLED_APPS within settings.py file as mentioned in below snippet
INSTALLED_APPS = [
.......
'rango',
]
The problem has to do with the level of directory I opened in PyCharm.
When I had the problem, I opened it from the topmost directory - which was why PyCharm couldn't find it.
rangoFolder
tango_with_django_project
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
When I opened the project from
tango_with_django_project
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
It found the module rango.
Go to Preferences > Project Interpreter and set the project interpreter and path mapping. Also set the Project Structure accordingly.
Related
I have this project directory structure:
.
.pylintrc
|--myproj .
|--myapp
|--myproj (settings.py is here)
__init__.py
manage.py
.
In settings.py, INSTALLED_APPS I have the first entry 'myapp'.
From the root folder (containing .pylintrc), I call
$ DJANGO_SETTINGS_MODULE=myproj.myproj.settings pylint myproj --load-plugins pylint_django
However, I get error no module named 'myapp'. If I change the INSTALLED_APPS entry to 'myproj.myapp', then it is able to continue, but now I'm unable to start the project normally with manage.py runserver.
pastebin myproj.settings
What am I doing wrong, and what can I do to proceed?
Likely your settings.py is improperly configured, you need to do something like this:
settings.py
INSTALLED_APPS = [
...
'myapp.apps.MyappConfig',
...
]
If you open the apps.py file in your myapp directory you'll see the config class that should be included in INSTALLED_APPS
The same problem happened to me and I find my solution.
In My Case, I missed my urls.py file.
re_path(r"^chaining/", include("smart_selects.urls")),
to
re_path(r"^chaining/", include("packages.smart_selects.urls")),
I just started testing out PyCharm on my existing Django project, and it doesn't recognize any imports from apps within my project:
in my_app1/models.py:
from my_app2.models import thing
"Unresolved reference 'my_app2'"
Why is this? My project's directory structure matches the recommended layout, and it runs without errors, it's just PyCharm's magic doesn't want to work on it.
It seems related to this question:
Import app in django project
But I can't figure out what I am doing wrong. If I try:
from ..my_app2.models import thing
The PyCharm error goes away and it can auto predict, etc. But when I run the project Django throws:
ValueError: attempted relative import beyond top-level package
EDIT:
Project structure:
my_project/
src/
manage.py
db.sqlite3
my_app1/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_app2/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_project_app/
settings/
__init.py__
urls.py
...
I was having this issue using a "2 Scoops of Django" project layout, e.g.
/project_root
/project_dir
/config
/settings
/my_app
/tests
models.py
/requirements
readme.rst
The code was working, but in /tests, IntelliJ/PyCharm showed an unresolved reference:
from my_app.models import Something
I had all the __init__.py files in place. I ended up having to set the sources root to project_dir:
Right-click on project_dir, Mark Directory as > Sources Root
Now that I can take a look over you project structure I can tell you that the problem appears to be related to a missing __init__.py in your 'src' folder. Try adding an empty file named __init__.py in the root of 'src' folder.
Also, take a look to this question, I think is the same problem or a very similar one.
Hope this could be useful, cheers!
I was having this issue after I change my environment to virtualenv, so I changed my python interpreter to my current virtualenv.
Go to File > Settings > Project Interpreter.
In that window you would be able to see all packages includes on this interpreter, Django should be there.
This worked for me.
Link about: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206598665-Unresolved-Reference-Errors-for-django
ı have a django project and i need to access some of models in my folder that under the django main project folder.Let me illustrate this.
src\
main\
urls.py
models.py
view.py
lib\
__init__.py
helper.py
This is the example folder structure and i need to import some class of main app's models inside the helper.py.I tried these:
from main.models import exampleClass
from ..main.models import exampleClass
And i also tried adding a __init__.py file in the main project folder:
src\
...
main\
lib\
__init__.py
Always errors 2 kind :
1)ValueError : relative import error
2) no module name..
I need the solution and need good explanation why i failed always.Thank you so much guys.
Add __init__.py in main folder instead of src folder. Then try to import using from main.models import exampleClass. It should be working.
You don't need .. if main and lib are both django's apps, and you have registered them in INSTALLED_APPS settings.
If main and lib are in the same level that manage.py:
src/
main/
...
lib/
...
manage.py
...
You just need:
from main.models import exampleClass
How did you set $PYTHONPATH variable ? The search paths are relative to this environment variable.
So if you want to specify a path like main.models, it should contain the src directory.
Note that you can also manage it with the sys.path array.
Django normally add all the applications to sys.path. You may try to print it inside the settings.py file to have an idea.
To add a path from the settings.py file of the project, you could do something like:
import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(os.path.join(BASE_DIR, "../lib"))
If for example you have a lib directory at the same level as the directory that contains the settings.py file.
I have a django app that I developed in the 1.2 days. I'm now trying to port it over to the 1.4 project format.
The old way my project was set up was as follows:
django_project/
settings.py
manage.py
urls.py
app1/
app2/
app3/
I'm changing it to use the new manage.py and my directories look like this:
django_project/
manage.py
project
urls.py
wsgi.py
app1/
app2/
app3/
The problem is that all over my code I import stuff like this:
from app1.models import SomeModel
which now gives me an import error. Doing this fixes it:
from project.app1.models import SomeModel
I really don't want to have to go all through my project to change all those imports. Is there something I'm missing? Is there an easier way? Or is this how you're supposed to do it?
You should not put your apps into the project module. Django's startapp puts them in the project root, as it was before. project module is a place for project-wide settings, urls and such stuff only. Your apps should stay outside, in project root.
You can keep your current layout, since it will works fine. For new projects I think you can start to put your apps inside the 'project' module. If you check the 1.4 Release Notes you'll see it's the recommended layout. But if you are developing generics apps (that you can use in more than one project) probably the better place is project root.
I am trying to include the following Tag In Google App Engine Web Application:
http://www.djangosnippets.org/snippets/1357/
Is there any configuration of this file to make it work with Google App Engine?
Cause I followed the Django Template tutorials: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
and have this structure:
templatetags/
__init__.py
range_template.py
in the Template file, I have {%load range_template%}
But I am getting the error:
TemplateSyntaxError: 'range_template' is not a valid tag library: Could not load template library from django.templatetags.range_template, No module named range_template
The other thing that might be a problem why this ain't working is, the INSTALL_APPS settings.py file. Not sure how to configure it.
I have a settings.py file in the root of my application and included this:
INSTALLED_APPS = ('templatetags')
Any advice would be greatly appreciated.
try doing the following:
$ python ./manage.py startapp foo
Add foo to installed apps:
INSTALLED_APPS += ('foo',)
And move your templatetags directory into your foo app. Something like:
./djangoproject
__init__.py
settings.py
urls.py
etc..
foo/
__init__.py
templatetags/
__init__.py
range_template.py
Django convention is that template tag code resides in apps, in directories named templatetags (see docs). I assume the same would be true for GAE.
In case someone searches for this, I wrote a small article in 2008 about this: http://daily.profeth.de/2008/04/using-custom-django-template-helpers.html
Please make sure to restart the development server after following the above step