I'm pretty new to Mezzanine and I'm having some difficulty getting the option View Mobile Site available in my index.html.
SETUP
In the settings.py, I specified the following:
INSTALLED_APPS = (
"newsletters",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.messages",
"django.contrib.contenttypes",
"django.contrib.redirects",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.sitemaps",
"django.contrib.staticfiles",
"mezzanine.boot",
"mezzanine.conf",
"mezzanine.core",
"mezzanine.generic",
"mezzanine.pages",
"mezzanine.blog",
"mezzanine.forms",
"mezzanine.accounts",
"mezzanine.mobile",
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"mezzanine.core.middleware.UpdateCacheMiddleware",
"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",
)
In my index.html:
{% ifinstalled mezzanine.mobile %}
<span class="separator">|</span>
{% trans "View Mobile Site" %}
{% endifinstalled %}
But I get this error when I access my index.html:
Reverse for 'set_device' with arguments '('mobile',)'
and keyword arguments '{}' not found.
0 pattern(s) tried: []
Any ideas why I'm getting this exception?
The exception means that django couldn't find a match in it's urls configuration.
Did you add some in the root urls.py? Post the file.
Maybe you are missing to include in it something like this:
urls.py
urlpatterns = [
...
url(r'the_app_name/', include('the_app_name.urls')),
...
]
So, you should have an url somewhere named set_device that accepts 'mobile' as parameter, something like this
app_name = 'the_app_name'
urlpatterns = [
url(r'^my_url/(?P<type>(mobile|...))/$', my_view, name='set_device'),
...
Should we asume it's built in the CMS or is it your own?
Related
In my Django 2.0 site, I want to set the lang atribute of the html tag to the current locale's language. In my base.html which other templates extend, I use get_current_language in the following way
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
...
</html>
The site has translations for multiple languages. If I switch the language in the browser, I see the correct translations, but the lang attribute will always contain en.
In my settings.py I have
USE_I18N = True
LANGUAGE_CODE = 'en-us'
and based on the suggestion of Goran the following middleware order
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
The LANGUAGES setting is unset.
As suggested by Kostadin Slavov I have tried printing the language from the view. It seems that get_current_language calls django.utils.translation.get_language, so I have inserted the following in my view
from django.utils import translation
print(translation.get_language())
It prints the correct value (eg de when accessing the view with a browser set to German).
What else am I missing?
I tried to simulate your environment with these steps:
$ cd ~
$ python3 -m venv ~/venvs/mysite
$ source ~/venvs/mysite/bin/activate
$ pip install django==2.0.8
$ django-admin startproject mysite
Then I updated the generate code as in your example:
mysite/settings.py
...
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
mysite/urls.py
from django.contrib import admin
from django.urls import path
from django.views.generic.base import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='base.html'), name='home'),
path('admin/', admin.site.urls),
]
templates/base.html
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<body>
<pre>LANGUAGE_CODE = {{ LANGUAGE_CODE }}</pre>
<body>
</html>
With the Django generated code and my few above updates I can see different language code if I switch the language of my browser visiting http://localhost:8000/ after starting it with:
$ python manage.py runserver
Try my steps on your local environment and check if it works, and then compare your project to the code above.
Update
Try to use diffsettings to see "differences between the current settings file and Django’s default settings".
Had the same problem. In my case, the function was called async and always returned the default language.
SOLUTION: pass language from 'main' context.
Code like in this example:
def get_context_data( self, **kwargs ):
context = super().get_context_data(**kwargs)
lng_code = get_language() # -> 'de'
#sync_to_async
def get_data():
context['data1'] = Model1.objects.filter(language==get_language()) # get_language() -> 'en'
#sync_to_async
def get_data2():
...
#sync_to_async
def get_data3():
...
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
asyncio.gather(
get_data1(),
get_data2(),
get_data3()
))
loop.close()
I want to translate my website to other language , i've configured my settings.py as it should be but no changes are happens.
I've created messages and i can see them inside my local folder, also i've compiled them successfully without any erors.
Here is my settings :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My apps
'main',
# Third part apps
'captcha',
'jsonify'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOCAL_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en'
LANGUAGES = (
('ru', _(u'RU')),
('en', _(u'EN'))
)
My views:
from django.utils.translation import gettext as _
def main(request):
context = {
'title':_('Главная'),
}
return render(request, 'index.html', context)
Also i've loaded the i18n above inside my template where the translations happens index.html:
{% load static %}
{% load i18n %}
<title>{% block head_title %}{{title}}{% endblock %}</title>
I got it to work, instead of typing LOCALE_PATHS i typed LOCAL_PATHS, now everything is working just fine .
Getting the following error when trying to install django-likes
NoReverseMatch at /post/25/
Reverse for '' with arguments '('posts-post', 25, 1)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: /Users/Pete/.virtualenvs/innerlocal-mvp/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 463
I'm not sure where to even start on this error, I've done a lot of searching, but nothing comes up.
My view that this is referring to:
def single_post(request, id):
post = get_object_or_404(Post, id=id)
...
return render(request, 'posts/single_post.html', locals())
with {% likes post %} in the html.
and this line highlighted from the resulting html:
<a class="liker" href="{% url like content_type content_obj.id 1 %}" rel="nofollow">{% trans "I Like" %}</a>
I'm using Django 1.7 so wouldn't be surprised if that was a problem.
Any help would be greatly appreciated!!
Extra settings as requested:
the urls.py lines (first, the app, second, the django-likes url:
url(r'^', include('posts.urls')),
(r'^likes/', include('likes.urls')),
and the urls.py for the posts app:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from views import PostDelete
urlpatterns = patterns('posts.views',
# (r'^', 'home'),
url(r'^$', 'all_posts', name='home'),
url(r'^post/(?P<id>\w+)/$', 'single_post', name='single_post'),
url(r'^new-post/$', 'new_post', name='new_post'),
url(r'^search/$', 'search', name='search'),
url(r'^delete/(?P<pk>\d+)/$', PostDelete.as_view(),
name='entry_delete'),
)
Installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.sites',
....
'secretballot',
'likes',
)
Middleware classes:
MIDDLEWARE_CLASSES = (
'django.middleware.gzip.GZipMiddleware',
'pipeline.middleware.MinifyHTMLMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
'secretballot.middleware.SecretBallotIpMiddleware',
'secretballot.middleware.SecretBallotIpUseragentMiddleware',
'likes.middleware.SecretBallotUserIpUseragentMiddleware',
)
Template context processors:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.static",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
)
Turns out that the app code needed a change:
In likes.html
from
<a class="liker" href="{% url like content_type content_obj.id 1 %}" rel="nofollow">{% trans "I Like" %}</a>
to
<a class="liker" href="{% url 'like' content_type content_obj.id 1 %}" rel="nofollow">{% trans "I Like" %}</a>
ie - the quotation marks around 'like'
I am using django 1.6.4 and django toolbar version django-debug-toolbar==1.2.
I do get the toolbar on the side but when I click on any of the menu items, it shows me the html elements from my home page within the toolbar overlay. I have done collectstatic after installing django toolbar. HTML generated is not valid but issue seems to be from the css and scripts tag from django toolbar.
Any help will be much appreciated.
Thanks
Updated:
urls.py entry
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
)
settings.py
DEBUG = True
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
THIRD_PARTY_APPS = (
'suit',
'debug_toolbar',
'south',
'crispy_forms',
'haystack',
'taggit',
)
I'm having 404 problems using Django CMS 3.0.0.beta3, Django 1.6.1 and i18n.
When I go to my site, everything is displayed, but when I try to click in the upper bar into Administration or whatever I get a 404 saying:
http://my_server/es-es/admin/
But instead, this URL is working!!
http://my_server/es/admin/
I don't understand why this es-es string puts itself in the middle. I have tried to change the LANGUAGE_CODE to es under settings.py but still nothing.
urls.py
from django.conf.urls import include, patterns, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
Part of settings.py
SITE_ID = 1
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms', # django CMS itself
'mptt', # utilities for implementing a modified pre-order traversal tree
'menus', # helper for model independent hierarchical website navigation
'south', # intelligent schema and data migrations
'sekizai', # for javascript and css management
'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list before 'django.contrib.admin'.
'django.contrib.messages', # to enable messages framework (see :ref:`Enable messages <enable-messages>`)
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
)
LANGUAGE_CODE = 'es-es'
TIME_ZONE = 'Europe/Madrid'
USE_I18N = True
USE_L10N = True
USE_TZ = True
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'cms.context_processors.media',
'sekizai.context_processors.sekizai',
)
TEMPLATE_DIRS = (
# The docs say it should be absolute path: PROJECT_PATH is precisely one.
# Life is wonderful!
os.path.join(PROJECT_PATH, "templates"),
)
CMS_TEMPLATES = (
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
LANGUAGES = [
('en', 'English'),
('es', 'Spanish'),
]
LANGUAGE_CODE should be 'es' and be sure to kill all cookies