How to send data from index.html to views.py in django - javascript

I have a table contains a list of fifty companies(items) on a side there is a button. I want to send the name of the company from the table in which user is clicked.
Code of index.html :
<table class="table table-striped table-dark" cellspacing="0">
<thead class="bg-info">
<tr>
<th>Company's Symbol</th>
<th>Current Price</th>
<th>View Current chart</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for a,b in stocks %}
<tr>
<th scope="row" class="comp_name">{{ a }}</th>
<td>{{ b }}</td>
<td>
<input type="submit" class="btn graph-btn" name="_graph" value="View Graph">
</td>
<td>
<input type="submit" class="btn predict-btn" name="_predict" value="Predict Closing Price">
</td>
</tr>
{% endfor %}
</tbody>
</table>
There are two buttons for different URL. Like when user liked on .graph-btn it will go to a different URL.
Help.

If you want to go to another path (Route) that needs variables
in your template you can use the url function as shown below :
.....
if you want to get the same result from your python code user reverse method
return HttpResponseRedirect(reverse('url_path', args(var1)))
for more check django's documentation here

Related

How to get the value of for each loop and use it in route query parameters using laravel

I have this problem wherein I have a table from foreach loop like example below and try to pass it on a url path href="{{ url('grade/semester/schoolyear')}}"
SEMESTER
SCHOOL YEAR
Action
1st
2020-2021
View
2nd
2019-2020
View
1st
2018-2019
View
2nd
2018-2019
View
Blade Code:
<table class="table">
<thead>
<tr >
<th scope="col" style="color:#1a0dab">Semester</th>
<th scope="col" style="color:#1a0dab">Academic Year</th>
<th scope="col" style="color:#1a0dab">Action</th>
</tr>
</thead>
<tbody>
#foreach($years as $year)
<tr>
<td style="text-align: left;">{{ $year->sem}}</td>
<td>{{ $year->sy}} </td>
<td>
<a title="View Grade" **href="{{ url('/grade/{{$year->sem}}/{{$year->sy}}')}}"**>
<i class="fas fa-file-alt"></i>
</a>
<a title="Print Grade" href="#">
<i class="fas fa-print"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
What I would like to get is the correct route: http://127.0.0.1:8000/grade/1st/2018-2019 when I click the link View of the row number 3 in my table so that it will give me the correct path.
Thank you very much for you answer.
You cannot evaluate the template multiple times, what you can do is to concatenate the string in order to obtain the url you want:
{{ url('/grade/' . $year->sem . '/' . $year->sy) }}
where . is the PHP operator for string concatenation
In web.php define your route as
Route::get('/grade/{sem}/{sy}')->name('grades');
In blade you can then access this route via its name and add parameters:
LinkTitle
grades in route('grades'... is the name given.
https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes

Passing the variable in the url in HTML

I am using the HTML and Javascript for an application which I built. I am using onlclick function,when I click the button it should take me to the new page.
function editStudent(studentId) {
console.log(studentId);
document.location.href = '/student/edit/{{studentId}}' {
{
studentId
}
}
}
<table class="table table-striped">
<thead>
<tr>
<th>Student ID</th>
<th>Student role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for each_student in result %}
<tr>
<td>{{ each_student.student_id }}</td>
<td>{{ each_student.studnet_role }}</td>
<td><input class="btn btn-info" type="button" onclick="editStudent('{{each_student.student_id}}')" id="edit" value="Edit"></td>
</tr>
{% endfor %}
</tbody>
</table>
When I click the edit button it goes to the new page with this url http://localhost:6464/student/edit/,
What my expected output is
/student/edit/studentid(studentid value has to be printed for example if it's 1,it should be student/edit/1)
if you do concatenation it may work.
document.location.href = '/student/edit/'+ studentId
In this case, studentId is being passed to the function as a Javascript variable, but you're treating like if it was still inside the loop, using {{studentId}}. Just use Javascript concatenation:
document.location.href = '/student/edit/' + studentId;
Refere this answer,
Some of code removed because {% for each_student in result %} will not work here.
You are passing studentId to javascript, so just use that variable in javascript function.
function editStudent(studentId) {
console.log(studentId);
location = '/student/edit/' + studentId;
}
<table class="table table-striped">
<thead>
<tr>
<th>Student ID</th>
<th>Student role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>student_id</td>
<td>studnet_role</td>
<td><input class="btn btn-info" type="button" onclick="editStudent(1)" id="edit" value="Edit"></td>
</tr>
</tbody>
</table>

Dynamic table Javascript - Angular 2

I m trying to access to some information in a table by HTML and it is working, but I would like to do it after clicking a button and not onInit, so I guess that I need to do it by Javascriptor Typescript
It would be perfect to create the table by HTML but filling it by Javascript or Typescript after clicking the button.
The table is the following:
HTML
<table id="myTableSystems" class="table table-bordred table-striped">
<thead>
<th>{{ 'Checkbox' | translate }}</th>
<th>{{ 'MappingMult' | translate }}</th>
</thead>
<tbody>
<tr *ngFor="let receiver of testCase.receivers;" class="pointer">
<td>
<input type="checkbox">
</td>
<td>{{receiver.mappingMult}}</td>
</tr>
</tbody>
</table>
In your component.ts file you will need to create a function to fill the object "testCase".
I'm not sure how the component is getting/receiving the data for "testCase". You will need to refactor the logic so that you get the data for "testCase" in the fillData function.
component.ts
public fillData(): void {
this.testCase.receivers = [{mappingMult: 'data'}];
}
component.html
<button type="button" (click)="fillData()" class="">Click To Fill Data</button>
<table *ngIf="testCase" id="myTableSystems" class="table table-bordred table-striped">
<thead>
<th>{{ 'Checkbox' | translate }}</th>
<th>{{ 'MappingMult' | translate }}</th>
</thead>
<tbody>
<tr *ngFor="let receiver of testCase.receivers;" class="pointer">
<td>
<input type="checkbox">
</td>
<td>{{receiver.mappingMult}}</td>
</tr>
</tbody>

How to provide an alert option to delete a table data from jstl tag

I have a table which contains data from my mongodb database.
Table data is as:
<tr>
<th class="text-center">Service Code</th>
<th class="text-center">Name</th>
<th class="text-center">Current Price</th>
<th class="text-center">delete</th>
</tr>
<c:forEach var="ser" items="${services}" varStatus="status">
<c:url var="deleteUrl" value="/serviceMaster/deleteType?serviceMasterId=${ser.serviceMasterId}" />
<tr>
<td class="text-center">${ser.serviceCode}</td>
<td class="text-center">${ser.serviceName} (${ser.serviceAbbreviation})</td>
<td class="text-center">Service Cost: ${ser.serviceCost}<br>Cost Price: ${ser.costPrice}</td>
<td class="text-center"><a href='<c:out value="${deleteUrl}"/>'><i id="del"
class="glyphicon glyphicon-trash"
style="color: #c12e2a; margin-left: 20px"
data-toggle="tooltip" title="Delete"></i></a></td>
</tr>
and this URLtriggers my controller which gets the Id and deletes the particular data from my db.
What is the best way to add alert to this approach, or confirmation message before deletion.
you can create function on link for confirmation or change as per below code.
Click

how to pass table row selected using checkbox in html n pass the data to views.py django

m new to python-django need help...
i want to pass the selected table row from template to views.py with checkbox checked using post method..and can select multiple rows...
n after submitting the row with POST method to views.py the selected row should get disappear from template
template:a.html
<tr > {% if getlog %}
<form action="{% url list %}" method="post">{% csrf_token %}
<td><input type="checkbox" id="check_all" checked="checked"/></td>
<thead><th COLSPAN="2">App Name</th>
<th COLSPAN="2">File Name</th>
<th COLSPAN="2">Function Name</th>
<th COLSPAN="2">Path Name</th>
<th COLSPAN="2">Line No</th>
<th COLSPAN="2">Level Name</th>
<th COLSPAN="2">Message</th>
<th COLSPAN="2">Thread Name</th>
</tr>
</thead>
{% for log in loglist %}
<tr id="tr1">
<td><input type="checkbox" class="check_field" checked="checked" name="list1"/> </td>
<td colspan="2" > {{ log.app.appname }} </td>
<td colspan="2"> {{ log.filename }}</td>
<td colspan="2" >{{ log.funcname }}</td>
<td colspan="2" >{{ log.pathname }}</td>
<td colspan="2" >{{ log.lineno }}</td>
<td colspan="2" >{{ log.typeid.levelname }}</td>
<td colspan="2" >{{ log.typeid.message }}</td>
<td colspan="2">{{ log.threadName }}</td>
</tr>
{% endfor %}
</tbody>
</table>
how to do that???where m going wrong?
thanks...:)
You're probably going wrong by using a non-Django way of handling data submission.
Django's power lies in a very robust forms system which can generate the forms for you as well as handling common tasks like saving records to the database, form validation, etc.
I know it's a pain but it really is worth it to take some time to run through the Django book and do some sample exercies so you can get familiar with Django basics. With that in hand you can think about what steps you'd take to move forward on the things you actually want to do.
Yours is a special case so a straightforward formset or ModelForm won't really do the trick, here's an impementation that I'd recommend.
models.py
class Log(models.Model):
# whatever fields you want
date_hidden = models.DateTimeField(null=True, blank=True)
forms.py
class LogDisplayForm(forms.Form):
logs_to_hide = forms.ModelChoiceField(required=False, queryset=Log.objects.exclude(date_hidden__isnull=False)
def save(self, *args, **kwargs):
if self.cleaned_data.get('logs_to_hide'):
Log.object.filter(pk__in=self.cleaned_data['logs_to_hide']).update(date_hidden=datetime.datetime.now())
Then in your template form, just rename list1 to logs_to_hide and make sure the value of the field is the primary key of the log record. This assumes that you read up on how Django actually works and know how you'd handle form validation and form saving in the view.
Note that the code I have given you is incomplete and won't work without considerable additional work on your part. Which is the point.

Categories

Resources