I'm new to Django and i've already come across a problem. I'm using Django 1.4.3 on OSX Mountain lion.
When I start a new app using
django-admin.py startapp "name"
the app is created and all the necessary files are within it (__Init__.py, models.py, tests.py, views.py). However, the admin.py file which should be automatically created is not in the app folder. Without it, i cannot edit my administrator site preferences.
Any ideas as to why this may be happening?
You have to create the file manually since the django admin is disabled by default.
Instructions on what to put in admin.py are here.
You are most likely reading the dev tutorial but using a stable release (currently 1.4.3). admin.py is created by startapp as of this commit which also updated the tutorial documentation but it won't make it into a stable release until 1.6.
Admin.py is not generated. In response to OP's to your comment on the post above:
From part 1 of the tutorial: https://docs.djangoproject.com/en/1.4/intro/tutorial01/
Let’s look at what startproject created:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
And on part 2, under the section "Make the poll app modifiable in the admin" it says:
But where’s our poll app? It’s not displayed on the admin index page.
Just one thing to do: We need to tell the admin that Poll objects have an admin interface. To do this, create a file called admin.py in your polls directory, and edit it to look like this:
from polls.models import Poll
from django.contrib import admin
admin.site.register(Poll)
Related
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
I have a python application (which we'll call app) I'd like to have a web front for. The application has many files and it's important the folder tree structure stays the way it is.
I've built a Django project and put it in a folder named "Web" in the app's folder, so now the folder tree looks like so:
[Data]
[Resources]
[Web]
[WebFront]
normal django app files
[Web]
__init__.py
settings.py
urls.py
wsgi.py
__init__.py
manage.py
main.py
Here's the code on the app's main.py:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.Web.settings")
django.setup()
This code causes an exception on the django.setup() line as (I think) django does not find the project modules: ImportError: No module named WebFront (WebFront is the name of the django app)
I suspect this is caused because django runs in the directory of python app, and therefore cannot find the folder WebFront - Which should actually be Web/WebFront
Can this be done? Or should I reverse the order and put the python app in the django app?
This is not a duplicate of the following questions as the folder nesting causes a different problem (I think)
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
Easiest way to write a Python program with access to Django database functionality
Using only the DB part of Django
You can locate your main.py script where you like. However, if it is outside of the Web folder, then you will have to add Web to the Python path, otherwise imports like import Webfront are going to fail.
import sys
sys.path.append('/path/to/Web/')
Once you have done that, you can change the DJANGO_SETTINGS_MODULE to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.settings")
I want to install the external app django-disqus in my blog. However, instead of install the module in the system via pip install or python setup.py install, I would like to download the code to a specific folder called libs and then link it to my project.
My folder structure is like this:
-root_folder
-- project (here I have settings.py, urls.py and wsgi.py)
-- blog (here I have models.py, admin.py, urls.py, templatetags/, template/)
-- libs (here I want to add the code of disqus)
If I downloaded the code in libs, how can I link it to INSTALLED_APPS in setting.py?
Note: I run django 1.8
You should be able to register it the same way you'd register any django app.
Make libs a package by adding __init__.py file
Then add it to your settings.INSTALLED_APPS
INSTALLED_APPS = (
...
'libs.disqus',
)
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