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.
Related
From html template say "createlist" I want to add link which actually request server to get one more object of image form to the "createlist" page. But when I run the code it actually happens only one time but I want maximum of 5 times this should be accepted. So here how I can use loop in html template so that it will get the request again and again up to the limit say 5.
here is the template code -
{% if morepicform %}
{{ morepicform }}
{% endif %}
Want to add more pics? <a href="{% url 'createlist' 'morepic' %}" role="button">
<img src="static/auctions/plus-circle-solid.svg" height="20px" width="20px"></a>
views function
def morepic(request, morepic=''):
return render(request, "auctions/createlist.html",{
"morepicform" : PictureForm(),
"picform" : PictureForm(),
"listform" : ListingForm()
})
url pattern path function -
path("createlist/<str:morepic>", views.morepic, name="createlist")
Now how can I add the functionality of requesting "morepicform"?
I am new to Django, and template tags and HTML and have a template where I use a for loop to fill out bootstrap cards from a database. In the model I has a field Resume_link that has a PDF file. All I want to is have the PDF file displayed in a different template file and not in the card where it is too small to read. (Since I am in the loop when someone clicks the link, I just want the specific resume connected to that card to be shown in the new template.) So all I think I should need to do is somehow either pass the the index of the loop, or another variable that identifies the correct database entry. But I think I am missing something fundamental and don't understand how to pass the value of a template tag in one template another template. Is there some way to pass a variable along with the url to a view so the variable can be used to make a new template tag in the desired template?
{% for key in myres %}
...fill out other parts of cards and create the below link...
<a href="{% url "show_pdf" %}" style="font-size: 20px">
{% endfor %}
where show_pdf is the view where I want to show the whole PDF file.
and that template show_pdf is
What I would like to do is be able to pass the key.Resume_link.url, or if not that the pk for that database table to the show_pdf template.
The view for show_pdf is
def show_pdf(request):
template = 'show_pdf.html'
myres=Research.objects.all()
context = {'myres': myres}
return render(request,'mainapp/show_pdf.html', context)
you can pass the current pdf id in the url and access it.
def show_pdf(request, pdf_id):
template = 'show_pdf.html'
myres=Research.objects.get(id=pdf_id)
context = {'myres': myres}
return render(request,'mainapp/show_pdf.html', context)
In you urls.py you must write like this
path('show_pdf/?P<int:pdf_id>/', views.show_pdf, name="show_pdf")
In HTML write <a href="{% url "show_pdf" key.id %}">
Instead of:
<a href="{% url "show_pdf" %}">
... you would include a parameter, e.g.:
<a href="{% url "show_pdf" pdf_id %}">
The resulting URL would now be formatted as indicated by the corresponding urlconf entry, for instance:
http://my.web.site/resumes/show_pdf/1343
(if pdf_id = 1343 ...)
You must specify as many parameters as the corresponding urlconf entry expects, and you may use either positional or keyword syntax (but not both).
Then, when the user clicks on the link, the View specified in that urlconf will get control, and it will have the specified parameter value (1343 ...) as one of its arguments. You'd select the PDF and send it to the template to be properly presented to the user.
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 }} />
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 }}">
<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
Hitting this url like localhost:8000/app/page works fine.
If I want something from views and append that pageid with url then showing error as Couldn't parse.
From views:{{page.pageid}}
output url should be :localhost:8000/app/?pageid=xx
for this i tried with below syntax:
<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
But above syntax did't worked for me.
urls.py
url(r'^(page)(?:/(?P<page_id>[0-9]+))?/$',page_ViewDetails_TemplateView.as_view(),name="page"),
May be some changes need to be done on urls.py as well.
Can someone share some idea!!
You're confused about at least two things here.
Firstly, you would never use {{ }} inside a tag. You're already in the template language context there: you have access to variables directly.
Secondly, the {% url %} tag works on urlpattern names and parameters, not literal URLs. And your page URL does not expect a querystring value for page_id: it expects it as part of the path. Your generated URL needs to be "/page/3", not "/page?page_id=3".
So your URL tag is just:
<a href="{% url 'page' page_id=page.pageid %}">