Django v1.5, TemplateDoesNotExist error - django

In settings.py I've chosen to run with sqlite3 and name this database database.db.
from blog_app.views import oneView, anotherView
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^one/$', oneView),
url(r'^another/$', anotherView),
# Examples:
# url(r'^$', 'django_project.views.home', name='home'),
# url(r'^django_project/', include('django_project.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)),
)
and my views.py looks like this
# -*- coding:UTF-8 -*-
from django.shortcuts import render_to_response
def oneView(request):
return render_to_response("index.html", {"content":"Hello World, from oneView"})
def anotherView(request):
return render_to_response("index.html", {"content":"Hello Again, from anotherView"})
my dir to the index.html is blog/blog_app/templates/index.html. since by standard the settings.py file says this:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
I think it's weird that it says that my index.html file cannot be found. isn't it looking in the correct directory?
nonetheless, I only get a "TemplateDoesNotExist at /one/" error when I try to go to
127.0.0.1:8000/one/ or 127.0.0.1:8000/another/
any ideas?

Try doing something like this in your settings.py
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT PATH = os.path.join(SETTINGS_PATH, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
BLOG_PATH = os.path.join(PROJECT_PATH, "blog_app)
TEMPLATES_PATH = os.path.join(BLOG_PATH, "templates")
TEMPLATE_DIRS = (
TEMPLATES_PATH,
)
The error usually is due to incorrectly configured templates directory

What is your exact error message? (debug informations)
I use this kind of configuration for templates:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates')
)
They are accessible in the templates folder of your django application (aside manage.py), in a sub-folder named following the application used if set (it's not your case for the urls you provided us with).

Related

Django urls does not work with reverse

I have this config
settings.py
ROOT_URLCONF = 'my_project.urls'
urls.py
urlpatterns = patterns('',
(r'^any_url/', Foo, name="something")
)
With this config my project works but if I use reverse('something') it breaks.
Besides, If I change my settings file to ROOT_URLCONF = 'my_project.my_project.urls' reverse works but my project breaks.
Why? what am I doing wrong?
I get:
ImportError: No module named urls
If change my settings I get:
No module named my_project.urls
Use this in urls. py
from django.conf.urls import url

Django - Page not found (404) for URL

I am getting the following error when ever I access the url's except /home.
Page not found (404)
Request Method: GET Request URL
Using the URLconf defined
in report_ui.urls, Django tried these URL patterns, in this order:
^admin/
^home/
The current URL, individual/, didn't match any of
these. You're seeing this error because you have DEBUG = True in your
Django settings file. Change that to False, and Django will display a
standard 404 page.
The code :
urls.py
# -*- coding: utf-8 -*-
from django.conf.urls import patterns, url
urlpatterns = patterns('dnslog.views',
# Examples:
url(r'$', 'home'),
url(r'^report/$', 'report'),
url(r'^angular/$', 'angular'),
url(r'^linear/$', 'LinearPrediction'),
url(r'^splunk/$', 'splunkdata'),
url(r'^toyprediction/$', 'toyprediction'),
url(r'^saleprediction/$', 'GetSalesPrediction'),
url(r'^customerprediction/$', 'GetCustomerPrediction'),
url(r'^monthwiseprediction/$', 'GetMonthWiseSalesPrediction'),
url(r'^getlinearprediction/(?P<duration>.\w+)/$', 'GetLinearPrediction'),
url(r'^getprediction/$', 'GetPredictionReport'),
url(r'^modeldata/(?P<obj>.\w+)/$', 'GetConsolidatedObjectData'),
# url(r'^modeldata/(?P<date>\d{4}-\d{2}-\d{2})/$', 'GetConsolidatedObjectData'),
url(r'^home/(?P<date>\d{4}-\d{2}-\d{2})/$', 'GetConsolidateHits'),
url(r'^bytes/$', 'BytesDownload'),
url(r'^BytesDownload/(?P<date>\d{4}-\d{2}-\d{2})/$',
'GetConsolidateDownloadBytes'),
url(r'^getCntPerData/$', 'GetConsolidateCntPer'),
url(r'^help/$', 'help'),
url(r'^individualhits/$', 'individualhits'),
url(r'^individualhits1/(?P<empid>.+)/(?P<date>\d{4}-\d{2}-\d{2})/$',
'GetIndividualHits'),
url(r'^bubble/$', 'bubble'),
url(r'^bubblechart/(?P<date>\d{4}-\d{2}-\d{2})/$',
'GetConsolidateHitsForBubble'),
#url(r'^test/$', 'test'),
url(r'^domaincloud/$', 'cloud'),
url(r'^getdomaincloud/(?P<date>\d{4}-\d{2}-\d{2})/$', 'Getclouddata'),
url(r'^circleview/$', 'circle'),
url(r'^getcircleview/(?P<date>\d{4}-\d{2}-\d{2})/$', 'Getcircledata'),
url(r'^comparisionview/$', 'comparision'),
url(r'^getcomparision/(?P<date>\d{4}-\d{2}-\d{2})/$', 'Getcomparisiondata'),
url(r'^individualcomparision/(?P<domain>.+)/(?P<empid>.+)/(?P<date>\d{4}-\d{2}-\d{2})/$',
'GetIndividualComparisonData'),
url(r'^trendview/$', 'trend'),
url(r'^gettrendview/$', 'Gettrenddata'),
url(r'^zoomabletree/$', 'zoomabletree'),
url(r'^getzoomabletree/(?P<date>\d{4}-\d{2}-\d{2})/$', 'Getcircledata'),
##url(r'^report_ui/', include('report_ui.foo.urls')),
#url(r'^individualConsolidatedHits/$', 'individualConsolidatedHits'),
url(r'^getConsolidatedCategoryDetails/(?P<date>\d{4}-\d{2}-\d{2})/$',
'getConsolidatedCategoryDetails'),
# overall individual reports
url(r'^individual/$', 'individual'),
## individual Category report
#url(r'^individualcategory/$', 'individualcategory'),
url(r'^GetIndividualCategory/(?P<empid>.+)/(?P<date>\d{4}-\d{2}-\d{2})/$',
'GetIndividualCategory'),
url(r'^twitter/$','twitter'),
root urls.py
from django.conf.urls import patterns, include, url
# to include the media url
from django.conf import settings
from django.conf.urls.static import static
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'report_ui.views.home', name='home'),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^home/', include('dnslog.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
)
Any help would be appreciated. Thanks!
Either change your root urls.py
url(r'^home/', include('dnslog.urls')),
to:
url(r'^/', include('dnslog.urls')),
Or change your request from
from:
individual/
to:
/home/individual

Django-cms root not found on production using i18n

Currently on django-cms 2.4.2. Encountered a peculiar problem, when on local machine where DEBUG=True, visiting 127.0.0.1/ will redirect to 127.0.0.1/en/. When running on a production server (virtualenvs, apache), visiting mysite.com/ will raise a 404 but display the home page behind while mysite.com/en/ displays the home page. Been trying to find the root of the problem but can't seem to figure out if it's in the urls, wsgi or settings.
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin_tools/', include('admin_tools.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(r'^accounts/', include('allauth.urls')),
... apps ...
url(r'^', include('cms.urls')),
)
# Static files
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
else:
from django.conf.urls.defaults import *
handler500 = 'error_pages.views.server_error'
handler404 = 'error_pages.views.custom_404'
handler403 = 'error_pages.views.permission_denied'
handler400 = 'error_pages.views.bad_request'
I have django.middleware.locale.LocaleMiddleware and cms.middleware.language.LanguageCookieMiddleware added in settings.
In my Apache httpd.conf:
NameVirtualHost *:27567
# Begin site configuration
<VirtualHost *:27567>
WSGIScriptAlias / /path/to/wsgi.$
ErrorLog "/path/to/log/error_path.log"
</VirtualHost>
Tried logging any errors that django might encounter and only received:
WARNING 2013-09-18 14:04:34,380 Not Found: /
Edit:
Seems like when Debug=True, the problem goes away. I have set allowed_hosts settings to 'localhost' and 'domain.com'. Have checked sequence of middleware loaded, i18n is set to true etc but still can't pinpoint where the issue lies.
Edit 2:
Just found the problem. It lies in the error pages where the views for error pages are:
from django.shortcuts import render
def server_error(request):
return render(request, '500.html')
Most likely a clash between django-cms' urls.

