Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin.site.register doesn't add my app admin

Tags:

python

django

as a django newbie (I have some exprience with other python webframework like turbogears and bottle but exploring django) I'm trying to auto create the admin management for my app model

in tha main URLS.py I have:

edit:

from django.contrib import admin
admin.autodiscover()

and after that:

urlpatterns = patterns('',
                       url(r'^appname/',include('appname.urls')),
                       url(r'^admin/',include(admin.site.urls)) 

notice this is in the main urls.py and not in the app urls.py

following the tutorial (which did work for me in the tutorial..) I created an 'admin.py' file in the appname folder and there:

from appname.models import Appname
from django.contrib import admin


class appnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)

and in setting.py I have uncommented

'django.contrib.admin'

I don't get any error in the commandline window and the basic admin screen does appear (auth and sites)

I checked the imports in admin.py in the manage.py shell and everything seemed to work allright, I also tried commenting AppnameAdmin class out and registring just:

admin.site.register(Appname) 

but that didn't work eith

I'm guessing I'm missing something obvious - I'll be glad to help with that

using django 1.4 + python 2.72

like image 947
alonisser Avatar asked May 26 '12 21:05

alonisser


People also ask

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

What is the correct way to include Django's admin urls?

If you open a Django project's urls.py file, in the urlpatterns variable you'll see the line path('admin/', admin. site. urls) . This last path definition tells Django to enable the admin site app on the /admin/ url directory (e.g. http://127.0.0.1:8000/admin/ ).


3 Answers

Have you restarted the server process?

Maybe this helps someone: in my case the problem was solved by stopping and starting the server process, because when you add a new admin.py file it does not reload automatically.

like image 132
Martijn de Milliano Avatar answered Nov 03 '22 00:11

Martijn de Milliano


Check all of these:

There are seven steps in activating the Django admin site:

  1. Add 'django.contrib.admin' to your INSTALLED_APPS setting.
  2. The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and django.contrib.sessions. If these applications are not in your INSTALLED_APPS list, add them.
  3. Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to MIDDLEWARE_CLASSES. (These are both active by default, so you only need to do this if you’ve manually tweaked the settings.)
  4. Determine which of your application’s models should be editable in the admin interface.
  5. For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model.
  6. Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
  7. Hook the AdminSite instance into your URLconf.
  • Do you have all the other admin dependencies in your installed apps?
  • Do you have admin.autodiscover() in your URLS.py?

Also, I think your code should look something more like this:

from projectname.appname.models import Appname
from django.contrib import admin


class AppnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)
like image 41
DanS Avatar answered Nov 02 '22 23:11

DanS


aaargghhh - I found the problem. I saved admin.py in the template/appname/ folder instead of the appname/ folder. so stupid of me. so sorry for the interruption.

like image 44
alonisser Avatar answered Nov 03 '22 00:11

alonisser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!