Reputation: 1345
I'm trying to add CSS to the HTML that my python code generates. This line causes an error when I post the HTML:
html += '<link rel=\'stylesheet\' href=\'{{ url_for(\'static\', filename=\'css/main.css\') }}\'>'
I also tried putting the quotes on the outside, I get the same error:
html += "<link rel=\'stylesheet\' href=\'{{ url_for(\'static\', filename=\'css/main.css\') }}\'>"
The error I'm getting is:
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:
if I remove that one line I can PUT to the web page.
This is the code that writes the HTML to the page:
def write_data_to_confluence(auth, html, pageid, title = None):
info = get_page_info(auth, pageid)
ver = int(info['version']['number']) + 1
ancestors = get_page_ancestors(auth, pageid)
anc = ancestors[-1]
del anc['_links']
del anc['_expandable']
del anc['extensions']
if title is not None:
info['title'] = title
data = {
'id' : str(pageid),
'type' : 'page',
'title' : info['title'],
'version' : {'number' : ver},
'ancestors' : [anc],
'body' : {
'storage' :
{
'representation' : 'storage',
'value' : str(html)
}
}
}
data = json.dumps(data)
url = '{base}/{pageid}'.format(base = BASE_URL, pageid = pageid)
r = requests.put(
url,
data = data,
auth = auth,
headers = { 'Content-Type' : 'application/json' }
)
r.raise_for_status()
print("Wrote '%s' version %d" % (info['title'], ver))
print("URL: %s%d" % (VIEW_URL, pageid))
I think I am quoting it wrong. I tried a couple different ways, but have yet to get it right. How can I quote this correctly?
Upvotes: 0
Views: 95
Reputation: 1065
Try this approach,
html += "<link rel=\"stylesheet\" href=\"{{ url_for('static', filename='css/main.css') }}\">"
Escaping single quotes is not required as the string is enclosed in double quotes. The attribute values are enclosed in double quotes.
or, using triple quotes
html += """<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">"""
Upvotes: 1
Reputation: 2816
If you want to use string interpolation ({{ url_for...
) then you need to use f-strings: f'interpolation: {{url_for(whatever)}}'
Also you can use double-quoted strings and not bother escaping every single quote: print("Single 'quotes' are fine here")
In more complex cases you can use multiline stings, they can contain newlines and other quote markers inside them, ''' this 'is' "ok" '''
. There are '''string'''
and """string"""
versions, functionally the same.
Upvotes: 0