django url not working except admin

hi guys my django app urls are not working
below is my project urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('recipe.urls', namespace="recipe")),
)
and here is my app urls.py
from django.conf.urls import patterns,url
urlpatterns = patterns('recipe.views',
url(r'^$', 'index', name='index'),
url(r'^create/recipe/$', 'create_recipe', name='create_recipe'),
url(r'^create/ingredients/(?P<recipe_id>\d+)/$', 'create_ingredients',
name="create_ingredients"),
url(r'^create/steps/(?P<recipe_id>\d+)/$', 'create_steps',
name="create_steps"),
url(r'^view/recipe/(?P<recipe_id>\d+)/$', 'view_recipe',
name="view_recipe"),
)
I am not able to get the index page and other urls except admin is working fine. Please help me
From the question comments it seems you are having issues with Django's templates instead of urlconfig. Here is how Django templates work. In your settings.py you define a variable TEMPLATES_DIRS where you specify a tuple of all directories where Django will look for templates.
Assume you have the following TEMPLATES_DIRS:
TEMPLATES_DIRS = (
'/absolute/path/to/foo',
'/absolute/path/to/bar',
)
Then if you look for a template base.html, Django will look for it in the following places and will use the first location if it finds one:
/absolute/path/to/foo/base.html
/absolute/path/to/bar/base.html
In your case you mentioned that you store your templates in both the Django's project folder as well as the app's folder. In that case you have to make sure that both folders are defined in TEMPLATES_DIRS like:
TEMPLATES_DIRS = (
'/absolute/path/to/project/templates',
'/absolute/path/to/app/templates',
)
Then in your case Django will be able to find both base.html and index.html. Now to make things simpler you can define PROJECT_PATH in your settings.py which will store the absolute path to your project path so that you can easily move your project to a different location. I think this is where your issue might be. In Django >= 1.4, you have the following project structure:
/project <= this should be your PROJECT_PATH
/project <= instead of this
templates/
base.html
settings.py
/recipe
templates/
index.html
models.py
With that in mind try using something like this:
PROJECT_PATH = os.path.abspath(os.path.join(__file__, '..', '..'))
PROJECT_NAME = os.path.basename(PROJECT_PATH)
TEMPLATES_DIRS = (
os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates'),
os.path.join(PROJECT_PATH, 'recipe', 'templates')
)
In the above PROJECT_PATH calculates the absolute path of the project. Imagine that your settings.py is located at /some/path/project/project/settings.py. Then you compute the project path as follows:
>>> # settings.py
>>> print __file__
/some/path/project/project/settings.py
>>> print os.path.join(__file__, '..', '..')
/some/path/project/project/settings.py/../../
>>> # now abspath normalizes the path two levels up
>>> print os.path.abspath(os.path.join(__file__, '..', '..'))
/some/path/project
>>> # now you figure out the project name so that you can get the project templates folder
>>> print os.path.basename(os.path.abspath(os.path.join(__file__, '..', '..')))
project
>>> print os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates')
/some/path/project/project/templates
>>> print os.path.join(PROJECT_PATH, 'recipe', 'templates')
/some/path/project/recipe/templates
In your settings.py, find and edit this -
TEMPLATE_DIRS = (
os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates"),
)

