Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'str' object has no attribute 'relative_url'

I'm getting the error message 'str' object has no attribute 'relative_url' when loading a template tag in wagtail.

Here is my tag code:

from django import template
from home.models import *

register = template.Library()

@register.inclusion_tag('home/subscribe_form.html')
def test_vdcristo():
    return {}

Here is my html template code:

{% load wagtailcore_tags  vdecristo_tags %}
    <form action="{% pageurl page %}" method="POST">
     {% csrf_token %}
    <div class="shop-subscribe bg-color-green margin-bottom-40">
        <div class="container">
                <div class="row">
                    <div class="col-md-8 md-margin-bottom-20">
                            <h2>Subscribase para mantenerse<strong> informado</strong></h2>
                    </div>




                    <div class="col-md-4">
                            <div class="input-group">


                             <input type="text" class="form-control" placeholder="Correo Electronico..." {{ form.subscribase }}>
                                    <span class="input-group-btn">
                                            <button class="btn" type="submit"><i class="fa fa-envelope-o"></i></button>
                                    </span>

                            </div>

                                {{ form.subscribase.errors }}
                        </div>
                </div>
        </div><!--/end container-->
    </div>

 </form>

If I remove the line:

<form action="{% pageurl page %}" method="POST">

The form content loads, but of course it won't post. Can someone help me figure out how to fix this?

like image 894
Leo Avatar asked Oct 21 '25 15:10

Leo


1 Answers

This is failing because your template code refers to the variables page and form, but you're not passing them from your tag code. (The error I'd expect to see here would be 'NoneType' object has no attribute 'relative_url' - your error suggests that you're passing a string as the page variable.)

One possible way to make this work:

@register.inclusion_tag('home/subscribe_form.html')
def test_vdcristo():
    # Look up the relevant form page by slug; replace FormPage and 'subscribe-now'
    # as appropriate for your site
    page = FormPage.objects.get(slug='subscribe-now')
    return {
        'page': page,
        'form': page.get_form(),
    }
like image 87
gasman Avatar answered Oct 23 '25 05:10

gasman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!