Registered models do not show up in admin - django

I added a model to admin via admin.site.register, and it does not show up in admin. Since admin is so "It just works", I have no idea of how to debug this. Pointers?

After adding and registering your admin:
# app/admin.py
class YourModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
Make sure your app is in your project settings.py:
# settings.py
INSTALLED_APPS = (
# other apps ...
'app',
)
Sync your project for that model if you have not done so already:
python manage.py syncdb
Restart your server, CTRL-C:
python manage.py runserver

In such a situation is also a good practice to check if the user logged in to the admin panel has rights to manage such a model. If they do then you could change your code to access the functions as root.

When in doubt, shut down server, syncdb, start server.

I have the experience, that sometimes after changing an admin.py the dev-sever won't be restarted. in that case touch settings.py helps.

I think the checklist in Thierry's answer is almost definitive, but make sure that urls.py contains admin.autodiscover() to load INSTALLED_APPS admin.py modules.
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)
More info in the django docs.

Have you added the application to your installed apps? That has happened to me both one and two times. :) Otherwise it would be useful for us to see the code to help you.

Also make sure there are no syntax errors in your admin.py or anything. That can cause an app to fail to be registered with the AdminSite.

I've faced the same problem, but it was a little tricky than yours.
Consider, that you have a project with, say, five or even more apps. For me it is more obvious to register all models in just one admin.py file, so I have decided to do it in one place - core directory. Of course, it was not an app, so none of models showed up on admin page.

comment out the some lines in urls.py see docs for more details
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)

Related

Why is the Django-Simple-Captcha image not showing up?

I'm trying to add a Django-Simple-Captcha image to my application's login screen.
This is what I have added at the top of my forms.py file:
from captcha.fields import CaptchaField
This is what I have added to the registration form:
captcha = CaptchaField(
label="What does this say?",
required=True,
)
This is what I added to my site's url.py file:
urlpatterns += patterns(
'',
url(r'^captcha/', include('captcha.urls')),
)
I have also added "captcha" to my INSTALLED_APPS in settings.py
However, when I load the page, I see that the Captcha image is a broken link: http://predictstat.com/accounts/register/. The server shows this on the console:
[23/Dec/2013 16:30:47] "GET /captcha/image/56edd656ba57a2a3e71571373e1a59c564e3d592/ HTTP/1.1" 500 72336
However, there is no such directory "captcha" under the directory for my application. So where is it trying to look for this image? And why doesn't it exist?
On closer inspection, I found the issue was this:
Exception Value: The _imagingft C module is not installed.
Details how to solve this issue can be found here: Python: The _imagingft C module is not installed
There is no need to create any captcha directories.
The problem is that you did not update your urls.py as mensioned in the documentation:
urlpatterns += patterns('',
url(r'^captcha/', include('captcha.urls')),
)
Another problem could be that you did not run syncdb:
./manage.py syncdb
./manage.py migrate # If you use migrations
The same was happening in my application and I manage to make a very strange workaround (XGH alert) usising two slashes in django-simple-captcha 0.4.4
urlpatterns += patterns('',
url(r'^captcha//', include('captcha.urls')),
)
I think it may has something with the conflict of globally and virtureenv environment,may be there is another app which contains the name captcha.

My DJango app is responding to /static urls

Update
I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!
Update
I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:
http://127.0.0.1:8000/static/admin/css/base.css
When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
def page_not_found(request, template_name='404.html'):
return HttpResponse("Could not find the resource you asked for...")
handler404 = 'hellodjango.urls.page_not_found'
and here's the urls.py under my polls directory:
from django.conf.urls import patterns, url
# Uncomment the next two lines to enable the admin:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Help?
It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:
from django.conf.urls.static import static
# ...
urlpatterns = patterns('',
# ...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
# ...
This should work for you -- go to /static/foo.css, and you should see your CSS.
It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.
The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?
INSTALLED_APPS = (
# ...
'django.contrib.staticfiles',
)
In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).
Glad you figured it out. Here's why it works like this.
django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.
The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Redirect in Django admin page

In my django project, after activating the admin application, I canno't get access to the page localhost/soundaxis/admin because I'm redirected to localhost/admin.
I think the problem has to do with the fact that the project was not located at the base address.
Urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^soundaxis/', 'views.index.home'),
(r'^soundaxis/admin/', include(admin.site.urls)),
)
Start the server by typing the following in the base dir:
python manage.py runserver
then go to http://localhost:8000/
Its rather complicated to get the project to run off another subdirectory so I suggest running it from the base, especially during development. Later in the game you can try work it from a subdir if required, but better yet would be subdomain.

django ignoring admin.py

I am trying to enable the admin for my app. I managed to get the admin running, but I can't seem to make my models appear on the admin page.
I tried following the tutorial (here) which says:
(Quote)
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)
(end quote)
I added an admin.py file as instructed, and also added the following lines into urls.py:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
...
(r'^admin/', include(admin.site.urls)),
)
but it appears to have no effect. I even added a print 1 at the first line of admin.py and I see that the printout never happens, So I guess django doesn't know about my admin.py. As said, I can enter the admin site, I just don't see anything other than "groups", "users" and "sites".
What step am I missing?
You need to ensure you have app containing Poll listed in INSTALLED_APPS :)
Also: If you're adding the admin.py file with the dev server running, make sure to restart it. This tripped me up for a minute. :)

Django admin App

I am building a app. The app will build a Poll OR a Quiz.
So my models will have a type field(Poll, Quiz)
Now i would like to display the 2 "Types" in the admin App list. But i dont what to create two apps Poll and Quiz. Is there a way, to display the 2 options in the list and then when you click on lets say Poll, the type field is set to Poll and then you fill in the rest of the models fields.
Thanks
have a short look to the second tutorial page of django. It describes the how to do that.
http://docs.djangoproject.com/en/1.1/intro/tutorial02/#intro-tutorial02
You need to activate the admin site:
Add "django.contrib.admin" to your INSTALLED_APPS setting.
Run python manage.py syncdb.
update urls.py
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
add the next line to urlpatterns
(r'^admin/', include(admin.site.urls)),
2 . You need to add your models to the admin interface
You only have to create a admin.py in your application directory (e.g. polls) and fill in the following content:
from mysite.polls.models import Poll, Quiz
from django.contrib import admin
admin.site.register(Poll)
admin.site.register(Quiz)
you have to change the first line of course to fit with your project name.
Hope this will help!
alas!I figured it out!
What you use is a Django Proxy Model
http://docs.djangoproject.com/en/1.1/topics/db/models/#id8
So I set up a Proxy model in my models.py file and then in admin.py I just used the proxy models as Admin.