{"id":2211,"date":"2024-06-04T09:53:16","date_gmt":"2024-06-04T07:53:16","guid":{"rendered":"https:\/\/alessandromasciadri.com\/?p=2211"},"modified":"2024-06-04T10:08:29","modified_gmt":"2024-06-04T08:08:29","slug":"come-ottenere-lurl-corrente-in-jinja2","status":"publish","type":"post","link":"https:\/\/alessandromasciadri.com\/come-ottenere-lurl-corrente-in-jinja2\/","title":{"rendered":"Come ottenere l&#8217;URL corrente in Jinja2"},"content":{"rendered":"\t\t<div data-akihiro-type=\"ama-post\" data-akihiro-id=\"2211\" class=\"akihiro akihiro-2211\" data-akihiro-post-type=\"post\">\n\t\t\t\t<div class=\"akihiro-element akihiro-element-a51968d e-flex e-con-boxed e-con e-parent\" data-id=\"a51968d\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"akihiro-element akihiro-element-288b6ec akihiro-widget akihiro-widget-text-editor\" data-id=\"288b6ec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Stiamo sviluppando una applicazione con Flask che usa Jinja2 come template engine. All&#8217;interno del template html che stiamo sviluppando abbiamo bisogno di ottenere l&#8217;URL corrente e la domanda che ci poniamo \u00e8 se esiste un modo nativo in Jinja2 che ci permetta di ottenerlo facilmente. La risposta \u00e8 si e ora vediamo qui di seguito come fare.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-323123d akihiro-widget akihiro-widget-heading\" data-id=\"323123d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h3 class=\"akihiro-heading-title akihiro-size-default\">Ottenere URL corrente in Jinja2<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-0cf6985 akihiro-widget akihiro-widget-text-editor\" data-id=\"0cf6985\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\tPartiamo da <code>http:\/\/www.my-domain.com<\/code> che \u00e8 l&#8217;esempio di URL pi\u00f9 semplice. Per stamparlo all&#8217;interno del nostro template, usiamo il seguente codice\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-2cc54ee akihiro-widget akihiro-widget-code-highlight\" data-id=\"2cc54ee\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python \">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>{{ url_for(request.endpoint) }}<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-1cca195 akihiro-widget akihiro-widget-text-editor\" data-id=\"1cca195\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Quindi, se per esempio volessimo creare un collegamento basandoci sul valore del nostro URL, potremmo fare qualcosa di simile:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-fffa067 akihiro-widget akihiro-widget-code-highlight\" data-id=\"fffa067\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-html \">\n\t\t\t\t<code readonly=\"true\" class=\"language-html\">\n\t\t\t\t\t<xmp><a href=\"{{ url_for(request.endpoint) }}\">Home<\/a>\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-88910d0 akihiro-widget akihiro-widget-heading\" data-id=\"88910d0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h3 class=\"akihiro-heading-title akihiro-size-default\">Ottenere URL corrente (incluso i parametri) in Jinja2<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-c1c78d0 akihiro-widget akihiro-widget-text-editor\" data-id=\"c1c78d0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>In questo caso il nostro URL \u00e8 composto cos\u00ec <code>http:\/\/www.my-domain.com\/example\/1\/2<\/code><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-ef8a254 akihiro-widget akihiro-widget-text-editor\" data-id=\"ef8a254\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Essendoci anche dei parametri, se vogliamo ottenere l&#8217;URL completo, allora nel nostro template Jinja2 utilizzeremo il seguente codice:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-26276ce akihiro-widget akihiro-widget-code-highlight\" data-id=\"26276ce\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python \">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>{{ url_for(request.endpoint, **request.view_args) }}<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-03f2bd1 akihiro-widget akihiro-widget-text-editor\" data-id=\"03f2bd1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Pertanto, riprendendo l&#8217;esempio del collegamento che abbiamo fatto sopra, il nostro HTML diventerebbe simile al seguente:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-ce9e6ee akihiro-widget akihiro-widget-code-highlight\" data-id=\"ce9e6ee\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-html \">\n\t\t\t\t<code readonly=\"true\" class=\"language-html\">\n\t\t\t\t\t<xmp><a href=\"{{ url_for(request.endpoint, **request.view_args) }}\">Home<\/a>\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Stiamo sviluppando una applicazione con Flask che usa Jinja2 come template engine. All&#8217;interno del template html che stiamo sviluppando abbiamo bisogno di ottenere l&#8217;URL corrente e la domanda che ci poniamo \u00e8 se esiste un modo nativo in Jinja2 che ci permetta di ottenerlo facilmente. La risposta \u00e8 si e ora vediamo qui di seguito [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[28,55,12],"class_list":["post-2211","post","type-post","status-publish","format-standard","hentry","category-programmazione","tag-flask","tag-jinja","tag-python"],"_links":{"self":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/2211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/comments?post=2211"}],"version-history":[{"count":4,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2215,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/2211\/revisions\/2215"}],"wp:attachment":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/media?parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}