Django isn't including the urls.py from an installed app

I am getting the error below:
Caught NoReverseMatch while rendering: Reverse for 'satchless-checkout-prepare-order' with arguments '()' and keyword arguments '{}' not found.
But I have satchless.contrib.checkout.common in my installed apps. Within satchless.contrib.checkout.common is a urls.py which contains:
from django.conf.urls.defaults import patterns, url
from .views import confirmation, prepare_order, reactivate_order
urlpatterns = patterns('',
url(r'^prepare/$', prepare_order, {'typ': 'satchless_cart'},
name='satchless-checkout-prepare-order'),
url(r'^(?P<order_token>\w+)/confirmation/$', confirmation,
name='satchless-checkout-confirmation'),
url(r'^(?P<order_token>\w+)/reactivate/$', reactivate_order,
name='satchless-checkout-reactivate-order'),
)
Why am I not able to call {% url satchless-checkout-prepare-order %} from my template?
Have you included it from your root urlconf?
Something like this in your main urls.py file:
url(r'^foo/' include('satchless.urls')),
You don't have to include satchless.contrib.checkout.common in your INSTALLED_APPS. It's not really app, it's python package which contains common views/decorators for multistep and singlstep checkout apps.
If you look into satchless/contrib/checkout/multistep/urls.py or satchless/contrib/checkout/singlestep/urls.py you can see that both including patterns from common/urls.py:
from ..common.urls import urlpatterns
urlpatterns = urlpatterns + patterns('',
...
What you have to do is to choose checkout method (let say multistep), add it to INSTALLED_APPS and include appropriate urls:
settings.py:
INSTALLED_APPS = (
...
'satchless.contrib.checkout.multistep',
...
)
urls.py:
urlpatterns = patterns(
...
url('^checkout/', 'satchless.contrib.checkout.multistep.urls')
...
)