Is there a way to use django url template in json_script? - django

trying to find a way to save a url into a json script but its not rendering and reading it literally unlike other values:
Tried this way
{{ "{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}" | json_script:"url_test"}}
And this way:
{{ url_test | json_script:"{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}"}}
But still comes out like this:
<script id="url_test" type="application/json">"{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}"</script>

You can store this in a variable:
{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date as myurl %}
{{ myurl|json_script:"url_test" }}

Related

How to use loop for requesting from html template in Django

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"?

Flask url_for in form action

I have a wtform in my html file, something like this:
{{ wtf.quick_form(form, action="/add/", method="post", extra_classes="form-horizontal",
role="form", form_type="basic") }}
(I actually notice I can omit action). So this works.
Now - if I change it to
action="{{ url_for('add') }}"
, I'm ending up in this url:
http://127.0.0.1:5000/add/%7B%7B%20url_for('add')%20%7D%7D
If I just create a link in html like so:
link
It does work and it gets me to /add/. I was wondering what the difference is.
Thanks!
instead of
action="{{ url_for('add') }}"
try to assign the output of url_for('add') function/helper to action
action=url_for('add')
without {{ and }}
so
{{ wtf.quick_form(form, action=url_for('add'), method="post", extra_classes="form-horizontal", role="form", form_type="basic") }}
Remember not to put commas in the tags ;)

How to execute Django uri template tags from within client side Java Script?

When we have a url tag in our java template it executes on a click
{% url 'product_details' pk=soproduct.id %} and it it is used inside the hyper link the destination will be full path including domain and path.
{{ soproduct.id }}
and works perfect and generate
http://127.0.0.1:8000/production/subproduct/details/7/
But if we want to use same template tag inside client side java script
var destination = {% url 'product_details' pk=soproduct.id %}
there is a problem that the link that will be generated will include the path but will not include the domain
/production/subproduct/details/7/
How this can be resolved?
UPDATE:
This is the script I try to execute it is located in the for loop in the django template .
<script>
var currentLocation = window.location.href;
function AddCardToTrello() {
Trello.addCard({
url: currentLocation,
name: "{{ soproduct.product }}",
due: {{ soproduct.required_date|date:"SHORT_DATE_FORMAT" }}
});
}
function addCardThenGo(url) {
AddCardToTrello();
window.location.href = url;
}
</script>
And right after the script I have my call
<a onclick="addCardThenGo({% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %})">Add to Trello</a>
So script will be repeated in the code as many times as loop goes with new parameters
Oh, I think I got it. If the code you provided is exactly the same you are using, then you forgot about quotes. It should be like this:
var destination = "{% url 'product_details' pk=soproduct.id %}";
And after that you can make this:
window.location.href = destination;
UPDATE
Please, try to do this:
<a onclick="addCardThenGo('{% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}')">Add to Trello</a>

django - how to apply custom template filter to absolute url

I am trying to write a small urlshortener app.
everything is set up well so far. now I am trying to apply the filter to the full url:
{{ request.build_absolute_uri }}{{ request.get_absolute_url}} gives me the full url. But if apply my filter like this:
href="{{ request.build_absolute_uri }}{{ request.get_absolute_url|shortenme}}"
my filter isnot getting anything because request.get_absolute_url is alone giving empty string.
only {{ request.build_absolute_uri }}{{ request.get_absolute_url}} is giving the full url I want. How can I apply my filter to the full url?
Requests do not have a get_absolute_url method.
You want to use request.path or request.get_full_path. The second includes any query string.
See the docs on request attributes for more info.

regex in Django urls.py

Help me please to fix urls.py
People suggested this way, but it does't work for me.....
#urls.py
(r'^/user/(?P<username>)/subject/([\w|\W]+)/$', subject),
#template
{% for subject in subjects %}
<li>{{ subject.name }} {{ del_form.delete }}</li>
{% endfor %}
#error
PAGE NOT FOUND
Request URL: http://127.0.0.1:8000/user/root/subject/Math%20140
....
....
^/user/(?P<username>)/subject/([\w|\W]+)/$
You have an error in your regular expression. You should use a regex builder if you are new to this:
http://ryanswanson.com/regexp/ (Perl)
http://www.pyregex.com/ (Python)
I think you want something like this:
^user/(?P<username>.+)/subject/([\w|\W]+)/
But you might want to change the '.+' to something more restrictive:
^user/(?P<username>[^/]+)/subject/([\w|\W]+)/
Note also that you probably don't want that leading slash - due to the way Django feeds the initial URL to the URL dispatcher.