When the edit option is clicked in the project, I want to transfer all the values in the relevant row into the modal, how can I do this?
here table body
<tbody class="list form-check-all">
{% for x in model %}
<tr>
<td class="id">{{x.name}}</td>
<td class="company_name">{{x.phonenumber}}</td>
<td class="leads_score">{{x.note}}</td>
<td class="phone">{{x.status}}</td>
<td class="location">{{x.callname}}</td>
<td class="date">{{x.dataname}}</td>
<td>
<ul class="list-inline hstack gap-2 mb-0">
<li class="list-inline-item" data-bs-toggle="tooltip" data-bs-trigger="hover" data-bs-placement="top" title="Edit">
<a class="edit-item-btn" href="#showModal" data-bs-toggle="modal"><i class="ri-phone-line fs-16"></i></a>
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
and here my modal
<div class="modal fade" id="showModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-light p-3">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="close-modal"></button>
</div>
<form action="">
<div class="modal-body">
<input type="hidden" id="id-field" />
<div class="row g-3">
<div class="col-lg-12">
<div>
<label for="leadname-field" class="form-label">Name</label>
<input type="text" id="leadname-field" class="form-control" placeholder="Enter Name" required />
</div>
</div>
<!--end col-->
<div class="col-lg-12">
<div>
<label for="company_name-field" class="form-label">Company Name</label>
<input type="email" id="company_name-field" class="form-control" placeholder="Enter company name" required />
</div>
</div>
<!--end col-->
<div class="col-lg-6">
<div>
<label for="leads_score-field" class="form-label">Leads Score</label>
<input type="text" id="leads_score-field" class="form-control" placeholder="Enter lead score" required />
</div>
</div>
<!--end col-->
<div class="col-lg-6">
<div>
<label for="phone-field" class="form-label">Phone</label>
<input type="text" id="phone-field" class="form-control" placeholder="Enter phone no" required />
</div>
</div>
<!--end col-->
<div class="col-lg-12">
<div>
<label for="location-field" class="form-label">Location</label>
<input type="text" id="location-field" class="form-control" placeholder="Enter location" required />
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="hstack gap-2 justify-content-end">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Kapat</button>
<button type="submit" class="btn btn-success" id="add-btn">Kaydet</button>
</div>
</div>
</form>
</div>
</div>
</div>
My codes are like this. When the edit option is clicked from here, I want to inherit the values of the relevant row in the table within the modal. How can I do that?
so basically I would do that this way - when you load modal, the element that triggers it has data-id attribute and you send it over AJAX to the Django view. Then, before the modal's fully open, you change html code of the whole div, prepopulated with single-instance template that you send over using JsonResponse.
Something like that should work:
const loadDetails = (e) => {
let btn = $(e.currentTarget);
const modalAjax = $("#modal-ajax");
$.ajax({
url: btn.attr("data-href"),
type: 'GET',
data: {id: <here_you_put_obj_id_using_data_attr>}
dataType: 'json',
beforeSend: function () {
modalAjax.modal("show");
},
success: function (data) {
modalAjax.find('.modal-content').html(data['html_form']);
}
});
};
Related
So, I have multiple jobs in view with "Report" modal. in that modal there is a form with reasons to report. The problem is that radio button highlights only in first form. if I open report for fifth job it will change first job report modal and there will be no button highlight, but if you open modal of first job, its highlight. I made sure to change name= attribute of every group, so I don't understand why it's happening
<!-- Modal report posting -->
<div class="modal" id="{{post.id}}-1" tabindex="-1" role="dialog" aria-labelledby="validate-modal-label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Report</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{{post.id}}
<div class="modal-body">
<form action="" class="report_job_form" method="post" novalidate>
<input type="hidden" name="crf-protect" id="crf-protect" value="{{csrf_token()}}">
<div class="report-element">
<div class="report-category hoverable">
<p>
<label for="fkj">
<input type="radio" id="fkj" name="report{{post.Position}}_value" value="Fake Job">
<span>Fake Job</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="disc-3">
<input type="radio" id="disc-3" name="report{{post.Position}}_value" value="Discrimination">
<span>Discrimination</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="advert">
<input type="radio" id="advert" name="report{{post.Position}}_value" value="Advertisement">
<span>Advertisement</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="other-2">
<input type="radio" id="other-2" name="report{{post.Position}}_value" value="Other">
<span>Other</span>
</label>
</p>
</div>
</div>
<div class="input-field col s12">
<textarea id="textarea7" class="materialize-textarea" data-length="120" name="details"></textarea>
<label for="textarea2">More Details</label>
</div>
<input type="hidden" name="user-ident" id="user-ident" value="{{current_user.id}}" readonly>
<input type="hidden" name="post-ident" id="post-ident" value="{{post.id}}" readonly>
<input class="btn report-submit" type="submit">
</form>
</div>
This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 8 months ago.
flask code
#bp.route('/delete/<int:id>')
#login_required
def delete_tracker(id):
if Trackers.user_id == current_user.id:
trackers = Trackers.query.filter_by(id=id).first()
print(trackers)
db.session.delete(trackers)
db.session.commit()
return redirect(url_for("authen.dashboard"))
Html code- look for /delete url t bottom there is the error
{% extends "base.html" %}
{% block title %} Dashboard {% endblock %}
{% block content %}
<nav class="sidenav">
<div class="main-buttons">
<a class="nav-link active" id="trackers" aria-current="page" href="/dashboard">Trackers</a>
<a class="nav-link active" id="goals" aria-current="page" href="/goals">Goals</a>
<div>
</nav>
<div id="dashbar">
<nav class="navbar bg-light">
<div class="container-fluid">
<button type="button" class="btn btn-outline-dark" data-bs-toggle="modal" data-bs-target="#exampleModal">
Add Tracker
</button>
<form method="POST">
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add a Tracker</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<label for="tracker_name" class="col-form-label">Tracker Name:</label>
<input type="text" class="form-control" id="tracker_name" name="tracker_name"
placeholder="Enter a Tracker name">
<div class="from-fieldset" style="width:200px;">
<label for="tracker_type" class="form-label">Choose a Tracker type:</label>
<select id="tracker_type" name="tracker_type">
<option value="">choose Tracker type</option>
{% for t in data_trackers %}
<option value="{{t.name}}">{{t.name}}</option>
{% endfor %}
<label for="tracker_description " class="col-form-label">Description</label>
<input type="text" class="form-control" id="tracker_description" name="tracker_description"
placeholder="Enter Description">
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-light border-dark">Submit</button>
</div>
</div>
</div>
</div>
</form>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
</div>
<div class="wrapper">
<div class="container-tracker">
{% for track in user.trackers %}
<div class="tra">
<h3 id="trackername"> {{track.tracker_name}}</h3>
<div class="dropdown-container" tabindex="-1">
<form method="GET" action="/delete/{{track.id}}">
<button type="submit" class="btn btn-outline-light">Delete</button>
</form>
<div class="three-dots"></div>
<div class="dropdown">
<a href="#">
<div>Records and Graphs</div>
</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
( in form method=GET , the delete url is there and the flask code is at top for that endpoint
if I remove if condition in the delete_tracker() fucntion that is if user.id==current_user.id then I will get a different error that is , this is for session 2nd but this is 3rd session something like this, so my file I want to delete is different session )
can you try to change your line
#bp.route('/delete/<int:id>')
to
#bp.route('/delete/<track.id>')
I have a modal popup that allows users to input data. I want to pass that data to Flask so that I can put it in my database. For some reason, no data is being passed. I am printing out request.form and it logs "ImmutableMultiDict([])", when it should contain my form's inputs. I have been trying to use POST, but I'm open to other ideas. This is happening on Heroku, I haven't tested it locally.
HTML
<!-- Modal -->
<div class="modal fade" id="newProjectModal" tabindex="-1" aria-labelledby="newProjectModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newProjectModalLabel">Create Project</h5>
<button type="button" class="btn-close" aria-label="Close"></button>
</div>
<form action="/createproject/" name="createProjectForm" id="createProjectForm" method="POST">
<div class="modal-body">
<div class="mb-3">
<label for="projectNameInput" class="form-label">Project Name</label>
<input type="text" class="form-control" id="projectNameInput" name="projectName" placeholder="New Project">
</div>
<div class="mb-3">
<label for="projectDescriptionInput" class="form-label">Project Description</label>
<textarea class="form-control" id="projectDescriptionInput" name="projectDescription" rows="3"></textarea>
</div>
<label for="selectUsersInput" class="form-label">Assign Personnel</label>
<select class="form-select" id="selectUsersInput" name="selectUsers" multiple aria-label="multiple select example">
{% for user in users %}
<option value="{{ user }}">{{ user }}</option>
{% endfor %}
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary">Close</button>
<button class="btn btn-primary" type="submit" name="submit" value="Submit">Create Project</button>
</div>
</tr>
</form>
</div>
</div>
</div>
Python
#app.route('/createproject/', methods=['POST'])
def createproject():
if request.method == 'POST':
print(request.form)
project_name = request.form.get('projectName')
project_description = request.form.get('projectDescription')
return '''
<h1>The name value is: {}</h1>
<h1>The description value is: {}</h1>'''.format(project_name, project_description)
project_name and project_description are always "None" instead of the entered value
All I want to do is to have a condition in order when the user clicks FOR SALE button to be redirected to url 'salesearch' or when clicks TO RENT to be redirected to url 'rentsearch'.
Here's the code:
<div class="search">
<form action="{% url 'salesearch' %}">
<div class="form-row">
<div class="col-md-4 mb-3">
<label class="sr-only">
Keywords
</label>
<input class="form-control" name="keywords" placeholder="Keyword (Pool, Garage, etc)" type="text">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">
City
</label>
<input class="form-control" name="city" placeholder="City" type="text">
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<button class="btn btn-secondary btn-block mt-4" name="For Sale" type="submit">
For Sale
</button>
</div>
<div class="col-md-6 mb-3">
<button class="btn btn-secondary btn-block mt-4" name="To Rent" type="submit">
To Rent
</button>
</div>
</div>
</div>
</form>
</div>
With the above example I am redirected always at url 'salesearch path. Is there any way to have a conditional to determine the url's path?
Sure is!
You can set formaction on button elements:
<button name="For Sale" type="submit" formaction="{% url 'salesearch' %}">
For Sale
</button>
<button name="To Rent" type="submit" formaction="{% url 'rentsearch' %}">
To Rent
</button>
I want to create a webapp in Django that converts orders in pdf to excel file. To be more flexible i want to retrive text from pdf and pass it to form on HTML page to be editable. If everythig will be ok then i want to download excel file with data from inputs.
There is several inputs (depending on how much rows is in pdf with order) and i want to pass it all to my app. But below code dont work. Only result i have is token and value from button.
print(request.POST.keys())
print(request.POST['Submit'])
print(request.body)
<form action="extract_pdf" method="post">
{% csrf_token %}
<div class="row">
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Company ID</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
{% for item in company %}
<option value="a">{{item.1}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="company_name">Company Name</span>
</div>
<input type="text" class="form-control" id="name" aria-describedby="basic-addon3" value="{{info.company_name}}">
</div>
</div>
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="order_no">Order Number</span>
</div>
<input type="text" class="form-control" id="ord_no" aria-describedby="basic-addon3" value="{{info.order_number}}">
</div>
</div>
</div>
{% for row in rows %}
<div class="row">
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="item_nr">Jeeves Code</span>
</div>
<input type="text" class="form-control" id="item" aria-describedby="basic-addon3" value="{{row.item}}">
</div>
</div>
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="item_qty">Item Qty</span>
</div>
<input type="text" class="form-control" id="qty" aria-describedby="basic-addon3" value="{{row.qty}}">
</div>
</div>
<div class="col-sm">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="item_price">Item price</span>
</div>
<input type="text" class="form-control" id="price" aria-describedby="basic-addon3" value="{{row.price}}">
</div>
</div>
</div>
{% endfor %}
<input type="submit" class="btn btn-success" value="Download" name="Submit" />
</form>
Please help me find bug in my code :)
You need to name your inputs. request.POST is a dictionary of key value pairs from the POST data, and if you do not provide names for the inputs there is no way for django to build that relationship and give you the data in your request.POST. Hope this helps!