[Answer]-Trouble using mako templates

1👍

You are mixing ${} with regular python in the if conditional and both a tags. Also, you cannot nest ${} inside ${}. You should probably refactor this code to be either out of the template or into a <% %> block, but something like this should work:

%if "courseware" in request.url:
<a href="${request.url[:request.url.find('courseware')+len('courseware')+1]+datum['url']}">
%else:
    <a href="${request.host + '/courses/' + datum['org'] + '/' + datum['course_ids'] + '/#/courseware/' + datum['url']}">
%endif

Here is a refactored version:

<%def name="courseware_link(datum)">
    <%    
    if "courseware" in request.url:
        url = request.url[:request.url.find("courseware")+len("courseware")+1]
        url += datum["url"]
    else:
        url = request.host + "/courses/" + datum["org"] + "/"
        url += datum["course_ids"] + "/#/courseware/" + datum["url"]
    %>
    <a href="${url}">
</%def>

Furthermore you might want to use a routing package to generate your urls instead of building them manually like this, Django should provide something to automatically construct urls.

Leave a comment