Form get detail not found fastapi - forms

I am using FastAPI to render a jinja2 template using a simple get request
This is my template
<!-- list.html !>
<html>
<head>
<title>Item Details</title>
</head>
<body>
{% for item in items %}
<p>{{ item }}</p>
{% endfor%}
</body>
</html>
#app.get("/list_items?search_string={search_string}", response_class=HTMLResponse)
async def list_items(req: Request, search_string: str):
print(8*'*', list(search_string))
items = list(search_string)
return templates.TemplateResponse("list.html", {"request": req, "items": items})
My form is as follows:
#app.get("/")
async def read_items():
html_content = """
<html>
<head>
<title></title>
</head>
<body>
<form action="/list_items/" method="get">
<input type="text" placeholder="Search.." name="search_string">
<button type="submit">Search</button>
</form>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
However, I get a detail not found error.

You should not include the {search_string} part in the route path:
#app.get("/list_items?search_string={search_string}", response_class=HTMLResponse)
^^^^
async def list_items(req: Request, search_string: str):^
You're already asking for this when including it as a parameter in your controller definition.
Instead, use the actual path you want to register the route for:
#app.get("/list_items", response_class=HTMLResponse)
async def list_items(req: Request, search_string: str):
.. and use the same path in your form action:
<form action="/list_items" method="get">
A small side note: since REST endpoints usually represent resources, a standard naming scheme would use just /items instead of /list_items, since GET on /items would mean "list the current items available".

Related

Passing form data to the JSP without passing to the server

html>
<head>
<title>Landing</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body>
<form name="testForm" action="move.jsp">
<label><h1>Enter the data <h1/></label><br/>
<input type="text" name="DATA"><br/>
<input type="submit">
</form>
<% out.println(DATA) %> <!-- WRONG!! -->
</body>
</html>
Please ignore the action part
I am working on a spring mvc project and I have a problem. What I want is that, when the user clicks submit, we should not leave the page, we should just stay. But the values submitted would be used as a parameter to a function in the same page. Here, let's just say I want to print it, and that is the part that is wrongly entered.
What should I do to accomplish this? Please help
You can use ajax here when your submit button is clicked call this function and then using this call your ajax passed the value from your input to your server and then at your server side perform operation which you needed to do and then the result back to ajax .
Your form :
<form name="testForm" action="move.jsp">
<label><h1>Enter the data <h1/></label><br/>
<input type="text" name="DATA"><br/>
<input type="button" onclick="submit_values()">
<!--^^added this-->
</form>
<div id="result"><!--here data will come back--></div>
Then on click of your button submit_values() function will get called . i.e :
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script>
function submit_values() {
//get input value
var values = $("input[name='DATA']").val();
console.log(values);
$.ajax({
type: 'POST',
data: {
values: values//passing to server
},
url: "Your_server_url",
success: function(data) {
alert(data);//this will display whatever server will return
$("#result").html(data);//add response back to show
}
});
}
</script>
Then at your server-side do like below :
String data =request.getParameter("values");//get value
String send_back = something(data);//call your function
out.println("DATA BACK"+send_back );//this will go back to ajax

How to write to an HTML element from the server in Dart

I am trying to learn the basics of server side Dart. When I hit the submit element on the HTML page, the screen is cleared and replaced with "hello." I am trying to keep all elements in place, and put the "hello" in the div place. I have not found any basic documentation on this, so I may be missing a lot here. Is there a straightforward way to do this?
Here is the Server code:
import 'dart:io';
Future main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
8080,
);
await for (HttpRequest request in server) {
request.response
..write("hello")
..close();
}
}
Here is the HTML:
<form action="http://localhost:8080" method="GET">
<input type="text" id="name" value="name">
<input type="submit" value="submit">
</form>
<div id="output"></div> // I want the "hello" to go here
It's quite common to build web applications that pull in data from other web applications, initiated by logic that performs HTTP calls to third party services.
lets look at how we can use the inbuilt HttpRequest classes to make HTTP calls to external services.
index.html
<!DOCTYPE html>
<html>
<head>
<title>A Minimalist App</title>
</head>
<body>
<div id="output">Hi</div> // I want the "hello" to go here
<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
main.dart
import 'dart:html';
void main() {
var response = await HttpRequest.getString('http://localhost:8080')
querySelector('#output').text = response;
}
For more info visit here
Thanks. :) , hope it helps.

#session.get not working in scala html template

I have my #session.get("id") not working below. after logging into the system, I set the session value id with .withSession("id" -> id) in my controllers. Is there a way to get this session.get working?
main.scala.html
#(title: String)(content: Html)
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" cantent="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimun-scale=1.0, user-scalable=no" />
<title>#title</title>
</head>
<body>
<div class="main container">
<div class="navigation container">
<div class="logo container">
<div class="user">#session.get("id")</div>
<div class="separator"></div>
</div>
<div class="separator"></div>
<ul>
<li class="user">USER</li>
<li class="code">CODE</li>
</ul>
</div>
<div class="header container">
<div class="left"></div>
<a class="right" href="#all-menu"></a>
</div>
<div id="mainnav" class="mainnav">
#content
</div>
</div>
</body>
This cause not found: value session error. Same not found issue even after adding
#()(implicit session: play.api.mvc.Session)
And this is my index.scala.html which becomes a basic structure of my templates.
#*
* This template takes a single argument, a String containing a
* message to display.
*#
#(message: String)
#*
* Call the `main` template with two arguments. The first
* argument is a `String` with the title of the page, the second
* argument is an `Html` object containing the body of the page.
*#
#main("Welcome") {
#*
* Get an `Html` object by calling the built-in Play welcome
* template and passing a `String` message.
*#
#message
}
Please Help!
You need to explicitly pass the session to your template:
vies.htlm.index(message,session) from your action. And then further pass this session to your main.scala.html.
And in index.scala.html, remove implicit from the session parameter.
Or, pass (implicit request: Request[T]) in your index.scala.html and also in your main.scala.html.
#(message: String)(implicit request: Request[T]) -> index
#(title: String)(content: Html)(implicit request: Request[T]) -> main
You can access the session like:
request.session.get("key")
This will do the needful.

Show method from resource controller returns null

I'm having trouble with learning laravel and decided to ask my question here since I can't find an answer via Google etc.
I am trying to return a client from the database via Id, maybe later via name.
This is my form:
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<p><<form action="{{route('client/'.$client->id.'/show/')}}"
method="get">
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="price">Achternaam:</label>
<input type="text" class="form-control" name="id">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
{{csrf_field()}}
<button type="submit" class="btn btn-success" style="margin-
left:38px">Zoek klant</button>
</div>
</div>
</form></p>
</body>
</html>
Which redirects to:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<div class="container">
<h2>Voer een klant in</h2><br />
<h1>Showing {{ var_dump($client) }}</h1>
</div>
</body>
</html>`
And this is the show method from the Clientcontroller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
This is the route I'm using:
Route::get('client/{id}/show','ClientController#show');
Can someone spot my mistake, because I can't after messing with this the last few hours.
EDIT: updated code and added the route, now getting the error that the client variable isn't defined in the
You must echo the client id in the form action. So, remove the inverted comma from $client->id. Use the following code :
<form action="{{action('ClientController#show', $client->id)}}"
method="get">
Additionally, remove backtick from the code. Use the following code :
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your routes/web.php
Route::get('client/{id}/show','ClientController#show');
In your form:
<form action="{{URL::to('client/'.$client->id.'/show/')}}"
method="get">
In your controller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your Client Controller make sure you use the Model that you are referencing to
Assuming App\Client.php Change it to App\User.php if you are referring to user
ClientController
use App\Client;
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
Now you can fetch the client model with $client For example $client->id
Make sure you pass the {id} parameter in the route

Why does Lift escape this <lift:bind> value?

I have (roughly) this LIFT-ified HTML in my default template:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1><lift:bind name="page-title" /></h1>
<div id="page-content">
<lift:bind name="page-content" />
</div>
</body>
</html>
...and then this in my main template:
<lift:surround at="page-content">
<lift:bind-at name="page-title">Home</lift:bind-at>
</lift>
...which give me this in the generated HTML:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1>Home</h1>
<div id="page-content">
</div>
</body>
</html>
Why is the <lift:bind> tag in the <title> getting escaped, and the one in the <body><h2> not? And how do I prevent that from happening?
Are the pages defined through SiteMap? As was mentioned before, <title> can be a special case, and it is interpreted in several places - some of which might be doing the escaping. If you can, I'd try setting the page title in one of two ways:
Through the Sitemap you can use the Title Loc Param as referenced here: Dynamic title with Lift
You can also have something like: <title data-lift="PageTitle"></title> to have it invoke a snippet called page-title. Where, the snippet would be something like:
class PageTitle {
def render = "*" #> "FooBar Application | Home"
}