NameError:Admin not found in django - django

The urls.py of the project is this
from django.conf.urls.defaults 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'^$', 'Sdr.views.home', name='home'),
# url(r'^Sdr/', include('Sdr.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)),
(r'', include('Sdr.sdr.urls')),
)
The urls.py of the app looks like this
# Import django modules
from django.conf.urls.defaults import *
from django.contrib import admin
# Import custom modules
import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('Sdr.sdr.urls')),
)
The error I am getting is exception found is
Django Version: 1.3
Exception Type: NameError
Exception Value:
name 'admin' is not defined

You forgot to import admin in the project's urls.py. Read harder.

Uncomment the following in your project's urls.py.
from django.contrib import admin
admin.autodiscover()

Faced with the same problem in tutorial.
Try to add in models.py:
from django.contrib import admin

Related

Adding simple views in django

I am getting started on django and this is how my view is looking like
from django.template import Context, loader
from datetime import datetime
from django.http import HttpResponse
def hello_view(request):
""" Simple Hello World View """
t = loader.get_template('helloworld.html')
c = Context({
'current_time': datetime.now(),
})
return HttpResponse(t.render(c))
def detail_view(request):
return HttpResponse("You're looking at detail view")
My urls.py file looks like this
from django.conf.urls import patterns, include, url
from posted.views import hello_view
#from posted.views import detail_view
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', view=hello_view, name='hello_page'),
#url(r'^$', view=detail_view, name='detail_page'),
# url(r'^posts/', include('posts.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)),
)
When i run the server and visit http://example.com:8000/ ,the hello_view gets displayed.I want my urls to be of the form http://example.com:8000/hello or http://example.com:8000/detail. Do i need .htaccess to achieve that?.
I have fixed the issue somewhat with this while i study what Daniel suggested i look at.
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
(r'^detail/', TemplateView.as_view(template_name="detail.html")),
# url(r'^posts/', include('posts.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)),
)

Moving to production environment broke my application

I've just deployed my application on dreamhost and it's stopped working when uploading files. Passenger just produces a 500 internal error. It works on my development set up.
My passenger file looks like:
import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), '/photosoc'))
os.environ['DJANGO_SETTINGS_MODULE'] = "photosoc.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I get the standard 404 error(debug is still enabled). I have had to change my urls file though from:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition import *
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.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)),
# url(r'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
To the following:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.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)),
# url(r'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
This is the only change I've made to the application from the switch from development to production. So what else could have gone wrong?
You may have a permission problem.
It seems like you are writing image files. Does the process that is running Django have permission to write files? When you are running Django locally with ./manage.py runserver Django will get the permissions of the user that runs from the command line, but in a typical production environment you'd be running with the permissions of the Apache user. This user may not have permission to write those image files. You could try temporarily hardcoding the write directory to /tmp to see if that makes a difference.
Of course, you will really need see that 500 error stack trace to know what's going on.
Are your settings exactly the same in what middleware classes your importing? Double check that these are the same.
Related:
ModSecurity: Output filter: Failed to read bucket (rc 104): Connection reset by peer

Why an extra slash is getting added to urls?

I've created a blog using django and hosted it on dotcloud http://www.honeybunny.dotcloud.com/blog/ its working fine on the localhost but when i try to access it online an extra slash is added to the urls what could be the reason ?
www.sitename.com/admin/
becomes
www.sitename.com//admin/
my urls.py is as following
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
import blog
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$',redirect_to,{'url':'/blog'}),
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/',include('blog.urls')),
)
Also submitting the login form on the admin redirects me to http://admin/ while in the local environment it works perfectly fine .
UPDATE : My problem seems remarkably similar to the one described here .
From django tutorials the url file should look like the one below.
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^articles/2003/$', 'news.views.special_case_2003'),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'honeybunny.views.home', name='home'),
# url(r'^honeybunny/', include('honeybunny.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'^$',redirect_to,{'url':'/blog'}),
url(r'^admin/$', include(admin.site.urls)),
)
Is my urls.py file
I have deleted import blog line because I don't have blog module.
It seemed to be a problem with dotcloud the same code worked without any errors when i uploaded it to epio

Django bug : ImproperlyConfigured at /databrowse/ . How to solve this bug?

My urls.py of the project looks like this
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib import databrowse
from world.views import welcome
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', welcome),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^databrowse/', include(databrowse.site.root)),
)
urlpatterns += patterns('',
(r'^media/$',include('django.views.static.serve'),{'document_root': settings.MEDIA_ROOT, 'show_indexes': True})
)
What changes have to be done for this to work ?
The only change you have to make is to follow the actual documentation. The documentation doesn't use include for databrowse.

