Create link with an id : href ="{{ url_for('users/%s'%user[0]) }}" /pb with endpoint and build url (jinja/python/flask ) - url

I am working on a project with 2 app (builded with python/flask).
The first one app. py is the server
The second client.py is the client side.
Now I am building link to acces users/8 from users
But it does'nt work
here under users.html / client.py #route /eror message from jinja
users.html:
{%block body%}
<div class="users">
<div class="list-group">
{% for user in users %}
<a href ="{{ url_for('users/%s'%user[0]) }}" class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{user[1]}}</h5>
<small>{{user[0]}}</small>
</div>
<p class="mb-1">{{user[2]}}</p>
<small>Know more...</small>
</a>
{% endfor %}
</div>
</div>
{%endblock%}
client.py:
#client.route('/users/<id>')#ok works
def user(id=None):
if 'logged_in' in session:
r = actualUser.get(id)
user=json.loads(r.text)
return render_template('user.html', user=user)
else:
return redirect(url_for('login'))
error message:
File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/users.html", line 1, in top-level template code
{%extends "base.html" %}
File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/base.html", line 44, in top-level template code
{%block body%}
File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/users.html", line 12, in block "body"
<a href ="{{ url_for('users/%s'%user[0]) }}" class="list-group-item list-group-item-action">
werkzeug.routing.BuildError: Could not build url for endpoint 'users/7'. Did you mean 'users' instead?

Looks like the users.html is looking for an users list (see {% for user in users %}) and you are passing a single user instance (render_template('user.html', user=user)).
Either you can modify the template and not use the for in loop or update the call to render_template like so render_template('user.html', users=[user])

The error message is pretty explicit here. The issue is an invalid use of the url_for function within the users.html template. You have written this
url_for('users/%s'%user[0])
But url_for expects an “endpoint” argument (i.e. the name of the corresponding route, here it is 'users') and variables to be passed as keyword arguments. More information and examples on usage here: https://flask.palletsprojects.com/en/1.1.x/quickstart/#url-building
Therefore, what you should replace that template instruction with is
url_for('users', id=user[0])
You'll probably end-up having other errors after having fixed this though, because you are passing a single user=user variable to your template whereas you want to loop over a users collection.

Related

create relative links with Thymeleaf

I have the following code which loops through a list of countries and creates a href links
<div class="container" th:each="country: ${countryList}">
<a th:href="${country.countryAbbr}"><div clss="row" th:text="${country.country}"></div></a>
</div>
The current page url is "localhost:8080/directory", and the generated url is showing as
"localhost:8080/us"
How can I make the url show as "localhost:8080/directory/us"
I want "us" to be added to the current url of the page.
Try create the following code in your Controller class.
#RequestMapping(value="/", method=RequestMethod.GET)
public String rootGet() {
return "redirect:/directory";
}
You can use ServletUriComponentsBuilder:
<div th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder)}"
class="container" th:each="country: ${countryList}">
<a th:href="${urlBuilder.fromCurrentRequest().path(${country.countryAbbr}).toUriString()}">
<div clss="row" th:text="${country.country}"></div>
</a>
</div>
Give a try to this one
<div class="container" th:each="country: ${countryList}">
<a th:href="#{${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>
Using # usually resolves the default context but I am not aware of your environment.
For example, if you had Tomcat .war file, and your application would be hosted at localhost:8080/myApp, you would want result /myApp/us rather then /us. # would do that trick.
If above is not relevant for you, use this one:
<div class="container" th:each="country: ${countryList}">
<a th:href="#{'/directory/' + ${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>

Sightly/HTL listChildren on different than current path

In Apache Sling, I can do something like this with HTL/sightly:
<div data-sly-list="${resource.listChildren}">
<p>title: ${item.title}</p>
</div>
Variable resource points to the current path of, say, /a/b/c. How can I get a resource at /x/y/z and list its children? That's through the resolver global variable but I am unclear about the syntax to be used.
You can use data-sly-use to grab a Resource object for a given path:
<sly data-sly-use.test="/content/core-components-examples"></sly>
<ul data-sly-list="${test.listChildren}">
<li>${item.path}</li>
</ul>

