title: How do I generate absolute urls in Django template?
body: <p>I’m having trouble generating the absolute url for a Django model. In &quot;courses.html&quot; template I iterate through two models, <code>category</code> and <code>course</code>, but am only able to generate the relative url <code>&lt;slug&gt;</code> rather than the full url <code>/courses/&lt;slug&gt;</code> from each object.</p>
  <p>Is there any way to generate an absolute url for each object?</p>
  <p><strong>main views.py</strong></p>
  <pre><code>def courses(request):
      return render(request=request,
                    template_name=&quot;main/courses.html&quot;,
                    context={'courses': Course.objects.all,
                             'categories': Category.objects.all},
                    )
  </code></pre>
  <p><strong>courses.html</strong></p>
  <pre><code>
  {% extends &quot;main/header.html&quot; %}
  {% block content %}
  
  {% for category in categories %}
      &lt;a href=&quot;{{ category.slug }}&quot;/&gt;
        &lt;p&gt;{{ category }}&lt;/p&gt;
      &lt;/a&gt;
  {% endfor %}
  
  {% for course in courses %}
      &lt;a href=&quot;{{ course.slug }}&quot;/&gt;
        &lt;p&gt;{{ course }}&lt;/p&gt;
      &lt;/a&gt;
  {% endfor %}
  </code></pre>