Django production - No db table load in admin - python

I'm building a Django app and now I'm in production. I have this problem: after performing manage.py syncdb (all it's ok) I go into admin and I can not find the models tables . My admin.py file is present and this is my file url.py:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'stambol.views.home', name='home'),
# url(r'^stambol/', include('stambol.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Where is the problem?

Permissions
First off, make sure you have permission to edit the missing models. It's common to be developing with a superuser account, and then to test your production deployment with a different non-superuser account. If you don't have at least read permission, the class won't be listed at all in the admin.
I think this is the most likely cause, but I will leave the rest since I had already written it.
Discovering your admin registrations:
One notable difference between runserver and a production server is that when you run runserver, it imports all your models.py files and validates the models. This does not happen in production, so if you register your models with the admin inside models.py you need to be sure to import that file so that code runs. You could do so in your main url conf.
The preferable solution is to do your registration in per-app admin.py files so they are picked up by autodiscover.
Settings:
You do need admin listed in installed apps, as #Pratik says. It also has some dependencies, as mentioned here. Installed apps should contain at least this:
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions'
'django.contrib.messages',
'django.contrib.admin',
#...
'myapp',
#...
]
Make sure the dir containing your app myapp is in your python path, so that myapp is picked up by autodiscover. This is working correctly for you already, or else you would get something like ImportError: No module named myapp.
Restarting the server:
Finally, just to recap what is buried deep within comments, you can restart your production server after making any code changes by touching your wsgi file: touch wsgi.py. Use tab-completion when you do this to be sure you're touching the existing wsgi file and not creating a new one thanks to a typo or some such. The wsgi file you're touching should contain something like this:
...
# tell django to find settings at APPS_DIR/mainsite/settings.py'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mainsite.settings'
# hand off to the wsgi application
application = WSGIHandler()
Still broken?
If things still aren't working as expected, think farther outside the box. Keeping in mind that you're new to your production environment, is it possible some other code besides your own is being served up? Make some obvious change to a front-end page, restart the server, and see if it works. This is just a shot in the dark, of course.

from django import admin
from example.models import YourModel
admin.site.register(YourModel)
# or
class ModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
This should do the trick

Make sure you have the below removed as comments inside INSTALLED_APPS in settings.py. Then run ./manage.py syncdb again. They should like as shown below without the # in front of them
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',

Related

Django serving each app separately in each its port

I've got a very simple project running on Django (no models yet) and I need to do the following:
I have created 2 apps, 'Ebony' and 'Ivory' that need to communicate with each other through JSON messages (originally designed to run on different machines but for now one is good enough).
The problem is that the Django Debug server is just one process which runs in a specific port. What I want to do is make each 'App' listen to its own port on the same server and if possible under the same Django project. Is such a scenario possible? And if yes, how should I go about it?
Thanks in advance
This is possible, but not the way you're conceptualizing it. A Django app is one part of what runs on a given web server. Thus a Django project, which has one or more apps, runs as a part of one web server.
The solution is to run multiple instances of Django. Not sure how well this is going to work for you with the debug servers. You can run each server on its own port by giving it a parameter telling it where to open the port, for example:
./manage.py runserver 8000
runs a debug server on 127.0.0.1:8000, and
./manage.py runserver 8080
runs another debug server on 127.0.0.1:8080. Usually this is done in separate shells.
You will need to make sure that the INSTALLED_APPS setting on one of these has 'Ebony' in it, and the other has 'Ivory'. You will also need to figure out some way to tell each instance how to connect to the other (usually by specifying a root URL).
That said, later on you will need to figure out if your two apps will be sharing the same database. If so, make sure that both machines can get to it. If not, make sure the DATABASES value in settings.py is different for each one. If you're sharing the database, Django's sites framework can help you keep things straight in your models.
To have both running from the same project, you have to tell Django which one to run. I prefer to use an environment variable. This changes the above runserver commands to:
SHARD=Ebony ./manage.py runserver 8000
and
SHARD=Ivory ./manage.py runserver 8080
In your settings.py file, this variable can be accessed through os.environ. So, for example, for the INSTALLED_APPS setting to have different values for each shard, you write something like:
SHARD = os.environ["SHARD"]
# Apps common to all shards go here.
LOCAL_APPS = [
commonApp,
]
# Add apps specific to each shard.
if SHARD == "Ebony":
LOCAL_APPS += [
Ebony,
]
elif SHARD == "Ivory":
LOCAL_APPS += [
Ivory,
]
# Add them to the apps that aren't mine.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
# ... omitted for brevity ...
'django_extensions',
'south',
'debug_toolbar',
) + LOCAL_APPS
By defining SHARD as a setting in this file, you avoid having to have all your code access the environment variable, and you confine the logic for setting SHARD to settings.py, in case you want to change it later. Your other Python files, if needed, can get the setting with from django.conf.settings import SHARD.
A similar mechanism can be used to give each shard its own DATABASES setting, too. And anything else in settings.py.
Then later in your urls.py file, you use that to pull in your apps' URLs:
from django.conf.urls import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'commonApp.views.get_homepage', name='home'),
url(r'^login$', 'django.contrib.auth.views.login', name="login"),
url(r'^logout$', 'django.contrib.auth.views.logout',
{"next_page": "/"}, name="logout"),
# Admin
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
# Auto-add the applications.
for app in settings.LOCAL_APPS:
urlpatterns += patterns('',
url(r'^{0}/'.format(app), include(app + '.urls', namespace=app)),
)
This means your apps need their own urls.py files, and your app URL names get prefixed with your app names. So if the app Ebony defines a URL pattern with name="index", you would get that URL in a template with {% url 'Ebony:index' %}.

