No module named simple error in Django - django

I am working on a Django Bookmarks project and it requires you to call the simple.py from django.views.generic. But when I ran my server I got an import error that their was no module named simple. I looked in the folder and it was not there. I looked up some information on the issue. I read that in the newer version through git hub it does not have the file. I cannot figure out how to fix it. Any help would be much appreciated!
import os
from django.conf.urls import patterns, include, url
from django_bookmarks.bookmarks.views import *
from django.views.generic.simple import direct_to_template
site_media = os.path.join(
os.path.dirname(__file__), 'site_media'
)
urlpatterns = patterns('',
(r'^$', main_page),
(r'^user/(\w+)/$', user_page),
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^logout/$', logout_page),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': site_media}),
(r'^register/$', register_page),
(r'^register/success/$', direct_to_template,
{'template': 'registration/register_success.html'}),
# Examples:
# url(r'^$', 'django_bookmarks.views.home', name='home'),
# url(r'^django_bookmarks/', include('django_bookmarks.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)),
)

It seems function based views have been deprecated in Django 1.3 (and may have been removed in the latest dev)
You should then replace them with the class-based views provided in Django 1.4
Deprecated function-based generic views
Class based views
You might be interested in the TemplateView.

django.views.generic.simple was deprecated and doesn't exist beyond django 1.4. Is there a specific reason you are using a development branch of django and not the latest stable version (1.4.2)?
I would recommend either using django 1.4.2 (which has django.views.generic.simple or use render (from django.shortcuts import render) instead of direct_to_template.

Related

Display static page in Django

I am trying to display contents of a static page in Django project.
urls.py :-
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'spollow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
url(r'^admin/', include(admin.site.urls)),
)
index.html is in the same directory as urls.py
I am getting 500 internal server error. Any ideas where I am going wrong?
First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.
On Django 1.5 or newer you can use TemplateView
Here's the example from the documentation
https://docs.djangoproject.com/en/dev/topics/class-based-views/
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
You can use the direct_to_template view on Django 1.4 or older
Here's the relevant documentation
https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
If it is the latter, I would use a module instead of string, (look at the import on the example).
Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.
Bonus note
If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.
If Your error is due to unable to find index.html
if yours is an app(ie: created by python manage.py startapp <app>) then:
Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.
so you need to create a folder templates inside your <app> and put index.html inside it.
if you don't have any apps, you want to add the template path manually :
open settings.py, then edit TEMPLATE_DIRS
TEMPLATE_DIRS = (
# Put the full path of the template dir here, like "/home/html/django_templates" or
# "C:/www/django/templates".
)
In Django 1.5 or newer you can use the render function instead of direct_to_template:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'django.shortcuts.render', {'template_name': 'index.html'}),
)
Or if you prefer the more complex way :), you can use class-based TemplateView:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
)

Why does Django throw a warning when loading my URLconf?

