How to remove everything before provided value - regex

I am using Bootstrap Vue and would like to use a formatter callback to insert html into a column inside the table. The Bootstrap documentation example formats the link as an anchor link
Ex. https://bootstrap-vue.js.org/docs/components/table/#shirley-partridge
<b-table striped responsive hover :items="episodes" :fields="fields">
<template v-slot:cell(version)="data">
<a :href="`${data.value.replace(/[^a-z]+/i,'-').toLowerCase()}`">{{ data.value }}</a>
</template>
</b-table>
I am pulling in a full url from the version property and would like to only use this url in the template, how can I remove everything before my url using the formatter?
this.episodes = response.map((item) => {
return {
category: item.fields.title,
episode_name: item.fields.splash,
episode_acronym: item.fields.description,
version: 'https://myurl/webinars' + item.fields.slug + '/' +'test',
}
})
Desired link would be https://myurl/webinars....

I was able to get this working, by keeping the relative url format, and updating the formatter with a - instead of a + sign
<a :href="`${data.value.replace(/[^a-z]-/i,'-').toLowerCase()}`">Definition</a>

Related

How to use a javascript variable inside a Django {% url %} tag?

Im trying to dynamically change the link of a button based on which div a user has currently selected.
I tried to run a quick test with a JS variable in the HTML script tag, but Django isn't reading the variable like a num.
<script type="text/javascript">
const testing = 10
</script>
<a href="{% url 'battlefield:onevsone_trainer_selection' num_trainers=testing %}" class='description__button btn btn__large'>Next ></a>
URL looks like:
path('one-vs-one/trainers/<int:num_trainers>', views.oneVsOne, name='onevsone_trainer_selection')
Not sure exactly why it's not working. When I passed it a string of '10' it worked
Django templates are handled server side by django, which means by the time the client browser has received the HTML, the url function, i.e. {% url 'battlefield:onevsone_trainer_selection' num_trainers=testing %}, will have been processed. Javascript, of course, runs in the browser, and you can't pass a javascript variable to a django function after it has arrived at the client.
If you want to change this clientside (without communicating with the server via ajax or something) you might need to change the href attribute directly. You could do something like:
<a id='changeableUrl' href="{% url 'battlefield:onevsone_trainer_selection' num_trainers=1 %}" class='description__button btn btn__large'>Next ></a>
<script>
//figure out where the URL should be going
const testing = 10
//get the current URL
url = document.getElementById('changeableUrl').getAttribute("href")
///replace the end of the url href with our value
url.replace('/1/', '/'+testing+'/')
///write back to the link attribute
document.getElementById('changeableUrl').setAttribute("href", url)
</script>
What you should understand is that django template engine works server-side, whereas Javascript only does its job on client side.
What happens is that you are asking django to render the following template :
<script type="text/javascript">
const testing = 10
</script>
<a href="one-vs-one/trainers/testing" class='description__button btn btn__large'>Next ></a>
Here, testing is not a javascript variable, it's just plain html content. Moreover, it's not an integer at rendering time, so the regex used internally by django shall not be matched and a rendering error shall be raised.
If you really want to set the url paramater client-side, then I see two solutions :
The first is to build the url manually without using the django url function. :
<a href="" id="to-change" class='description__button btn btn__large'>Next ></a>
<script type="text/javascript">
document.getElementById("to-change").href = "one-vs-one/trainers/" + num_trainer;
</script>
The second solution is the one proposed by SamSparx.
Whatever the solution you choose, you should carefully keep in mind that neither of the two if fully safe. In the first case, you make the code a little bit less maintainable by giving more work if one day you change the url in your urls.py file. In the second one, you expose you to a url that is predefined. If JS is not enabled or if something wrong happens on the browser of your client, it may mean that your link shall be linked to what it should not.

How to download file from local directory using python

