In my Django template I am having some problem to hyperlink. The link is a dynamically generated IP (not a file location).
in views.py
def basestations(request, host_id):
'ipaddr': basestation.mni_address
some code
return render_to_response('basestations.html', locals(), context_instance=RequestContext(request))
This variable holding the dynamically produced IP address from database and passes to front end in the variable name basestation.mni_address
In Django template, I want the basestation should be hyperlinked with the basestation.mni_address
<td><a href="{% url what should I write here? %}"><i
class="icon-th-large"></i> {{ basestation.name }}</a></td>
e.g. basestation.name is also passed to front end dynamically. This basestation.name should be hyperlinked with the IP address, means while clicking on the name, user should be forwarded to a link as ex -'http://192.168.255.66'
Any help is much appreciated.
You wouldn't use {% url %} at all. That's for generating links to other URLs within your Django app. You want to link to an IP address, so just put that value in directly:
<a href="{{ basestation.mni_address }}">
Related
I have a template which I am going to send as an email.
Thus I need to have absolute full urls (along with protocol and domain name) instead of relative ones.
The content in the email is going to come dynamically from database (entered using ckeditor, so I CAN NOT do something like {{ protocol }}{{ domain_name }}{% static '' %}. This would work only for static files. However the media content uploaded via ckeditor will recide in media files and I have absolutely no control over it.
Also i cant use javascript as it is an email template.
Currently I have built a python function which scans the entire template after rendering and prepends the protocol and domain name to every src attribute in img tag and all href attributes.
I would like to know if any better way exists
You can use request.build_absolute_uri and make a custom template tag in order to used when rendering your mail template.
Example
#templatetags/url_helper.py
#register.simple_tag()
def full_uri(request, relative_url):
return request.build_absolute_uri(realtive_url)
Then ...
{# Some template.html #}
{% full_uri request some_img.url as full_img_url %}
<img src={{ full_img_url }} />
I'm trying to dynamically generate a URL based on a context variable (objecttype) passed into a template:
<a href="{% url 'wakemeup:edit_object' objecttype='school' objectid='new' %}">
Instead of objecttype='school', I want something like objecttype={{ objecttype }}.
I tried using the |add: operator and including {{ objecttype }} directly in the URL, but I keep getting parsing errors.
I've created a link in a Django template which appears to lead to the correct URL in the elements pane of the developer tools. But the actual link in the page only goes to localhost. I thought I understood how the Django url template tag worked, but I must be missing something.
URL pattern:
url(r'^dollhouse/(?P<dollhouse>[0-9]+)$', views.dollhouse, name='dollhouse')
Template:
{% for workingdollhouse in dollhouses %}
<a href src="{% url 'dollhouse' dollhouse=workingdollhouse.id %}">{{workingdollhouse.dollhouse_name}}</a>
{% endfor %}
Text of element appearing in developer console (hovering over the '"/dollhouse/1"' reveals the intended address of localhost/dollhouse/1):
'<a href src="/dollhouse/1">dollhouse1</a>'
Actual link just goes to localhost.
This has nothing to do with Django.
a elements don't have a src attribute. The destination goes in the href attribute, which you left blank.
{{workingdollhouse.dollhouse_name}}
i have some link in a template to which i want to point to particular url.
template is accessed at url : loacalhost:8000/account/profile
{% for poll in voted_poll_list %}
<h4>{{ poll.title }} </h4>
{% endfor %}
in models.py i have created the url for the poll objects to be used in a template.
def link_url(self):
return "polls/"+ "allcat/" +str(self.id)
the problem is when a link in template is clicked it is point to the loacalhost:8000/account/profile/polls/allcat/1 instead of loacalhost:8000/polls/allcat/1 which matches to url pattern
url(r'^polls/(\w+)/(?P<pid>[-\d]+)/', 'pollsite.views.poll_detail', name='detail_poll'),
the problem is link url of object is appended to current url. how can i avoid this ?
#garnertb 's solution works, but you should probably look into using the reverse function rather than hardcoding your url.
Something like:
return reverse('detail_poll', self.id)
This will not only take care of the leading slash, but also avoid trouble if you ever start changing your url configuration.
Try leading the url with a forward slash:
def link_url(self):
return "/polls/allcat/" +str(self.id)
My model: a "Workshop" has a related item that stores time and place in a "Session" object:
item=models.OneToOneField(Session)
a Session stores the location as a foreign key to the locations:
location = models.ForeignKey(conference_models.Location,
verbose_name=_("location"), blank=True, null=True)
I'm showing a Workshop in a template and I want to link to its location. If the template is a single Workshop view, this all works, but if I pass a list of workshops and wrap this all in:
{% for w in workshops %}
then bad things happen...
I have this in urls.py to define the URL for a location view:
url(r'^locations/(?P<location_pk>\d+)/$',
views.view_location, name='view-location'),
and in the workshop template I have:
<a href="{% url 'view-location' location_pk=w.item.location.pk %}">
{{w.item.location.pk}} {{w.item.location}}</a>
- {{w.item.start}} to {{w.item.end}}
And I get Reverse for 'view-location' with arguments '()' and keyword arguments '{'location_pk': ''}' not found.
where location_pk is the parameter to my location view. Everything seems to be correct. For example, if I do:
<a href="{% url 'view-location' location_pk=123546 %}">
{{w.item.location.pk}} {{w.item.location}}</a>
- {{w.item.start}} to {{w.item.end}}
I get the expected URL with 123546 in it. If I do:
<a href="{% url 'view-location' location_pk=w.item.pk %}">
{{w.item.location.pk}} {{w.item.location}}</a>
- {{w.item.start}} to {{w.item.end}}
then I get the item primary key in the URL (which isn't what I want, but proves the point that I'm not going mad expecting this to work...).
In all cases the {{w.item.location.pk}} tag expands to the correct value.
I've tried wrapping it in a {% with %} tag so there's no dotting going on. No joy.
This is Django 1.4.5, part of a complex project that probably won't handle an update to 1.5. If this requires 1.5 for a bigfix I'll have to rethink...
Note this only seems to happen in a {% for %} loop...
Since location field has null=True, blank=True, There might be some entries which are null, hence w.item.location.pk is evaluating to ''.
You can check {% if w.item.location %} and then load the URL for location.