Django accessing admin page and adding a new url - BEGINNER

I have created a DJango project in eclipse. Later i added a new application (R-CLick Project folder ---> DJANGO ---> Create application (manage.py startapp))
I names it Super.
Then again i created another new application (using the same steps described above), and named it Human.
In my project now, i have 2 applications created (In eclipse it appears as 2 packages).
I have a file called admin.py inside the package Super.
The code is as follows:
from django.contrib import admin
from Super.models import People
from Human.models import NormalHuman
admin.site.register(People)
admin.site.register(NormalHuman)
I even registered the 2 new applications in the Settings.py file.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Super',
'Human',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
I also made changes to the urls.py file.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from Human.models import NormalHuman
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^normal/', NormalHuman),
)
Problems i want to solve:
1.) After restarting the server, when i try to navigate to the url 127.0.0.1:9095/normal i end up in a 404
2.) I need to add NormalHuman to the admin page, so i have access to its content.
You have already registered admin url so you do not need to add
url(r'^normal/', NormalHuman),
to see content of NormalHuman model.
just simply hit 127.0.0.1:9095/admin/human/normalhuman to see content of NormalHuman model.

Why does my newly installed Django project only display the welcome page?

Right up front: I'm pretty new to python and django, but I've done the tutorial and some other basic stuff on a local install.
I recently started a django project on a live server (hostgator.com, with fastcgi) to the point where it shows that "It worked! Congratulations on your first Django-powered page." message. Next, I uncommented the appropriate lines in urls.py in order to enable the built-in django admin. I also figured out how to restart the fastcgi on my server just in case.
Upon going to /admin, it still displayed the welcome page. In fact, no matter what I put after the /, it displays the welcome page no matter what. I have also tried creating a blog app and getting that to display, but that also didn't work.
I get the feeling I've missed something painfully obvious, or there's something horribly wrong with my setup.
It is worth mentioning that my django project is set up in a subdomain. Thinking this was the problem, I tried writing a middleware that overwrote process_request from a tutorial here: http://djangosnippets.org/snippets/1119/. Still, the welcome page is the only thing that displays.
Any help would be greatly appreciated.
UPDATE:
I guess it would help if I put up some code, which you think I would have thought before.
Here is my urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^/$', 'blog.views.index'),
(r'^admin/', include(admin.site.urls)),
)
Here are the INSTALLED_APPS in settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
'blog',
)
I haven't added anything to any views.py just yet.
I have run syndb, and here is the list of the db tables:
auth_group
auth_group_permissions
auth_message
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_session
django_site
Are you sure you have restarted the server properly? Try putting some text in urls.py to provoke a syntax error to make sure the restart did in fact work.

Apps won't show in Django admin

