如何在 mako 中编写 url python

How to write urls in mako python

你好,谢谢你的帮助,

我有一些 file.mako.md 文件,我正在如下所示更改此文件,但它显示 404 错误,这是因为它使用了错误的 url 地址,即它需要 url喜欢 https://whosebug.com/questions/ask" 而不是 https://whosebug.com/questions/ask 并且由于这会导致服务器错误,任何帮助将不胜感激

<%!
  import requests
   
  def build_path_with_dataset_name(ds_name):
    github_path = "https://github.com/Eshan-Agarwal/datasets/tree/patch-58/docs/catalog/images/" + ds_name + ".jpg"
    return github_path
  
  def example_exists(path):
    r = requests.head(path)
    return r.status_code == 302 
%>

它读取 url https://github.com/Eshan-Agarwal/datasets/tree/patch-58/docs/catalog/images/" 给出 404 错误 see this image

编辑:我认为这是由于下划线出现在任何人都知道我们如何删除它

你试过用 f 弦吗?

import requests

def build_path_with_dataset_name(ds_name):
    github_path = f"https://github.com/Eshan-Agarwal/datasets/tree/patch-58/docs/catalog/images/{ds_name}.jpg"
    return github_path

def example_exists(path):
    r = requests.head(path)
    return r.is_redirect

dataset_name = 'mnist'
path = build_path_with_dataset_name(dataset_name)
print(example_exists(path))


这也解决了我的问题,感谢@pierresegonne 给我时间,

问题是 .md 文件将“http://github.com/Eshan-Agarwal/”作为自动 url,因此它还包含最后一个引号 " 这是解决方案。

import requests

def dataset_examples_paths(ds_name):
 github_path = "http://" + "github.com/Eshan-Agarwal/datasets/tree/patch-60/docs/catalog/images/" + ds_name + ".jpg"
 return github_path

def example_exists(path):
 r = requests.get(path)
 return r.status_code==200

dataset_name = 'mnist'
path = build_path_with_dataset_name(dataset_name)
print(example_exists(path))