Django-cms root not found on production using i18n - django

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.

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")),
)

Django Default main page functionality

i have installed DJANGO 1.5, once entered 127.0.01:8000 the beautiful HTLM page appears.
"it worked"
now whatever i enter in the Browser URL, it always goto default welcome page.
once i start playing with url.py this functionality get vanished. and i start getting 404 page.
is there any way to keep this functionality on i.e what ever is typed in the browser url it goes to main page exception for the defined url in url.py
please help
url.py
from django.conf.urls import patterns, include, url
from article.views import HelloTemplate
urlpatterns = patterns('',
url(r'^hello_template/$', 'article.views.hello_template'),
url(r'^hello_template_simple/$', 'article.views.hello_template_simple'),
other code snippet for "myproject/urls.py"
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^myapp/', include('myproject.myapp.urls')),
(r'^$', RedirectView.as_view(url='/myapp/list/')),
(r'', 'myproject.myapp.views'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Just put a your default page as a catch-all URL at the end of the other URLs:
urlpatterns = patterns('',
url(r'^hello_template_simple/$', 'article.views.hello_template_simple'),
url(r'', 'article.views.hello_template'),
Now any URL which isn't matched by hello_template_simple will be caught by hello_template.

Django-CMS WYMeditor not correctly working on Nginx

I am a new user to Django and Django Cms.
on local machine
all js, html,css files download well when i work with backend
But on production when i use text plugin I can't get some files
There are:
GET http://example.com/admin/cms/page/15/edit-plugin/56/lang/uk.js/ 500 (Internal Server Error)
GET http://example.com/admin/cms/page/15/edit-plugin/56/iframe/default/wymiframe.html/ 500 (Internal Server Error) (Status Code:301 MOVED PERMANENTLY)
Where example.com - my own site:)
File uk.js - i added by monkey patching. (everything work fine on local machine)
In my html code in local I have:
<iframe src="/static/cms/wymeditor/iframe/default/wymiframe.html" onload="this.contentWindow.parent.WYMeditor.INSTANCES[0].initIframe(this)"></iframe>
On production side:
<iframe src="iframe/default/wymiframe.html" onload="this.contentWindow.parent.WYMeditor.INSTANCES[0].initIframe(this)"></iframe>
My urls.py:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('core.urls')),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
my core urls.py:
from django.conf.urls import patterns, url, include
from django.http import HttpResponse
urlpatterns = patterns('core.views',
url(r"^robots.txt$", lambda r: HttpResponse("User-agent: *\nDisallow: /", mimetype="text/plain")),
url(r"^commissions/$","commissions", name = "commissions" ),
url(r"^commissions/(?P<slug>[a-zA-Z0-9_\-]+)/$","commissions", name = "comma" ),
url(r'^news/', include('cmsplugin_news.urls'),{}),
url(r'^', include('cms.urls')),
)
WHen i do python manage.py collectstatic - everything fine and i have all static files on my static folder
Please help me what i doing wrong ?

Redirect after login not working

I've been searching around but can't figure out why the redirection after login is not working as expected.
I successfully login at http://localhost:8000/accounts/login/. After that I'm always redirected to http://localhost:8000 no matter if I use settings.py variable LOGIN_REDIRECT_URL or if I leave it completely out.
# urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^grappelli/', include('grappelli.urls')),
url(r'^$', 'myapp.views.home', name='home'),
url(r'^home/$', 'myapp.views.apphome', name='apphome'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^admin/', include("massadmin.urls")),
)
I don't know where I should start looking for a problem. Any ideas? Is there some settings I'm missing here? Any other files I should include to help debug the problem?
Check your form action! (glad I could help :) )

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)),
)