I've read all the other threads but I still don't get why my apps are not showing up in Django admin. Everything else works fine.
My apps are in settings.py
I have admin.autodiscover in my root urls.py file
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', direct_to_template, {
"template": "homepage.html",
}, name="home"),
url(r'^admin/invite_user/$', 'signup_codes.views.admin_invite_user', name="admin_invite_user"),
url(r'^account/signup/$', "signup_codes.views.signup", name="acct_signup"),
(r'^account/', include('account.urls')),
(r'^profiles/', include('basic_profiles.urls')),
(r'^notices/', include('notification.urls')),
(r'^announcements/', include('announcements.urls')),
(r'^tagging_utils/', include('tagging_utils.urls')),
(r'^attachments/', include('attachments.urls')),
(r'^comments/', include('threadedcomments.urls')),
#
(r'^wayfinder/', include('wayfinder.urls')),
(r'^site/', include('jsite.urls')),
(r'^kiosk/', include('kiosk.urls')),
(r'^navigator/', include('navigator.urls')),
(r'^location/', include('location.urls')),
(r'^event/', include('event.urls')),
#(r'^news_reader/', include('news_reader.urls')),
#(r'^weather_reader/', include('weather_reader.urls')),
(r'^admin/(.*)', admin.site.root),
)
if settings.SERVE_MEDIA:
urlpatterns += patterns('',
(r'^site_media/', include('staticfiles.urls')),
)
All my apps have an admin.py file containing something like
from django.contrib import admin
from event.models import Event
class EventAdmin(admin.ModelAdmin):
list_display = (
'short_name',
'long_name',
'locations',
'categories',
'description',
'phone',
'email',
'url_source',
'url_location',
'external_ref',
'show_event'
)
admin.site.register(Event, EventAdmin)
And I have restarted the server over and over ;-)
I am building on top of Pinax, but from my reading, it shouldn't change anything. Any clue what might be wrong ?
Do you have your apps in the INSTALLED_APPS section in settings.py?
Make sure it has your apps listed there. My section reads
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'squick.items',
'cowsite.search',
'cowsite.posts',
)
for instance. I'm pretty sure for security, they won't show up in the admin unless they are in installed apps. I think I had this same issue, where I couldn't get cowsite to show up in the admin.
The Django docs say about the admin page: "By default, it displays all the apps in INSTALLED_APPS that have been registered with the admin application, in alphabetical order"
By coincidence I had the same problem this morning. Briefly, this is what worked for me (see references for details):
In the top level directory of MyApp (ie same directory as models.py, etc.) I added a python module admin.py, containing:
from models import ThisModel, ThatModel
from django.contrib import admin
admin.site.register(ThisModel)
admin.site.register(ThatModel)
Then in mysite directory I did syncdb and runserver, and ThisModel and ThatModel were in the admin interface.
Does that work for you?
Best wishes
Ivan
** References
(I am a new member so I am allowed to post one hyperlink only!)
Django tutorial: Make the poll app modifiable in the admin
There was also a query on the Pinax google group recently titled, "How to add my app to Admin in a Pinax project?"
Are you logging in to admin as a superuser? If not, it could be a permissions problem.
Not sure which version of django you're using but the current docs suggest including the admin urls.
('^admin/', include(admin.site.urls))
For other's coming across this, I had the same issue due to grappelli.dashboard being in the installed apps but not actually installed in the virtualenv, so do a pip freeze and ensure all your requirements are actually installed.
add your app name in "settings.py" file installed app.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
If the other solutions did not work for you, try to load your admin dashboard in a different browser. One of my apps was not displaying on the admin dashboard while I was using Google Chrome. After trying multiple answers others suggested, I decided to use Firefox instead. Voila! I was finally able to see my app on the admin dashboard.
You didn't answer Antony's question. Are you logging in as a superuser, or at least with a user with add/edit rights for the applications? If not, you won't see them.
I had the same problem, what worked for me was changing this line in urls.py:
url(r'^admin/', include(admin.site.urls)),
to
url('^admin/', include(admin.site.urls)),
(Removing the r in the first bit of code)
For some reason I am not aware of, the Polls became visible in admin after that.

Why does Django throw an exception whenever I enable admin.autodiscover()?

Here is my setup. I am using Django version 1.1.1 on Dreamhost, Python 2.4. The problem I am having is whenever I create a simple app and also have admin.autodiscover() enabled, Django will throw an exception. My setup:
from django.conf.urls.defaults import *
from testapp.views import HelloWorld
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^HelloWorld/$', HelloWorld),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
My settings.py looks like:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'testapp',
)
My testapp.views looks like:
from django.http import HttpResponse
def HelloWorld(request):
return HttpResponse("Hello world")
If I comment out admin.autodiscover() I can get to my view of HelloWorld. If I enable admin.autodiscover() Django throws an exception that I am not able to trap.
Does anyone know why this might be happening and what I can do to fix it?
I've had a similar issue when I've renamed an app. Basically, if you've launched the app and used the admin using admin.autodiscover() in your urls.py file, it will cause an admin.pyc file to be created in your app folder. Delete this admin.pyc file and run the server again...and voila!
I'm going to guess that testapp/admin.py does not import the models.Model class you are creating admin for. Try the following:
./manage.py shell # you may immediately get a stack trace
>> import testapp.admin # I'll bet it blows up.
Old question, but still relevant. I had a similar problem that stumped me today. The issue was that I had refactored a set of files in a directory within a Django app folder (lib/cache) into a single file (lib/cache.py). Because there was still an __init__.py in the cache directory, Python was seeing the empty directory as a module and preventing access to cache.py.
Commenting out admin.autodiscover() in my urls.py made it a bit easier to track this down but it still took some guesswork.

Categories

Resources