How to point blog to menu item in Mezzanine? - python

How do you point blogs to a menu item in Mezzanine? I am able to point my blogs to Home using urls.py but how about to page types like link and richtextpage?

Add a rich text page, call it Blog or however you want, then in meta data group, in the field URL add /blog/ or whichever is the url to the main blog app. Mezzanine will match the url with the page and will add the Page object to the rendering context, so you can use it in templates.

Related

Can Django's URL tag show the URL of the RedirectView

Let's say that in my urls.py I have a url like this:
path("support/", RedirectView.as_view(url="http://www.example.com"), name="support"),
And in one of my templates I use the url tag:
{% url "support" %}
This of course outputs /support/ as expected. But what if I want it to output http://www.example.com instead? Is that at all possible? Skip the redirect basically.
So Link would output Link.
But what if I want it to render http://www.example.com instead? Is that at all possible? Skip the redirect basically.
No, in short it is not possible with django views, since the url is of another website and you can't render it in your own.
You can see what exactly render() does.
If you'd like to directly redirect, then simply use anchor tag as:
Visit website.
You can also do this dynamically, by creating a URLField in one of the models and simply iterating it with href attribute of anchor tag.

Use different wagtail page as root without slug

I'm attempting to set a page as the root home page of my wagtail based site.
I know you can set the homepage using the sites setting. But, my use case is different.
I'm using a "redirect" page which redirects to an index page as the homepage. The index is restricted to only allow ertain page types (since it's an index....). But, this causes issues with other pages not being in the tree if it was set at the root.
Hence, the redirect. But, upon redirecting the serve view of the redirect page to use this page it picks up the slug.
I still want it to appear at the root url. Meaning, /. How can I achieve this?
High level description:
from django.shortcuts import redirect
class IndexPage(Page):
# children restricted to IndexSubpage
class IndexSubpage(Page):
# parent restricted to IndexPage
class RedirectPage(Page):
def serve(request):
# Link is a link to a page through a ForeignKey....
return redirect(self.link, permanent=True)
So, this will pick up the slug of IndexPage instead of being at /

Globally getting context in Wagtail site

I am working on a Wagtail project consisting of a few semi-static pages (homepage, about, etc.) and a blog. In the homepage, I wanted to list the latest blog entries, which I could do adding the following code to the HomePage model:
def blog_posts(self):
# Get list of live blog pages that are descendants of this page
posts = BlogPost.objects.live().order_by('-date_published')[:4]
return posts
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['posts'] = self.blog_posts()
return context
However, I would also like to add the last 3 entries in the footer, which is a common element of all the pages in the site. I'm not sure of what is the best way to do this — surely I could add similar code to all the models, but maybe there's a way to extend the Page class as a whole or somehow add "global" context? What is the best approach to do this?
This sounds like a good case for a custom template tag.
A good place for this would be in blog/templatetags/blog_tags.py:
import datetime
from django import template
from blog.models import BlogPost
register = template.Library()
#register.inclusion_tag('blog/includes/blog_posts.html', takes_context=True)
def latest_blog_posts(context):
""" Get list of live blog pages that are descendants of this page """
page = context['page']
posts = BlogPost.objects.descendant_of(page).live().public().order_by('-date_published')[:4]
return {'posts': posts}
You will need to add a partial template for this, at blog/templates/blog/includes/blog_posts.html. And then in each page template that must include this, include at the top:
{% load blog_tags %}
and in the desired location:
{% latest_blog_posts %}
I note that your code comment indicates you want descendants of the given page, but your code doesn't do that. I have included this in my example. Also, I have used an inclusion tag, so that you do not have to repeat the HTML for the blog listing on each page template that uses this custom template tag.

Managing django urls

I am making a personal site. I have a blog page(site.com/blog), where I have a list of my blog posts. If I want to check a blog post simple enough I can click it and the code:
<h1>{{ obj.topic_title }}</h1>
will get me there. Also if I want to go to my contacts page (site.com/contacts). Easy enough I click nav contacs button and I go there
Contacts
but if I enter a blog post (site.com/blog/1), I am using the same template and if I want to go to my contacts page I have to yet again click the
Contacts
link, but that will port me to a 404 page site.com/blog/contacts . How do I deal with this problem without harcoding every single page
Use the built-in Django url template tag, which takes the view name and returns an absolute path to it.
Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters.
You can give it a view name and any of its view parameters as well. This is much better than how you link to the blog page:
{% url 'blog-view-name' obj.id %}
This ensures that if you ever change the structure of your views, it will still not break your links.

Viewing a django-cms page via the slug

How do you view a published django-cms page using a path that incorporates the slug?
I installed django-cms without error, and I can view the default cms homepage just fine. I created and published a simple "About" page with the slug "about", but when I visit http://localhost:8000/about/ I get a 404 error. I can see the page if I use the "View on site" button, but that takes me to http://localhost:8000/?preview=1&language=en, not the real published path.
What am I doing wrong?
you won't get access until you check the published in cms page list view in admin.
View on site help with a preview before page is published.
After digging through the code, I discovered that django-cms doesn't actually expose pages via their slug unless they're created UNDER a home page. The code that looks up a page via their slug looks in the cms_title table, and it stores '' for the slug for any page that's not a child. Very unintuitive, but after I re-created the page under a "Home" page, I could then access it via the /about/ page.

Categories

Resources