Django Template Issue - django

I am following along on this tutorial, after I add the templates and put the pattern in urls.py, I go the website I get a template not loaded page.
This is my template dirs section from settings.py:
TEMPLATE_DIRS = (
"/home/django-projects/base/templates/",
)
the base folder hierarchy is this:
blog<dir>
dbs<dir>
__init__.py
manage.py
settings.py
templates<dir>
urls.py
urls.py looks like this:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('base.blog.views',
(r"", "main"),
# Example:
# (r'^base/', include('base.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
In templates I have a folder called blog, and it contains the two html files from the tutorial. I am just not sure what setting I am missing to display the proper templates. I will be happy to post anything else you would need to see. I am sure it is something trivial, I just can't seem to figure out what it is. Thanks for your help.
EDIT
So I was playing around with it, and I realized I couldn't get to admin. So I uncoupled the urls for the blog part away from the main. The urls.py on the top level looks like this:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'blog/', include('blog.urls')),
# Example:
# (r'^base/', include('base.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
And in the blog folder urls.py looks like this:
from django.conf.urls.defaults import *
urlpatterns = patterns('base.blog.views',
(r'^$', 'main'),
)
Now I can get to admin, but still get a template load error when I try to get to the blog.
TemplateDoesNotExist at /blog/
blog/list.html
Request Method: GET
Request URL: http://192.168.1.124:9999/blog/
Django Version: 1.2.3
Exception Type: TemplateDoesNotExist
Exception Value:
blog/list.html
Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/template/loader.py in find_template, line 138
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path: ['/home/kevin/django-projects/base', '/usr/local/lib/python2.6/dist-packages/setuptools-0.6c11-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Tue, 2 Nov 2010 11:59:13 -0500
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:

Should the last line of the "main" view contain "blog/list.html" instead of "list.html"?
Try replacing that line with the following:
return render_to_response("blog/list.html", dict(posts=posts, user=request.user))
Do the same thing with the "post" view.
Basically, Django is going to look under each of the configured template directories in settings.py and if you specify "list.html" it better be in the root of of of those directories. Since you put the "list.html" in the "blog" sub-directory and not where you told it to look, Django can't find it.

First, you have a missing leading / in your template directory:
TEMPLATE_DIRS = (
"/home/django-projects/base/templates/",
)

Related

Django admin panel works but main page gives me a 404

I'm brand new to Django and Python. I've only done php.
It took me three tries, but I've almost Django installed correctly. I followed the tutorial on installing it, and it worked. So I enabled the admin, panel, and set up mysql.
The admin panel works, and I've got my static folder so it has all of the css as well. But when I go to the main page I get this 404. (My project is called firstweb) So whenI go into firstweb there is a 404, but firstweb/admin works.
Using the URLconf defined in firstweb.urls, Django tried these URL patterns, in this order:
1. ^admin/
The current URL, firstweb/, didn't match any of these.
And I'm confused by the whole runserver thing. Is that some other little server other than my Apache? Why do I need to start it every time?
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'^$', 'firstweb.views.home', name='home'),
# url(r'^firstweb/', include('firstweb.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 looks like your url conf does not include any url pattern for any other pages accept the admin.
If you want the root url to display a page, uncomment this line
url(r'^$', 'firstweb.views.home', name='home'),
And then make sure that in firstweb/views.py you have a view function called 'home', which I assume is part of the tutorial.

django cms apphook error

I am learning django-cms. I tried to make custom plugin which was quite successful but when I tried to hook my custom made plugin to apphook, its giving me an error, saying,
No Module named urls
.
I followed the tutorial which was given in django cms sites documentation, and created the cms_app.py file. Currently my application directory has all the files which is required to make a custom plugin for django cms, and an additional file of cms_app.py.
Is something wrong with setting of the url or do I need to create a new urls.py file inside my app directory?
My cms_app.py is exactly the same as given in the tutorial.
i have created a project called myproject using command -
python django-admin.py startproject
myproject
After referring to the tutorial given for cms I created a plugin called first, using the basic command
python manage.py startapp first
Now the plugin is working perfectly well, and the directory structure before making an attempt to the apphook was,
first/
__init__.py
cms_plugins.py
models.py
tests.py
views.py
Now after making an attempt to hook the app in apphook, the directory structure is:
first/
__init__.py
cms_app.py
cms_plugins.py
models.py
tests.py
views.py
My cms_app.py is as follows:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class FirstApp(CMSApp):
name = _("First App") # give your app a name, this is required
urls = ["first.urls"] # link your app to url configuration(s)
apphook_pool.register(FirstApp) # register your app
i have a urls.py file in myproject folder, and it is as follows:
from django.conf.urls.defaults 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()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.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'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
I have restarted the server as was mentioned in the tutorial, but no success.
Any ideas as to what is wrong with my simple app?!
EDIT - 1
My views file is as follows:
from django.http import HttpResponse
def index(request):
“””Generate the context for the main summary page”””
return render_to_response(‘first/first.html’)
Edit - 2
I have changed my urls.py inside first app folder to this :
from django.conf.urls.defaults 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()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.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'^first/$', include('first.views.index')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
But now I am getting this error:
SyntaxError at /
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.3
Exception Type: SyntaxError
Exception Value:
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Exception Location: /usr/local/lib/python2.6/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 11:00:41 -0500
I have edited the urls and views but now I am getting this error.
NameError at /first/
global name 'render_to_response' is not defined
Request Method: GET
Request URL: http://localhost:8000/first/?preview
Django Version: 1.3
Exception Type: NameError
Exception Value:
global name 'render_to_response' is not defined
Exception Location: /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 14:50:32 -0500
You do not have a first.urls module with the URLs of your 'first' app. Next to your file first/models.py, create a file first/urls.py which contains the URL patterns for the 'first' app.
For the views you give in your questions, the urls.py should look something like this:
from django.conf.urls.defaults import *
from first.views import index
urlpatterns = patterns('',
url(r'^$', index),
)
Also note that in your views, you use non-standard quote characters, it should look like this:
from django.http import HttpResponse
def index(request):
"""Generate the context for the main summary page"""
return render_to_response("first/first.html")

Regular expression issue when using Generic Views in Django

I am a front-end developer currently dabbling into Django to build a simple portfolio site to display my work. I used Django quite regularly in my previous job but its now been over 6 months and unfortunately I do have old work to look over.
My site consists of:
A homepage with links to featured "projects"
A project list page
A project detail page
I am trying to take advantage of Django's Generic Views to create the latter 2 pages for me. However I am encountering issues when editing my urls.py file. Here it is:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
from django.views.generic import list_detail
from homepage.models import Project
projects_list_info = {
'queryset' : Project.objects.all(),
'allow_empty': True,
'template_name' : '../templates/project_list.html',
}
projects_detail_info = {
'queryset' : Project.objects.all(),
'template_object_name' : 'project',
'slug' : 'slug',
'slug_field' : 'slug',
'template_name' : '../templates/project_detail.html',
}
urlpatterns = patterns('',
# Example:
# (r'^howelltocode/', include('howelltocode.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^test','homepage.views.viewTest'),
(r'^$','homepage.views.viewHome'),
(r'projects/$', list_detail.object_list, projects_list_info),
(r'^projects/(?P<slug>[-\w]+)/$', list_detail.object_detail, projects_detail_info),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
The projects list view page successfully navigates to '/templates/project_list.html' when I type the url 'http://localhost:8000/projects'. However if I type 'http://localhost:8000/projects/htc-wildfire-facebook-application/' to view the "../templates/project_detail.html" page I get a 404 page with a "No project found matching your query" message.
I see this as encouraging as it obviously looking for a project but I think my regular expression could be wrong. Any help would be greatly appreciated.
Thanks
James
You've overwritten the slug coming from the URL with the bare value "slug" in your projects_detail_info dictionary. Remove that entry from the dict.

Django-tagging: Grabbing objects by specifying multiple tags?

I currently have an entry in urls.py which fetches lone permalinks for my bugs:
from django.conf.urls.defaults import *
from tagging.views import tagged_object_list
from bugs.models import Bug
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^workarounds/', include('workarounds.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')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template':'homepage.html'}),
(r'^bugs/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),
(r'^bugs/tagged/(?P<tag>[^/]+)/$',
'tagging.views.tagged_object_list',
{
'queryset_or_model': Bug,
'template_name' : 'tag/lone.html'}),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
So if I specified a url of say, bugs/tagged/firefox it would bring up firefox tags. How could I make it filter out by multiple tags? eg: firefox+css would return all objects tagged with firefox and css.
You'll have to build your own view instead of using tagging.views.tagged_object_list.
(r'^bugs/tagged/(?P<tags>[-\w+]+)/$', your_tag_view)
In your view, get a list of the tags you are searching for:
tags = tags.split('+')
Then, use the TaggedItem.objects.get_by_model query, which conveniently will accept a list of tags:
from tagging.models import TaggedItem
bugs = TaggedItem.objects.get_by_model(Bug, tags)

Page Not Found error

Why I get this error?
Page not found (404)
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/doc/
^admin/
The current URL, , didn't match any of these.
I just uncomment the related lines in urls.py to enable admin and admindoc. They both work; but when I go to the root domain (e.g. example.com), I get this error.
my urls.py is like this:
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^myproject/', include('myproject.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/', include(admin.site.urls)),
)
Thanks in advance...
That's because you don't have a URL defined for the root domain. Check our URLconf: it contains the two lines you listed, which means that you only have URLs defined for example.com/admin and example.com/admin/doc, but not just example.com.