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. :)
Related
I am following a tutorial on http://www.lightbird.net/dbe/todo_list.html to create a simple todo app. In one of the steps, I had to modify view to add an ability in 'admin' to mark tasks as done from that view. However I get the error ImportError at /admin/ no module named todo.
The error is not thrown from any particular line from the code so I do not know how to debug this. I am new to django. So I documented my error in my blog here: http://djangounchain.wordpress.com/2013/01/10/tutorial-8-todo-list-app/
Hope someone can help me!
You are registering your models to AdminSite in todo/models.py itself.
As per official django documentation, you need to create admin.py file inside your app for admin.autodiscover() to work properly.
The last step in setting up the Django admin is to hook your AdminSite
instance into your URLconf. Do this by pointing a given URL at the
AdminSite.urls method.
In this example, we register the default AdminSite instance
django.contrib.admin.site at the URL /admin/
# urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
Above we used admin.autodiscover() to automatically load the
INSTALLED_APPS admin.py modules.
I can not get my models to display within the Admin Interface even after registering them in django-admin.py using admin.site.register(topic).
I have registered a model class topic, but it just isn't showing up in the interface. Instead, I'm getting groups and users in the auth section and sites in the sites section.
Below is the code I currently have. Any help would be appreciated.
models.py
class topic(models.Model):
topic_name = models.CharField(max_length=30)
description=models.CharField(max_length=255,null=True, blank = True)
class Admin:
pass
def str__(self):
return '%s''--' %(self.topic_name)
admin.py
from django.contrib import admin
from edc.kds.models import *
if __name == "main":
management.execute_from_command_line()
admin.site.register(topic)
You're doing some strange stuff.
1: You have some ancient, years old class Admin syntax which isn't necessary.
2: You have a strange if __name block in there. Clearly that should raise a NameError, but assuming you actually wrote __name__ == 'main', there's your problem.
__name__ is set to 'main' only if the file is directly executed. If it's executed by django machinery, the if block will never fire, and thus admin.site.register will never be called.
Where'd you get this idea?
# urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
In DjangoBook, it says that the every books app should have its own admin.py. However, none of my apps have there own separate admin.py as the text suggests. I was just wondering if this is a Django 1.3 thing and if so, where is the admin.py data stored now, if not in a separate admin.py file?
The chapter I'm referring to is here:
http://djangobook.com/en/2.0/chapter06/
P.S. I'm not talking about django-admin.py
By default there is no admin.py file created for you when you create a new app, you will need to create your own. Here are the directions on how to create the admin.py file.
http://docs.djangoproject.com/en/1.3/ref/contrib/admin/
Edit: 1.3 is no longer supported, here is a link to 1.8:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/
You need to create admin.py in new app and edit it with from django.contrib import adminIn project/urls.py, in my case (mysite/urls.py) specify the url of newapp/admin (polls/admin) likefrom django.conf.urls import patterns, include, url from django.contrib import admin
admin.autodiscover() urlpatterns = patterns('',url(r'^admin/', include(admin.site.urls)),)
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.
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)),
)