I have a model called Primary and I want to access the fields of the models inside my viewStudent template, because i have more fields in the models.
the model:
class Primary(models.Model):
profilePicture = models.ImageField(blank=False, null=False)
firstName = models.CharField(max_length=25, blank=False, null=False)
sureName = models.CharField(max_length=25, blank=False, null=False)
lastName = models.CharField(max_length=25, blank=False, null=False)
address = models.CharField(max_length=50, blank=False, null=False)
classOf = models.CharField(max_length=20, blank=False, null=False)
yearOfGraduations = models.CharField(max_length=20, blank=False, null=False)
hobbies = models.TextField(blank=False, null=False)
dateOfBirth = models.CharField(max_length=20)
year = models.ForeignKey(Album, on_delete=models.CASCADE)
when i type this inside my: viewAlbum template it’s throws me an error like this: NoReverseMatch at /viewAlbum Reverse for ‘view-Student’ with arguments ‘(”,)’ not found. 1 pattern(s) tried: [‘view/(?P[0-9]+)/\Z’]
<a href="{% url 'view-Student' primaries.id %}">view Students</a>
the urls.py
path('viewAlbum', views.viewAlbum, name='view-Album'),
path('view/<int:pk>/', views.viewStudent, name='view-Student'),
the views:
def viewAlbum(request):
primaries = Primary.objects.all()
return render(request, 'viewAlbum.html', {'primaries': primaries})
def viewStudent(request, pk):
post = get_object_or_404(Primary, id=pk)
primaries = Primary.objects.get(id=pk)
return render(request, 'viewStudent.html', {'post': post, 'primaries': primaries})
inside my viewAlbum template i have tried this:
<a href="{% url 'view-Student' primaries.pk %}">view Students</a>
the viewAlbum template:
<div class="container">
<div class="row justify-content-center">
{% for prima in primaries %}
<div class="col">
<div class="card my-2" style="width: 18rem;">
<img src="{{ prima.profilePicture.url }}" alt="" class="card-img-top">
<div class="card-body">
<p>{{ prima.firstName }}</p>
<br>
<a href="{% url 'view-Student' prima.pk %}">view Students</a>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
the viewStudents template:
<div class="container">
<div class="row justify-content-center">
<div class="card">
<div class="card-body">
<div class="row justify-content-center">
<img src="{{ prima.profilePicture.url }}" alt="" class="card-img-top">
<p>{{ prima.firstName }}</p>
</div>
</div>
</div>
</div>
</div>
but it’s not working
how can I solves this problem?
>Solution :
In your view,
def viewAlbum(request):
primaries = Primary.objects.all() # RETURNS ALL
return render(request, 'viewAlbum.html', {'primaries': primaries})
primaries does not have a pk, since it is a queryset.
Perhaps you mean to iterate through ALL the Primary objects, something like this?
{% for p in primaries %}
<a href="{% url 'view-Student' p.pk %}">view Student</a>
{% endfor %}
Edit
The name you pass through the view must match the one you use in the template. Since you are using the name prima in your template, then that is what you must pass in the view:
def viewStudent(request, pk):
post = get_object_or_404(Primary, id=pk)
prima = Primary.objects.get(id=pk)
return render(request, 'viewStudent.html', {'post': post, 'prima': prima})