Select following-sibling if element unknown

I want to select the first sibling of a tag from an HTML element. Lets say I want to get the <a> that follows this <span>:
<span id="unique"></span>
<a href="#">
This can easily be done with the following Xpath:
//div[#id="unique"]/following-sibling::a
This, however, is very specific in a way that it looks for an <a> tag. How can I make it more generic that it would find any tag as long as it is the first sibling to the element I selected?
Write as below
//span[#id="unique"]/following-sibling::*[1]
Here is a sample HTML :
<div>
<span id="unique"></span>
<p>foo</p>
<p>bar</p>
</div>
XPATH expression:
//span[#id="unique"]/following-sibling::*[1]
output
<p>foo</p>

text displaying in every form field

i'm making an Ember app with a Rails back end. In one of the templates, I list the available teams from a database and beside each team put a form field for the user to enter some textual information. If I enter information in one form field, the text appears in all of the form fields (kind of like a multiple cursor experience). That's one problem. Also, when I submit the button to attend one of the conferences, nothing's happening in the controller. I'm wondering if this is a related problem as Ember might think I'm trying to submit from multiple forms at one time due to the fact that these forms are acting as if they're the same form.
Can you see what I'm doing wrong?
<script type="text/x-handlebars" id="conferences">
<div class='span4'>
{{#each item in model}}
<li> {{#link-to 'conference' item}}
{{ item.name }}
{{ partial 'conferences/form'}}
{{/link-to }}</li>
{{/each}}
</ul>
</div>
<div class="span4 offset4">
{{ outlet}}
</div>
</script>
Inserted Form
<script type="text/x-handlebars" id="conferences/_form">
<form class="form-horizontal" {{action "attend" on="submit"}}>
<div class="controls">
{{input value=username type="text"}}
</div>
<button type="submit" class="btn">attend</button>
</form>
</script>
Conferences controller
App.ConferencesController = Ember.ObjectController.extend({
actions: {
attend: function() {
console.log('this is not logging, & no indication rails route is posted to)
$.post("/join_conference_path", {
username: this.get("username"),
}).then(function() {
document.location = "/";
}, function() {
}.bind(this));
}
}
});
Having all input values synced to the same text happen due to fact that each input value is bound to the same property username.
This happens because all _form partials are rendered in the same context which is in your case App.ConferencesController. To make them render in individual context use itemController argument for each helper. See spec here: http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_each
{{#each item in model itemController="conference"}}
In this case I suggest you rename your App.ConferencesController to App.ConferenceController which will represent individual context for each "item". Having it done this way your action will always use username input for specific "item" only.
Action may not trigger because by default all actions targets routes and not controllers. Try to add target="controller" attribute to action helper:
{{action "attend" on="submit" target="controller"}}

how can i make cucumber test on multiple 'a' tag with image

this is my view code where i have make multiple 'a' tag and want to test third element from li. and we can uniquely identified with offer id as per below code ...
<div class="search_data_outer_div">
<ul>
<li class="small_preview">
<div class="image_area">
<a href="/offer/show/334">
</div>
<span>
<span class="artist_name">Artist 1</span>
<span class="remaining_time">Remaining Time: 7 days</span>
<i id="334" class="icon-remove pointer" style="position:absolute;right:0;display:none;"></i>
</span>
</li>
and i have tried make one step defination ... it is working fine with cucumber but when execute with selenium (WebDriver) then page not open after click.
Scenario:
When I press third offer
Then I should see "YOUR OFFER"
and its step defination file
When /^I press third offer$/ do
page.execute_script %Q{ $(".search_data_outer_div ul li .image_area a").eq(2).click(); }
end
let me know right solution if anybody can help
thanks
IIRC CSS style selectors have issues with some version of Web Driver. Ideally you'd have PageObject variables set up for this so you're not using raw webdriver code in your steps but something like this should work:
browser.div( :class => 'search_data_outer_div').div( :class => 'image_area').links.first.click

Resources