I'm creating a menu using snipets.
The menu items have the option to have a internal page as destination, or an external page (e.g. google.com).
The internal option works great, but the external option opens in a new tag, with the hostname attached to the external link:
http://127.0.0.1:8000/google.com
Not sure if something is wrong with my Orderable
model or what's going on.
Code:
class MenuItem(Orderable):
link_title = models.CharField(
blank=True,
null=True,
max_length=50
)
link_url = models.CharField(
max_length=500,
blank=True,
)
link_page = models.ForeignKey(
"wagtailcore.Page", #app y modelo de tu proyecto
blank=True,
null=True,
related_name="+",
on_delete=models.CASCADE,
)
open_in_new_tab = models.BooleanField(default=False, blank=True,)
page = ParentalKey("Menu", related_name="menu_items")
panels = [
FieldPanel("link_title"),
FieldPanel("link_url"),
PageChooserPanel("link_page"),
FieldPanel("open_in_new_tab"),
]
# @tood add properties
# link
@property
def link(self):
if self.link_page:
return self.link_page.url
elif self.link_url:
return self.link_url
return "#"
@property
def title(self):
if self.link_page and not self.link_title:
return self.link_page.title
elif self.link_title:
return self.link_title
return 'Missing title'
@register_snippet
class Menu(ClusterableModel):
title = models.CharField(max_length=100)
slug = AutoSlugField(populate_from="title", editable=True)
panels = [
MultiFieldPanel([
FieldPanel("title"),
FieldPanel("slug")
], heading="Menu"),
InlinePanel("menu_items", label="Menu item")
]
def __str__(self):
return self.title
html:
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul>
<li>
<a href="/">Home</a>
</li>
{% for item in navigation.menu_items.all %}
<li>
<a href="{{ item.link }}" class="nav-link" {% if item.open_in_new_tab %} target="_blank" {% endif %}>{{ item.title }}</a>
</li>
{% endfor %}
</ul>
<form class="form-inline ml-auto">
<a href="" class="btn btn-outline-secondary">Ingresar</a>
<a href="" class="btn btn-primary ml-2">Registro</a>
</form>
</div>
Found this other question, but it differs from my approache:
Making external links open in a new window in wagtail
Screenshoot: