Reputation: 175
I am new to django so I am sorry if this is a noob question.
I have created a model "Students" in models.py and a view "Add_Student" for adding a new student, and another view "Display_Students" for displaying all the students. I have a table in which all the students are displaying perfectly with specific columns. But in the table, I added one more column that is "Action" and there is an eye-icon in that column. Now if someone click on that eye-icon so the complete information should be display in a new form/page. Displaying the information in a new form/page isn't the problem, but I don't know how it will work. Similarly, there will be an edit and delete icons.
So here, my question comes, when I click on a eye-icon, how the django will get the desired row id from the table?
from django.db import models
# Create your models here.
class students(models.Model):
firstname = models.CharField(max_length=50)
lastname = models.CharField(max_length=50)
fathername = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip = models.IntegerField(blank=True)
address = models.CharField(max_length=100)
contact =models.CharField(max_length=20)
photo = models.FileField()
Views.py
from django.shortcuts import render, redirect
from home.models import students
# Create your views here.
def index(request):
return render(request, "index.html")
def Add_Student(request):
if request.method == 'POST':
firstname = request.POST.get('firstname')
lastname = request.POST['lastname']
fathername = request.POST['fathername']
city = request.POST['city']
country = request.POST['country']
state = request.POST['state']
zipcode = request.POST['zip']
address = request.POST['address']
contact = request.POST['contact']
photo = request.POST['photo']
Add_Student = students.objects.create(firstname=firstname, lastname=lastname, fathername=fathername, city=city,country=country, state=state, zip=zipcode, address=address, contact=contact, photo=photo)
Add_Student.save()
return redirect('Add_Student')
else:
return render(request, 'student/Add_Student.html')
def Display_Student(request):
Display_Student = students.objects.all()
return render(request, 'student/display_students.html',{'Display_Student':Display_Student})
display_student.html
In this page we can see the icons in last (Action) column. I want when someone click on the eye-con of the first row so it should get all the records from the table of the selected first row. In there is another page where somemore information will display in the form of card in student_profile.html page
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Student Name</th>
<th>Father Name</th>
<th>Contact</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for student in Display_Student %}
<tr>
<td>{{student.firstname}} {{student.lastname}}</td>
<td>{{student.fathername}}</td>
<td>{{student.contact}}</td>
<td>
<a href="student_profile">
<i class="far fa-eye"></i>
</a>
<a href="#">
<i class="far fa-edit"></i>
</a>
<a href="#">
<i class="far fa-trash-alt"></i>
</a>
</td>
</td>
</tr>
{% endfor %}
</tbody>
</table>
student_profile.html
When someone click on that eye-icon the complete information should display in this page.
<div class="card card-primary card-outline">
<div class="card-body box-profile">
<div class="text-center">
<img class="profile-user-img img-fluid img-circle"
src="{% static 'dist/img/user4-128x128.jpg' %}"
alt="User profile picture">
</div>
<h3 class="profile-username text-center">{{student.firstname}}</h3>
<p class="text-muted text-center">Software Engineer</p>
<ul class="list-group list-group-unbordered mb-3">
<li class="list-group-item">
<b>Father Name</b> <a class="float-right">1,322</a>
</li>
<li class="list-group-item">
<b>Contact</b> <a class="float-right">543</a>
</li>
<li class="list-group-item">
<b>City</b> <a class="float-right">13,287</a>
</li>
</ul>
<a href="#" class="btn btn-primary btn-block"><b>Follow</b></a>
</div>
It will be highly appreciated if someone helped me. Please I am new and I have to do this and it is my last hope. Thank you
Upvotes: 0
Views: 3803
Reputation: 1014
In display_student.html
Update
<a href="student_edit">
<i class="far fa-eye"></i>
</a>
with
<a href="{% url 'student_edit' pk=student.pk %}">
<i class="far fa-eye"></i>
</a>
Also update your URL for student_profile
as
'student/<int:pk>/edit', student_edit_view, name="student_edit"
Then you can retrieve the pk in the view as
def student_edit_view(request, pk):
#your_code
Note : pk
means primary key
Upvotes: 1