Reputation: 3076
So I am struggling a bit, with something that logically seems so simple but due to my limited understanding of Django I am not sure where to look and how to formulate a solution.
Basically I have a Blog app set up and it shows the complete(all the content including disqus discussion) latest post on the home page. The post has a further link to the posts own page as well. I have set up Disqus and need to get some key information to use for the disqus_url
and disqus_identifier
. I have set up the model as follows with a method for get_absolute_url
as follows:
def get_absolute_url(self):
return reverse('blog.views.renderBlog',args=[str(self.id),str(self.slug)])
My view is set up as follows:
def renderBlog(request,postid=1,slug=None):
template = 'blog_home.html'
if(postid == 1 and slug == None):
post = Post.objects.latest('date_created')
else:
post = Post.objects.get(slug=slug, id=postid)
data = {
'post':post,
}
return render(request, template, data)
As you can see the view is set up to handle both URL's as follows:
url(r'^$', 'renderBlog', name='blogHome'),
url(r'^post/(?P<postid>\d{1,4})/(?P<slug>[\w-]+)/$', 'renderBlog', name='blogPostPage'),
In my template I am setting disqus_identifier = '{{ post.get_absolute_url }}'
and I am hardcoding the domain portion in the meantime as disqus_url = 'http://127.0.0.1{{ post.get_absolute_url }}';.
. Same goes for the comment count <a href="" data-disqus-identifier
.
I dont like doing things in a hackish manner, what would be the best method for me to get the full absolute url. I have looked at request.get_absolute_uri but am not sure on how to actually use it to get what I want.
Thanks
Upvotes: 5
Views: 7500
Reputation: 39
I know the question is old, but I'm not sure the best answer provided is the correct one.
You should just use
<a href="{{ object.get_absolute_url }}"></a>
as the Django docs point out.
https://docs.djangoproject.com/en/4.0/ref/models/instances/#get-absolute-url
The url provided comes with the domain as well.
Regards
Upvotes: 0
Reputation: 556
This question is pretty old but I think its still relevant.
To get_absolute_url
with domain in Django template you can do the following:
<li><a href="{% if request.is_secure %}https://{% else %}http://{% endif %}{{ request.get_host }}{{ object.get_absolute_url }}">Home</a></li>
First check if the request is https or not and then get the request of the host and pass the absolute url.
This way you will get full URL with domain in Django template.
Upvotes: 2
Reputation: 9767
The way I like to do it is configure a context_processor
:
from django.contrib.sites.models import Site
def base_context_processor(request):
return {
'BASE_URL': "http://%s" % Site.objects.get_current().domain
}
# or if you don't want to use 'sites' app
return {
'BASE_URL': request.build_absolute_uri("/").rstrip("/")
}
in settings.py
:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'path.to.base_context_processor',
...
)
(In newer versions of Django, modify context_processors
under TEMPLATES
, OPTIONS
instead.)
then in templates:
<a href="{{ BASE_URL }}{{ obj.get_absolute_url }}">Object Name</a>
Another solution would be to use request.build_absolute_uri(location)
, but then you would have to create a template tag that takes a request
object and a location
or an object that has get_absolute_uri
method. Then you would be able to use it templates like that: {% get_full_uri request=request obj=post %}
. Here is documentation on how to write custom tags.
Upvotes: 8