I have a Django app running on a production server. It is handled with gunicorn 0.14.2 behind nginx. When I reload the app (by reloading the gunicorn workers), I get this error:
Traceback (most recent call last):
File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/handlers/base.py", line 101, in get_response
request.path_info)
File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/urlresolvers.py", line 250, in resolve
for pattern in self.url_patterns:
File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/urlresolvers.py", line 283, in _get_url_patterns
raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name)
ImproperlyConfigured: The included urlconf myapp.urls doesn't have any patterns in it
Others with this problem have commonly noted that it occurs while using reverse in a URLconf, but I am not using reverse in any URLconfs (nor are they used in any third-party apps). Also, this error only occurs in production -- never in development (using the Django dev server) or on my staging server (using gunicorn 0.14.2 behind nginx). It also doesn't seem to cause trouble with the site at any other time then during reloads.
Any ideas what's causing the problem?
Here's the main URLconf (and the one referenced in the stack trace):
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
from django.http import HttpResponse, Http404
from django.views.generic.simple import direct_to_template, redirect_to
from myapp.apps.magazine.views import *
from myapp.apps.books.views import *
from myapp.apps.forms.views import *
from myapp.apps.blogext.views import *
from myapp.apps.sharing.views import expand_url, email_link_send
from myapp.apps.magazine.feeds import *
from satchmo_utils import urlhelper
from satchmo_store.urls import urlpatterns
from myapp.apps.myapp_shop.views import *
admin.autodiscover()
if settings.SHOP_BASE == '':
shopregex = '^'
else:
shopregex = '^' + settings.SHOP_BASE[1:] + '/'
myapp_patterns = patterns('',
# calendar
(r'^calendar/', include('events.urls')),
# for multimedia SWF access
#(r'^crossdomain.xml$', direct_to_template, {'template': 'crossdomain.xml'}),
# intercept checkout
# TODO: need to use a config value
(r'^catalog/checkout/$', 'myapp.apps.cart.views.myapp_checkout', {'SSL': not settings.LOCAL_DEV}, 'myapp_checkout'),
(r'^catalog/add/$', 'myapp.apps.cart.views.smart_add_wrapper', {}, 'myapp_smart_add'),
# URLs for NYRB apps
(r'^$', direct_to_template, {'template': 'newhomepage.html'}),
(r'^newhomepage/$', direct_to_template, {'template': 'newhomepage.html'}),
(r'^mobile/$', redirect_to, {'url': '/', 'permanent': True}),
(r'^books/authors/', include('myapp.apps.books.urls.authors')),
(r'^books/', include('myapp.apps.books.urls.books')),
(r'^articles/', include('myapp.apps.magazine.urls.articles')),
(
r'^mobile/articles/archives/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
redirect_to,
{'url': '/articles/archives/%(year)s/%(month)s/%(day)s/%(slug)s/', 'permanent': True},
),
# for national poetry month (april)
url(
regex = r'^national-poetry-month/',
view = poetry_month,
name = 'poetry_month',
),
(r'^issues/', include('myapp.apps.magazine.urls.issues')),
(r'^contributors/', include('myapp.apps.magazine.urls.contributors')),
(r'^galleries/', include('myapp.apps.magazine.urls.illustrations')),
(r'^multimedia/', include('myapp.apps.multimedia.urls.multimedia')),
(r'^online/$', direct_to_template, {'template': 'online.html'}),
#(r'^search/', include('myapp.apps.search.urls')),
(r'^search/', include('solango.urls')),
(r'^textareas/', include('myapp.apps.textareas.urls')),
(r'', include('myapp.apps.forms.urls')),
(r'^utils/', include('myapp.apps.utils.urls')),
#(r'^rss/$', 'myapp.apps.magazine.views.rss_list'),
(r'^rss/huffpo/$', redirect_to, {'url': '/articles/feeds/huffpo/', 'permanent': False}),
(r'^rss/googlenews/$', redirect_to, {'url': '/articles/feeds/googlenews/', 'permanent': False}),
(r'^newsletter/', include('myapp.apps.newsletter.urls')),
(r'^subscriptions/', include('myapp.apps.newsubscriptions.urls')),
(r'^shared/', include('myapp.apps.sharing.urls')),
(r'^counter/', include('myapp.apps.counter.urls')),
# Redirects for legacy NYRB system
(r'^nyrev/(\w+)', uber_legacy_article_redirect),
(r'^nyrev/', redirect_to, {'url': '/'}),
(r'contents/(?P<legacy_date>\d+)/', legacy_issue_detail_redirect),
(r'^archives/browse/?$', legacy_browse_archives),
(r'^gallery/gallery-browse/?$', legacy_gallery_browse),
(r'^gallery/', legacy_illustration),
(r'authors/(?P<legacy_author_id>\d+)/', legacy_author_detail_redirect),
#(r'shop/product', legacy_book_detail_redirect),
(r'^shop/product/?$', legacy_product),
(r'^myapp/browse/?$', legacy_book_browse),
(r'blogs/myapplog/post/(\d+)/(?P<slug>[-\w]+)/$', solango_blogsearch_redirect),
# URL shortening
(r'^u/(?P<short_url>[a-zA-Z0-9]+)/$', expand_url),
# NYRB shop
(r'^shop/', include('myapp.apps.myapp_shop.urls')),
(r'^admin/shop/order/csv/?', csv_order_export_day),
(r'^admin/shop/order/(?P<order_id>\d+)/csv/?', csv_order_export),
# URLs for Savoy apps
(r'^tags/', include('savoy.contrib.sections.tag_urls')),
(r'^podcasts/', include('savoy.contrib.podcasts.urls')),
(r'^blogs/$', redirect_to, {'url': "/blogs/myapplog/", 'permanent': False}),
(r'^blogs/', include('savoy.contrib.blogs.urls')),
(r'^media/', include('savoy.core.media.urls')),
# this is to use our own edit profile view
(r'^users/(?P<username>.+)/edit/$', edit_profile),
(r'^users/', include('savoy.core.profiles.urls')),
# django-authopenid
(r'^account/getusername/', get_lost_username),
#(r'account/signin/?', 'myapp.apps.forms.views.dual_login'),
(r'account/signin/?', 'django.contrib.auth.views.login', {'template_name': 'authopenid/signin.html'}),
(r'account/signout/?', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r'account/sendpw/?', 'myapp.apps.forms.views.dual_sendpw'),
(r'account/resetpw/?', 'myapp.apps.forms.views.reset_pw'),
(r'account/signup/?', 'myapp.apps.forms.views.link_and_signup'),
#(r'^account/', include('django_authopenid.urls')),
# django-mailfriend
(r'^mail_friend/send/?', email_link_send),
(r'^mail_friend/', include('mailfriend.urls')),
# django.contrib.comments
# Django admin (Satchmo additions):
(r'^admin/print/(?P<doc>[-\w]+)/(?P<id>\d+)', 'shipping.views.displayDoc'),
(r'^admin/product/configurableproduct/(?P<id>\d+)/getoptions/', 'product.views.get_configurable_product_options'),
# Orders
(r'^admin/open-orders/$', 'myapp.apps.myapp_shop.views.open_orders'),
# Institutional subscription CSV
(r'^admin/subscriptions/institutionalsubscription\.csv', 'myapp.apps.subscriptions.views.institutional_sub_csv'),
# COUNTER admin for institutional reports
#(r'^admin/institution-counts/multiple-report/csv/$', 'myapp.apps.subscriptions.views.institution_multiple_csv'),
#(r'^admin/institution-counts/multiple-report/$', 'myapp.apps.subscriptions.views.institution_multiple_report'),
(r'^admin/institution-counts/institutions/(?P<id>\d+)/csv/', redirect_to, {'url': '/counter/%(id)s.csv'}),
(r'^admin/institution-counts/institutions/(?P<id>\d+)/$', redirect_to, {'url': '/counter/%(id)s/'}),
#(r'^admin/institution-counts/process_file/?$', 'myapp.apps.subscriptions.views.institution_process_file'),
(r'^admin/institution-counts/$', redirect_to, {'url': '/counter/'}),
# Django admin (standard)
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
# custom feeds
(r'^feed/author/(?P<slug>[-\w]+)/$', AuthorFeed()),
)
# attach satchmo patterns after our patterns so we can override if needed
from satchmo_store.urls import urlpatterns
urlpatterns = myapp_patterns + urlpatterns
if settings.DEBUG:
urlpatterns += patterns('django.views.static',
(r'media/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}),
)
My guess is it has to do with your satchmo patterns and overloading on the name "urlpatterns" which django specifically looks for.
Try
from satchmo_store.urls import urlpatterns as satchmo_patterns
urlpatterns = myapp_patterns + satchmo_patterns
#etc.
The strange behavior may be bacause before if settings.DEBUG, urlpatterns is empty for some reason.
When running the dev server, it gets populated with the urlpatterns from the static app. When running in production, it remains empty.
You can check this easily with a clause like
if urlpatterns:
# something is in urlpatterns
# What's its type, contents, ...?
else:
# urlpatterns is empty
Once you figured this out, you can dig deeper. You can also add the check in a unit test or do it directly in the python shell, by importing the urls module if you want to make it more robust.
I think, and this is just a hunch, your urls maybe fine, (since no issues arise in your staged dev machine, only reloading on prod) the problem maybe with the way gunicorn re-loads the modules, and the way python compiled code behaves between different versions of the interpreter and how iterators behave, but again this are all hunches.
I recommend that you use the same version of python that you have on production in your staged machine and see if you can recreate the error if there already same version, then it must be something else, the reason I ask is that most production machines tend to use older versions of python, though I could be wrong.
good luck.
Instead of the urlpatterns = myapp_patterns + urlpatterns stuff, surely we could instead call myapp_patterns urlpatterns off the bat, and have inside the end of it, something along the lines of:
(r'^', include('satchmo_store.urls')),
As this is the "Django" way to include URL patterns?
This might solve your issue, I'm not sure. Worth a shot, I guess.

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 cannot find admin.sites.url

I am developing a django site locally using rc1.3 and after making several adjustments to my urls.py file i am receiving error messages that django cannot import admin.site.urls. by urls.py file is below.
urlpatterns = patterns('',
# Example:
(r'^city/$', 'venue.views.city_index'),
(r'^accounts/', include('registration.urls')),
(r'^accountss/', include('registration.backends.simple.urls')),
(r'^profiles/', include('profiles.urls')),
(r'^admin/', include('admin.site.urls')),
(r'^city/(?P<city>[-\w]+)/$', 'venue.views.city_detail'),
(r'^city/(?P<city>[-\w]+)/venue/$', 'venue.views.venue_index'),
)
Any idea what might be cause this, thanks.
http://docs.djangoproject.com/en/dev/intro/tutorial02/#activate-the-admin-site
Don't include the string path, actually import the admin module and point to its urls.
from django.contrib import admin
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
)

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.