NoReverseMatch implementing django-likes - django

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'

Related

NoReverseMatch Exception using mezzanine.mobile

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?

Django translations not working?

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 .

NoReverseMatch error Django with slug

I'm trying to make the index view show a list of "Journal"s, with each linking to a view that lists "Manuscripts" in the Journal. I also want journal_view to be located at a slug for the name of the journal, and likewise for manuscript_view.
This is the traceback I get:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/journals/
Django Version: 2.0.3
Python Version: 3.6.4
Installed Applications:
['social_django',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'journal.apps.JournalConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']
Template error:
In template ... error at line 8
Reverse for 'view_journal' not found. 'view_journal' is not a valid view function or pattern name.
1 : {% load static %}
2 :
3 : <link rel="stylesheet" type="text/css" href="{% static 'journal/style.css' %}" />
4 :
5 : {% if journal_list %}
6 : <ul>
7 : {% for journal in journal_list %}
8 : <li>{{ journal.title }}</li>
9 : {% endfor %}
10 : </ul>
11 : {% else %}
12 : <p>No journals are available.</p>
13 : {% endif %}
# ...(Traceback)...
Exception Type: NoReverseMatch at /journals/
Exception Value: Reverse for 'view_journal' not found. 'view_journal' is not a valid view function or pattern name.
The Journal model looks like this:
class Journal(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(unique=True)
# blah blah
def __str__(self):
return self.title
def get_absolute_url(self, *args, **kwargs):
return reverse("view_journal", kwargs={"slug": self.slug})
My views look like this:
class IndexView(generic.ListView):
template_name = 'journal/index.html'
context_object_name = 'journal_list'
def get_queryset(self):
return Journal.objects.all()
class JournalView(generic.ListView):
template_name = 'journal/journal.html'
context_object_name = 'latest_article_list'
def get_queryset(self):
return Manuscript.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
My app urls look like this:
app_name = 'journal'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<slug>/', views.JournalView.as_view(), name='view_journal'),
path('<slug>/', views.ArticleView.as_view(), name='view_manuscript'), ]
Project urls:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('journals/', include('journal.urls')),
path('social/', include('social.apps.django_app.urls', namespace='social')),
]
I had it working this way:
I defined a slug field in my model first, as you did too:
slug = models.SlugField(max_length=20, unique=True)
then in my views.py I did add this:
slug_url_kwarg = 'slug'
and finally wrote my path this way:
path('<slug:slug>/', MyView.as_view(), name='myview')

Django CMS i18n url problems

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

Problem with messages framework in Django 1.2

I'm running Django 1.2 beta and trying out the new feature: message framework.
http://docs.djangoproject.com/en/dev/ref/contrib/messages/
Everything seems to work, but when I try to output the messages, I get nothing. Seems that messages variable is empty. I double checked all the settings, they seem to be just like in the manual. What could be wrong?
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', #send messages to users
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
#debug tool
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.messages.context_processors.messages', #send messages to users
'django.core.context_processors.auth',
)
#Store messages in sessions
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage';
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.admin',
'django.contrib.messages',
'debug_toolbar',
#my apps
#...
)
views.py
def myview(request):
from django.contrib import messages
messages.error(request, 'error test');
messages.success(request, 'success test');
return render_to_response('mytemplate.html', locals());
mytemplate.html
{% for message in messages %}
{{ message }}<br />
{% endfor %}
In template nothing is outputted.
You'll need to use a RequestContext in your call to render_to_response.
return render_to_response('mytemplate.html', locals(),
context_instance=RequestContext(request))
See the note a few screens down under this documentation section.