Search code examples
pythonhtmldjangofolium

Writing url in View in Django


I want to define a link for a character in the view. I used the following code, but it has an error. Error: Encountered unknown tag 'url'

code:

            html="""
                 <p>
                  <a href="{% url index2 %}">more information</a>
                 </p>
                """

I use the Folium library and want to put a link in the popup. So I can not use the normal mode and I have used the above method.


Solution

  • You have template tags in your HTML, so you have to pass it through the Django template processor:

    from django.template import Template, Context
    
    html = Template(
        """
        <p>
        <a href="{% url index2 %}">more information</a>
        </p>
        """).render(Context({}))
    

    Note that extra spaces included in your quoted string will appear in the html output.

    Also note that I am passing an empty dictionary to construct the Context, but if you were to put some entries into that dictionary, they would be available as template variables in the template.