Python Error: name 'admin' is not defined
Solution 1
from django.config import admin
should be from django.contrib import admin
Solution 2
at the top of your **url.py**
file, add the following code
from django.contrib import admin
admin.autodiscover()
So it that particular block should look something like the following
from django.conf.urls import patterns, include, url
**from django.contrib import admin
admin.autodiscover()**
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'examplesite.views.home', name='home'),
# url(r'^examplesite/', include('examplesite.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)),**
)
Solution 3
i change my urls.py like this... this should be the basic format
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
Solution 4
Well after an long, agonizing quest to fix this stupid problem, I FINALLY came across the answer! Another Django programmer ran into the same problem and found this:
IN THE PARENT OF ChoiceInLine (which you'll see as 'admin.StackedInline' in the tutorial), THE L IN StackedInline SHOULD NOT BE CAPITALIZED ... It's as simple as that ... thankyou SO much Karen Tracey ...
Ty Bailey
Full stack developer specializing in PHP/MySQL Web Applications and React Native mobile applications.
Updated on July 21, 2020Comments
-
Ty Bailey almost 4 years
I am creating a Python application in Django for the first time. I know that I must uncomment the admin tools in the urls.py, I have done that. I have also added
autodiscover
. Everytime I try to add a new feature to the administration panel, I get this error:"NameError: name 'admin' is not defined"
Here is the code I am using in my model to add to the admin panel:
class ChoiceInline(admin.StackedInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline]
here is the code in the python terminal I am using
admin.site.register(Poll, PollAdmin)
and here is the code from my urls.py:
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'iFriends.views.home', name='home'), # url(r'^iFriends/', include('iFriends.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)), )
Anyone have any idea why it cannot find the admin name?
EDIT
Here is my entire model file:
from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField() def __unicode__(self): return self.choice_text #COMMENTED OUT UNTIL I FIX THE ADMIN NAME from django.config import admin class ChoiceInline(admin.StackedInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline] #ADD THIS TO THE MAIN PYTHON FUNCTION admin.site.register(Poll, PollAdmin)
-
Don Cruickshank almost 11 yearsDoes this answer the question?
StackedInline
seems to be capitalized correctly in the question.