Ensure correct creation of full URL for canonical urls

This commit is contained in:
Iacopo Spalletti 2015-11-19 19:51:06 +01:00
parent b92d560cdc
commit e921c88ecc
5 changed files with 16 additions and 6 deletions

View file

@ -3,7 +3,7 @@
{% block meta_description %}{{ post.meta_description }}{% endblock meta_description %}
{% block meta_keywords %}{{ post.meta_keywords }}{% endblock meta_keywords %}
{% block canonical_url %}<link rel="canonical" href="{{ SITE.domain }}{{ view.get_view_url }}"/>{% endblock canonical_url %}
{% block canonical_url %}<link rel="canonical" href="{{ meta.url }}"/>{% endblock canonical_url %}
{% block title %}{{ post.get_title }}{% endblock %}
{% block content_blog %}{% spaceless %}

View file

@ -1,7 +1,7 @@
{% extends "djangocms_blog/base.html" %}
{% load i18n thumbnail %}{% spaceless %}
{% block canonical_url %}<link rel="canonical" href="{{ SITE.domain }}{{ view.get_view_url }}"/>{% endblock canonical_url %}
{% block canonical_url %}<link rel="canonical" href="{{ view.get_view_url }}"/>{% endblock canonical_url %}
{% block content_blog %}
<section class="blog-list">
@ -38,4 +38,4 @@
{% endif %}
</section>
{% endblock %}
{% endspaceless %}
{% endspaceless %}

View file

@ -26,12 +26,13 @@ class BaseBlogView(AppConfigMixin, ViewUrlMixin):
'Missing `view_url_name` attribute on {0}'.format(self.__class__.__name__)
)
return reverse(
url = reverse(
self.view_url_name,
args=self.args,
kwargs=self.kwargs,
current_app=self.namespace
)
return self.request.build_absolute_uri(url)
def get_queryset(self):
language = get_language()

View file

@ -4,6 +4,7 @@
<head>
<title>{% block title %}{% page_attribute 'title' %}{% endblock title %}</title>
{% render_block "css" %}
{% block canonical_url %}{% endblock canonical_url %}
{% include "meta_mixin/meta.html" %}
<style type="text/css">
.nav {

View file

@ -58,6 +58,7 @@ class ViewTest(BaseTest):
self.assertEqual(set(view_obj.get_queryset()), set([posts[0], posts[1], posts[2]]))
view_obj.kwargs = {}
view_obj.args = ()
view_obj.object_list = view_obj.get_queryset()
view_obj.paginate_by = 1
context = view_obj.get_context_data(object_list=view_obj.object_list)
@ -74,6 +75,7 @@ class ViewTest(BaseTest):
view_obj = PostListView()
view_obj.namespace, view_obj.config = get_app_instance(request)
view_obj.request = request
view_obj.args = ()
view_obj.kwargs = {}
view_obj.object_list = view_obj.get_queryset()
context = view_obj.get_context_data(object_list=view_obj.object_list)
@ -106,7 +108,10 @@ class ViewTest(BaseTest):
view_obj_1.args = ()
view_obj_1.kwargs = {}
view_obj_1.namespace, view_obj_1.config = get_app_instance(request)
self.assertEqual(view_obj_1.get_view_url(), pages[1].get_absolute_url())
self.assertEqual(
view_obj_1.get_view_url(),
'http://testserver{0}'.format(pages[1].get_absolute_url())
)
request = self.get_request(pages[2], 'en', AnonymousUser())
view_obj_2 = PostListView()
@ -114,7 +119,10 @@ class ViewTest(BaseTest):
view_obj_2.args = ()
view_obj_2.kwargs = {}
view_obj_2.namespace, view_obj_2.config = get_app_instance(request)
self.assertEqual(view_obj_2.get_view_url(), pages[2].get_absolute_url())
self.assertEqual(
view_obj_2.get_view_url(),
'http://testserver{0}'.format(pages[2].get_absolute_url())
)
view_obj_2.view_url_name = None
with self.assertRaises(ImproperlyConfigured):