Managing django urls
<p>I am making a personal site. I have a blog page(site.com/blog), where I have a list of my blog posts. If I want to check a blog post simple enough I can click it and the code:</p>

<pre><code>        &lt;a href="/blog/{{ obj.id }}"&gt;&lt;h1&gt;{{ obj.topic_title }}&lt;/h1&gt;&lt;/a&gt;
</code></pre>

<p>will get me there. Also if I want to go to my contacts page (site.com/contacts). Easy enough I click nav contacs button and I go there</p>

<pre><code>                    &lt;a href="/contacts"&gt;Contacts&lt;/a&gt;
</code></pre>

<p>but if I enter a blog post (<em>site.com/blog/1</em>), I am using the same template and if I want to go to my contacts page I have to yet again click the</p>

<pre><code>                    &lt;a href="/contacts"&gt;Contacts&lt;/a&gt;
</code></pre>

<p>link, but that will port me to a 404 page <em>site.com/blog/contacts</em> . How do I deal with this problem without harcoding every single page</p>

<p>Use the built-in Django <a href="https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url" rel="nofollow"><code>url</code></a> template tag, which takes the view name and returns an absolute path to it.</p>

<blockquote>
  <p>Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters.</p>
</blockquote>

<p>You can give it a view name and any of its view parameters as well. This is much better than how you link to the blog page:</p>

<pre><code>{% url 'blog-view-name' obj.id %}
</code></pre>

<p>This ensures that if you ever change the structure of your views, it will still not break your links.</p>

python html django url