Django Admin application no working on URL

In my urls file I have configured the Django admin application to run off the url /adminDJ/. However it doesn't run. It loads up my own admin stuff. Here is my urls.py:
(r'^admin/add/member/$', 'astonomyStuff.attendance.views.newMember'),
(r'^admin/add/$', 'astonomyStuff.attendance.views.addPage'),
(r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^adminDJ/$', include(admin.site.urls)),
(r'^talks/$', 'astonomyStuff.attendance.views.talksIndex'),
(r'^talks/past/$', 'astonomyStuff.attendance.views.viewAllTalks'),
(r'^members/$', 'astonomyStuff.attendance.views.viewMembers'),
(r'^members/(?P<member_number>[^/]+)/$', 'astonomyStuff.attendance.views.viewMember'),
(r'^members/(?P<member_number>[^/]+)/delete$', 'astonomyStuff.attendance.views.deleteMember'),
(r'^admin/add/talk/$', 'astonomyStuff.attendance.views.newTalk'),
(r'^talks/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewTalk'),
(r'^attendance/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewAttendance'),
(r'^databrowse/(.*)', databrowse.site.root),
(r'^adminDoc/doc/', include('django.contrib.admindocs.urls')),
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
(r'^accounts/profile/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^admin/add/attendance/$', 'astonomyStuff.attendance.views.addAttendance'),
(r'^members/(?P<member_number>[^/]+)/edit$', 'astonomyStuff.attendance.views.editMember'),
(r'^public/talks/$', 'astonomyStuff.attendance.views.publicViewTalks')
I have rearranged the order to see if this was the problem but that's not fixed it. Must the django admin application run on /admin/?
Edit
I have had the admin application working before just to let you know it only broke when I played around with the urls.
Edit 2
Here is my complete urls.py:
from django.conf.urls.defaults import *
from astonomyStuff.attendance.models import Member
from astonomyStuff.attendance.models import Non_Member
from astonomyStuff.attendance.models import Talk
from astonomyStuff.attendance.models import Event_Attendance
from django.contrib import admin
from django.contrib import databrowse
admin.autodiscover()
admin.site.register(Member)
admin.site.register(Non_Member)
admin.site.register(Talk)
admin.site.register(Event_Attendance)
databrowse.site.register(Member)
databrowse.site.register(Non_Member)
databrowse.site.register(Talk)
databrowse.site.register(Event_Attendance)
urlpatterns = patterns('',
# Example:
# (r'^astonomyStuff/', include('astonomyStuff.foo.urls')),
# 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/add/member/$', 'astonomyStuff.attendance.views.newMember'),
# (r'^admin/add/$', 'astonomyStuff.attendance.views.addPage'),
# (r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^admin/$', include(admin.site.urls)),
(r'^talks/$', 'astonomyStuff.attendance.views.talksIndex'),
(r'^talks/past/$', 'astonomyStuff.attendance.views.viewAllTalks'),
(r'^members/$', 'astonomyStuff.attendance.views.viewMembers'),
(r'^members/(?P<member_number>[^/]+)/$', 'astonomyStuff.attendance.views.viewMember'),
(r'^members/(?P<member_number>[^/]+)/delete$', 'astonomyStuff.attendance.views.deleteMember'),
# (r'^admin/add/talk/$', 'astonomyStuff.attendance.views.newTalk'),
(r'^talks/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewTalk'),
(r'^attendance/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewAttendance'),
(r'^databrowse/(.*)', databrowse.site.root),
(r'^adminDoc/doc/', include('django.contrib.admindocs.urls')),
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
(r'^accounts/profile/$', 'astonomyStuff.attendance.views.adminPage'),
# (r'^admin/add/attendance/$', 'astonomyStuff.attendance.views.addAttendance'),
(r'^members/(?P<member_number>[^/]+)/edit$', 'astonomyStuff.attendance.views.editMember'),
(r'^public/talks/$', 'astonomyStuff.attendance.views.publicViewTalks'),
)
Not this (r'^adminDJ/$', include(admin.site.urls)),
But this (r'^adminDJ/', include(admin.site.urls)), ##note, no $ in the regex
Remember folks, gotta check your regexes...
This is probably nothing but it caught my eye. From the first snippet:
(r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^adminDJ/$', include(admin.site.urls)),
And the second snippet:
(r'^admin/$', include(admin.site.urls)),
Is this how you want it? The first snippet uses ^adminDJ/$ while the second one uses admin/$ opposite include(admin.site.urls).
Are these two files? And are the both used? In that case the first snippet could very well override the second, thereby causing your admin views to show up instead of Django's.