I want to download a file from the local directory when the user clicks on a particular link. How I can do it using python.
Try this on click of link:
location.replace('your_loacal_filepath')
For Example:
location.replace('/files/out.txt')
if you are rendering HTML, then you should read about the HTML download Attribute
Quoting
The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.
This attribute is only used if the href attribute is set.
else if(currentobj.filename != null){
var xy = '{% static "/documents/dataset/" %}' + currentobj.filename;
alert(xy);
var $tr = $('<tr><td style="padding:5px;"><a href="'+ xy +'"
target="_blank" download>' + currentobj.filename.slice(0,25) +
"....." +'</a></td> </tr>');
$tbl.append($tr);
}
just add download tag along the hyperlink
if you want to specify a specific name for the download file then just mention it like this
<a href="/images/myw3schoolsimage.jpg" download="w3logo">
You can try this.
The download attribute instructs browsers to download a URL instead of navigating to it.
Check out. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

Link to a specific div in another app(page) in Django using anchor tag

I have a div with a certain id
<div id="services">
Then I try to link to it using Django templates
<a href="{% url 'homepage' %}#services">
But it only works if I'm in the same page (App)
Is there a way to work around this ?
I found out what the problem was. I had a script that does smooth scrolling and it had "event.preventDefault();" in it. as I removed that it worked.

How to create html with link-to property inside custom jquery plugin?

I have a custom jquery plugin that use to create drop down html for a search box.
Inside that, html is created as follows.
$.each(stack, function (i, stackItem) {
var url = _getItemURL(stackItem, lang);
if (stackItem.INS !== undefined) {
html += '<li class="row"> ' + stackItem.DS_HIGHLIGHTED + ' - ' + stackItem.E + '<br>' + stackItem.TICKER_DESC + ' </li>';
} else {
html += '<li class="row"> ' + stackItem.COMPANY_DESC_HIGHLIGHTED + '<br>' /*+ _getCountry(stackItem.CC, lang)*/ + ' </li>';
}
itemCount++;
});
As you can see href attribute is used to create links in this html. But when user click on those links application reloads the browser page. I want it to correctly map to different route in the application like in hbs files as follows
<li> {{#link-to 'stock-overview' 'en' 'tdwl' '1010'}}Stock{{/link-to}} </li>
How can I generate html like that inside previous java script code.
Appreciate any help?
First, I would recommend you to think if you really need that jQuery plugin or could go with a plain ember solution. There are some pretty fancy addons like ember-power-select.
But if you really want to do that you need to understand that a {{#link-to}} does not only generate a <a> tag but only handles clicks on it and then doing a transitionTo.
So no, its not possible to do this when you simply append the HTML to the DOM. You would have to catch the click events on the <a> tags manually and then trigger the transition. It depends a bit how you inject the html into the DOM.
One thing you could do is to create a unique id for each <a> tag and after you inserted the html into the DOM you find the <a> tags by the id and attach the onclick event.
A better way would be to create DOM instead of HTML, so manually call document.createElement and then attach the click event.
To trigger the transition you can do something like this inside any Object that has the owner injected (like a component):
Ember.getOwner(this).lookup('controller:application').transitionToRoute('stock-overview', 'en', 'tdwl', '1010');
However cleanly the best solution would be to go for a ember handlebars template and a {{#each}} loop instead of the jQuery solution. To tell you how to do that however we need more information. So best is probably you ask a new question and explain exactly what you want to achieve, and now ask about a problem you have with a probably wrong solution for another problem.

Remove value from GET dictionary and redirect to new URL

I want to have a view in my app that allows the user to remove one of many filters from the GET list, then redirect using the remaining variables in the list. How can I achieve this? Possibly having one filter-remove view that works for all the variables the user can set in the filter.
If I understand you right you're looking for something like this:
from django.http import HttpResponseRedirect
def myview(request):
mypath = ..... #your redirect
remove_get_variable = request.GET.pop('myvar')
return HttpResponseRedirect(mypath)
If you need this more often you could also include this functionality in a middleware!
I think you can achieve this using jquery's library query plugin
<script>
function deleteFilterFromQuery(filter){
//remove this filter from url
var newquery = $.query.Remove(filter);
//redirect to the new url
window.location = newquery;
}
</script>
<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter1">remove filter 1 </a>
<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter2">remove filter 2</a>
<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter3">remove filter 3</a>