How to get_absolute_url with domain in Django template? -
so struggling bit, logically seems simple due limited understanding of django not sure , how formulate solution.
basically have blog app set , shows complete(all content including disqus discussion) latest post on home page. post has farther link posts own page well. have set disqus , need key info utilize disqus_url , disqus_identifier. have set model follows method get_absolute_url follows:
def get_absolute_url(self): homecoming reverse('blog.views.renderblog',args=[str(self.id),str(self.slug)]) my view set follows:
def renderblog(request,postid=1,slug=none): template = 'blog_home.html' if(postid == 1 , slug == none): post = post.objects.latest('date_created') else: post = post.objects.get(slug=slug, id=postid) info = { 'post':post, } homecoming render(request, template, data) as can see view set handle both url's follows:
url(r'^$', 'renderblog', name='bloghome'), url(r'^post/(?p<postid>\d{1,4})/(?p<slug>[\w-]+)/$', 'renderblog', name='blogpostpage'), in template setting disqus_identifier = '{{ post.get_absolute_url }}' , hardcoding domain portion in meantime disqus_url = 'http://127.0.0.1{{ post.get_absolute_url }}';.. same goes comment count <a href="" data-disqus-identifier.
i dont doing things in hackish manner, best method me total absolute url. have looked @ request.get_absolute_uri not sure on how utilize want.
thanks
the way configure context_processor:
from django.contrib.sites.models import site def base_context_processor(request): homecoming { 'base_url': "http://%s" % site.objects.get_current().domain } # or if don't want utilize 'sites' app homecoming { '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 utilize request.build_absolute_uri(location), have create template tag takes request object , location or object has get_absolute_uri method. able utilize templates that: {% get_full_uri request=request obj=post %}. here documentation on how write custom tags.
django
No comments:
Post a Comment