how to update a table value after click - django

lets think you have a table like this
MODELS.PY
class test(models.Model):
name = models.CharField(max_length=20)
pluspoints = models.BooleanField(default=False)
minuspoints = models.BooleanField(default=False)
total_points = models.IntegerField()
def __str__(self):
return str(self.name)
points.html
<form id="points_form">
{% csrf_token %}
<div class="row">
<div class="col-3">
{{ form.name|as_crispy_field }}
</div>
<div class="col-3">
{{ form.pluspoints|as_crispy_field }}
</div>
<div class="col-3">
{{ form.minuspoints|as_crispy_field }}
</div>
</div>
<br>
<input class="btn btn-outline-primary" type="submit" name="submit" value="give the points" />
</form>
views.py
def points(request):
points_filter = test.objects.filter(total_points=True)
points_count = test.objects.count()
context = {
'points_filter':points_filter,
'points_count':points_count,
}
return render(request, 'point/points.html', context)
now, I want to update the "total_points" -1 when I click on minuspoints and +1 when I click on pluspoints. Is this possible? or what could I do else.

Related

How to get value in Dropdown list on edit in Django template

I have a form (edit_city.html) where I want to edit my record, there is also one dropdown
field in which data is retrieving from another model name Country. How can I get the exact
value in dropdown field, when I click on edit.
Here is my Code
class Country(models.Model):
CountryID = models.AutoField(primary_key=True)
CountryName = models.CharField(max_length=125, verbose_name="Country Name")
def __str__(self):
return self.CountryName
class City(models.Model):
CityID = models.AutoField(primary_key=True)
CityName = models.CharField(max_length=125, verbose_name='City Name')
Country = models.ForeignKey(Country, verbose_name='Country Name',
on_delete=models.CASCADE)
def __str__(self):
return self.CityName
views.py
def Edit_City(request, id):
city = City.objects.get(CityID=id)
country = Country.objects.all()
context = {
'city':city,
'country':country,
}
return render(request, 'City/edit_city.html', context)
edit_city.html
<form method="post" action="{% url 'update_city' %}">
{% csrf_token %}
<div class="row">
<div class="col-12">
<h5 class="form-title"><span>Edit City</span></h5>
</div>
{% include 'includes/messages.html' %}
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="">Country</label>
<select class="form-control" name="country_id" required>
<option>Select Country</option>
{% for con in country %}
<option value="{{con.CountryID}}">{{con.CountryName}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label>City Name</label>
<input type="text" class="form-control" name="city_name" value="{{city.CityName}}" required>
<input type="text" class="form-control" name="city_id" value="{{city.CityID}}" required hidden>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Update City</button>
</div>
</div>
</form>

Django multiple of the same dependent dropdownboxs in one form

I have a problem with the following;
I have a dependent dropdown menu in a Django model form. Now I want to use it several times in the same form but I can't get it to work if I load more than one then I can only enter/use one. And I have a 2nd problem if this works how do I get the different values in a list/array so that I can store them in the db?
The ultimate goal is to select how many beams are desired and on that basis to be able to enter the desired hours, ion species, energy and flux per beam.
My code is as follows:
models.py
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
class IonSpecies(models.Model):
# ionspecie = models.CharField(max_length=10)
Name = models.CharField(max_length=10)
def __str__(self):
# return self.ionspecie
return self.Name
class Energys(models.Model):
Ion_Species = models.ForeignKey(IonSpecies, on_delete=models.CASCADE)
Name = models.CharField(max_length=50)
def __str__(self):
# return self.energy
return self.Name
# Create your models here.
class CreateBeamRequestModel(models.Model):
#status choices
SELECT = 'Select'
REQUEST = 'Request'
TENTATIVE = 'Tentavive'
ACCEPTED = 'Accepted'
CONFIRMED = 'Confirmed'
CANCELLED = 'Cancelled'
COMPLETED = 'Completed'
QUEUED = 'Queued'
STATUS_CHOICES = [
(SELECT, ('Select an option')),
(REQUEST, ('Request')),
(TENTATIVE, ('Tentative')),
(ACCEPTED, ('Accepted')),
(CONFIRMED, ('Confirmed')),
(CANCELLED, ('Cancelled')),
(COMPLETED, ('Completed')),
(QUEUED, ('Queued')),
]
#different beam choices (1 to 4)
DIFBEAMS_CHOICES = [tuple([x,x]) for x in range(1,5)]
#shift choices (1 to 16 is max a week)
SHIFTS_CHOICES = [tuple([x,x]) for x in range(1,17)]
#fields of the model
Project_Code = models.CharField(unique=True, max_length=20, blank = True)
Pac_Rate = models.CharField(max_length=10, blank = True)
Partrec_Contact_Name = models.CharField(max_length=50, blank = True)
Partrec_Contact_Email = models.EmailField(blank = True)
Previous_Experiment = models.CharField(max_length=200, blank = True)
Status = models.CharField(max_length=25, choices=STATUS_CHOICES, default='SELECT')
Project_Title = models.CharField(max_length=100, blank = True)
Spokesperson_Name = models.CharField(max_length=50, blank = True)
Spokesperson_Adress = models.CharField(max_length=50, blank = True)
Spokesperson_Phonenumber = models.CharField(max_length=20, blank = True)
Spokesperson_Email = models.EmailField(blank = True)
Collaborator_Name = models.TextField(blank = True)
Collaborator_Nationality = models.TextField(blank = True)
Collaborator_Home_Institute = models.TextField(blank = True)
Different_Beams = models.IntegerField(choices=DIFBEAMS_CHOICES, default='1')
# Shifts = models.IntegerField(choices=SHIFTS_CHOICES, default='1')
Hours = models.IntegerField(default='1')
Ion_Species = models.ForeignKey(IonSpecies, on_delete=models.CASCADE)
Energy = ChainedForeignKey(Energys, chained_field="Ion_Species", chained_model_field="Ion_Species", show_all=False, auto_choose=True)
Flux = models.CharField(max_length=50, blank = True, null = True)
Start_Date = models.DateTimeField(blank = True, null=True)
End_Date = models.DateTimeField(blank = True, null=True)
Requiered_Equipment = models.TextField(blank = True)
Special_Requirements = models.TextField(blank = True)
Special_Safety_Procedures = models.TextField(blank = True)
Lab_Support_Requirements = models.TextField(blank = True)
Funded = models.TextField(blank = True)
Summary = models.TextField(blank = True)
def __str__(self):
return self.Project_Code
def get_absolute_url(self):
return f"/beamrequest/{self.Project_Code}"
def get_edit_url(self):
return f"{{self.get_absolute_url}}/update/"
def get_delete_url(self):
# return f"/beamrequest/{self.Project_Code}/delete/"
return f"{{self.get_absolute_url}}/delete/"
forms.py
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block extrahead %}
{% endblock %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
<form method="post" novalidate>
{% csrf_token %}
<div class="Error_Messages">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
<br><br>
<b>Internal contact information</b>
<hr class="divider"></hr>
<div class="row">
<div class="col-6">
{{ form.Project_Code|as_crispy_field }}
</div>
<div class="col-6">
{{ form.Partrec_Contact_Name|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col-6">
{{ form.Pac_Rate|as_crispy_field }}
</div>
<div class="col-6">
{{ form.Partrec_Contact_Email|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col-6">
{{ form.Previous_Experiment|as_crispy_field }}
</div>
<div class="col-6">
</div>
</div>
<div class="row">
<div class="col-6">
{{ form.Status|as_crispy_field }}
</div>
<div class="col-6">
</div>
</div>
<br><br>
<b>External contact information</b>
<hr class="divider"></hr>
<div class="row justify-content-center">
<div class="col-6">
{{ form.Project_Title|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col-6">
{{ form.Spokesperson_Name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.Spokesperson_Adress|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col-6">
{{ form.Spokesperson_Phonenumber|as_crispy_field }}
</div>
<div class="col-6">
{{ form.Spokesperson_Email|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col-4">
{{ form.Collaborator_Name|as_crispy_field }}
</div>
<div class="col-4">
{{ form.Collaborator_Nationality|as_crispy_field }}
</div>
<div class="col-4">
{{ form.Collaborator_Home_Institute|as_crispy_field }}
</div>
</div>
<br><br>
<b>Beam information</b>
<hr class="divider"></hr>
<div class="row">
<div class="col-6">
{{ form.Start_Date|as_crispy_field }}
</div>
<div class="col-6">
{{ form.End_Date|as_crispy_field }}
</div>
</div>
<div class="justify-content-center">
<div class="col-6">
{{ form.Different_Beams|as_crispy_field }}
</div>
</div>
<div class="row justify-content-center">
<div id="hidden1">
<div class="col-6">
{{ form.Hours|as_crispy_field }}
{{ form.Ion_Species|as_crispy_field }}
{{ form.Energy|as_crispy_field }}
{{ form.Flux|as_crispy_field }}
</div>
</div>
<div id="hidden2">
<div class="col-6">
{{ form.Hours|as_crispy_field }}
{{ form.Ion_Species|as_crispy_field }}
{{ form.Energy|as_crispy_field }}
{{ form.Flux|as_crispy_field }}
</div>
</div>
<div id="hidden3">
<div class="col-6">
{{ form.Hours|as_crispy_field }}
{{ form.Ion_Species|as_crispy_field }}
{{ form.Energy|as_crispy_field }}
{{ form.Flux|as_crispy_field }}
</div>
</div>
<div id="hidden4">
<div class="col-6">
{{ form.Hours|as_crispy_field }}
{{ form.Ion_Species|as_crispy_field }}
{{ form.Energy|as_crispy_field }}
{{ form.Flux|as_crispy_field }}
</div>
</div>
</div>
<div class="row justify-content-center">
{{ form.Requiered_Equipment|as_crispy_field }}
{{ form.Special_Requirements|as_crispy_field }}
{{ form.Special_Safety_Procedures|as_crispy_field }}
{{ form.Lab_Support_Requirements|as_crispy_field }}
{{ form.Funded|as_crispy_field }}
{{ form.Summary|as_crispy_field }}
</div>
<div class="row justify-content-center">
<button type="submit" class="btn btn-success">Save New Request</button>
</div>
</form>
<br>
<script type="text/javascript">
$('#hidden2').css({
'display': 'none'
});
$('#hidden3').css({
'display': 'none'
});
$('#hidden4').css({
'display': 'none'
});
$('#id_Different_Beams').on('change', function() {
if (this.value === '2') {
$('#hidden1').show();
$('#hidden2').show();
$('#hidden3').hide();
$('#hidden4').hide();
}
else if ($(this).val() == '3') {
$('#hidden1').show();
$('#hidden2').show();
$('#hidden3').show();
$('#hidden4').hide();
}
else if ($(this).val() == '4') {
$('#hidden1').show();
$('#hidden2').show();
$('#hidden3').show();
$('#hidden4').show();
}
else {
$('#hidden1').show();
$('#hidden2').hide();
$('#hidden3').hide();
$('#hidden4').hide();
}
});
</script>
{% endblock %}
Views.py
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from .models import CreateBeamRequestModel, IonSpecies, Energys
from .forms import CreateBeamRequestForm
# Create your views here.
#home page
def beam_request_home_page(request):
page_title = 'Beam Request'
template_name = 'request/home.html'
context = {"title": page_title}
return render(request, template_name, context)
##login_required
#Create view
#staff_member_required
def beam_request_create_page(request):
page_title = 'Create new Request'
template_name = 'request/create.html'
form = CreateBeamRequestForm(request.POST or None)
if form.is_valid():
print(form.cleaned_data)
form.save()
form = CreateBeamRequestForm()
context = {"title": page_title, "form": form}
return render(request, template_name, context)

Displaying ManyToManyField in django template

I've been trying to display the exact values of model with ManyToMany relation but I just couldn't, all what I've achevied is receiving QuerySet
<QuerySet [<Stack: PHP>, <Stack: js>]>
by adding to template tags
{{ brand.technologies.all }}
But I would like to receive and display 2 fields, name and icon. I've tried with some loops like
{% for brand in brands %}
{% for technologies in brand.technologies.all %} {{ technologies.name }} {{ technologies.icon }} {% endfor %}
{% endfor %}
but it doesn't give any results. There is not problem with syntax because page is displaying and looks like this
image
models.py
STACK = (
('PHP', 'PHP'),
('js', 'JavaScript'),
...
)
STACK_ICONS = (
('/static/icons/stack/php.png', 'PHP'),
('/static/icons/stack/javascript.png', 'JavaScript'),
...
)
class Company(models.Model):
name = models.CharField(max_length=100, blank=False)
students = models.CharField(max_length=3, choices=STUDENTS)
type = models.CharField(max_length=15, choices=TYPES)
workers = models.PositiveIntegerField(validators=[MinValueValidator(1)])
city = models.CharField(max_length=15,choices=CITIES)
company_about = models.TextField(max_length=500, blank=False)
slug = models.SlugField(unique=True)
icon = models.ImageField(blank=True, null=True)
image = models.ImageField(blank=True, null=True)
logo = models.ImageField(blank=True, null=True)
technologies = models.ManyToManyField('Stack')
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Company, self).save(*args, **kwargs)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.name
# object stack relation manytomany with Company
class Stack(models.Model):
name = models.CharField(max_length=30, choices=STACK)
icon = models.CharField(max_length=80, choices=STACK_ICONS)
def __str__(self):
return self.name
views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
...
def comp_list(request):
f = CompanyFilter(request.GET, queryset=Company.objects.all())
return render(request, 'company/comp_list.html', {'filter': f})
def companies(request):
company = get_object_or_404(Company)
return render(request, 'company/comp_list.html', {'company': company})
def home(request):
return render(request, 'company/home.html')
def brands(request, slug):
brand = get_object_or_404(Company, slug=slug)
return render(request, 'company/comp_view.html', {'brand': brand})
def stacks(request):
stack = get_object_or_404(Stack)
return render(request, 'company/comp_view.html', {'stack': stack})
comp_view.html
{% extends 'company/base.html' %}
{% block content %}
<div class="row company-details">
<div class="col col-md-2"></div>
<div class="col col-md-8">
<input class="btn btn-success" type="button" value="Go Back" onclick="history.back(-1)" />
<div class="col col-md-12 company-bg" style="background-image: url('{{ brand.image.url }}'); background-repeat: no-repeat, repeat;">
</div>
<div class="bottom-overlay"></div>
<div class="company-description">
<div class="heading">
<div class="title">About us</div>
<div class="social-media">
Facebook
</div>
</div>
<div class="company-about">
{{ brand.company_about }}
</div>
</div>
<div class="company-stats">
<div class="company-logo-container">
<img class="company-logo" src="{{ brand.logo.url }}">
</div>
<div class="company-attributes">
<div class="field-group">
<div class="field">Type</div>
<div class="value">{{ brand.type }}</div>
</div>
<div class="field-group">
<div class="field">Company size</div>
<div class="value">{{ brand.workers }}+</div>
</div>
<div class="field-group">
<div class="field">Headquarters</div>
<div class="value">{{ brand.city }}</div>
</div>
<div class="field-group">
<div class="field">Open for students</div>
<div class="value">{{ brand.students }}</div>
</div>
</div>
<div class="technologies-section">
<p class="tech-heading">Technologies</p>
<div class="technologies-wrapper">
{{ brand.technologies.all }}
{% for brand in brands %}
{% for technologies in brand.technologies.all %} {{ technologies.name }} {% endfor %}
{% endfor %}
</div>
</div>
</div>
<div class="col col-md-2"></div>
</div>
{% endblock %}
I don't understand why you've suddenly added an outer loop through brands. You don't have anything called brands, and you're successfully accessing all the other data via brand. Just continue to do so, and drop the outer loop.
<div class="technologies-wrapper">
{% for technologies in brand.technologies.all %} {{ technologies.name }} {% endfor %}
</div>

how to code view with a "submit" AND "delete" method

I have a simple app that takes entries/tasks and distibutes them to appropriate inbox's.
The model, template and view follows. What I want to do is modify the view so that I can
have a functional "delete" button in my form (alongside the "add/submit" button, like in an admin form.):
models.py
class InboxEntry(models.Model):
job_number = models.CharField(validators=[RegexValidator(regex='^\w{8}$', message='Please enter a valid job number', code='nomatch')], max_length=8, unique=False, blank=False, null=False)
cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
request = models.CharField(max_length=64, choices=PRINT_CHOICES)
date_in = models.DateField(("Date"), auto_now=True, auto_now_add=True)
date_due = models.DateTimeField(("Due"),auto_now=False, auto_now_add=False)
basecamp_link = models.URLField(validators=[RegexValidator(regex='^(http|https)://', message='url must begin with http or https', code='nomatch')], blank=True, null=False, max_length=300)
note = models.TextField(max_length=1000, unique=False, blank=True, null=True)
assigned_by = models.ForeignKey(UserProfile, blank=False, null=False)
box = models.CharField(max_length=64, choices=INBOX_CHOICES)
assigned_to = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
assigned_team = models.ManyToManyField(ProjectTeam, blank=True, null=True)
status = models.CharField(max_length=30, choices=STATUS_CHOICES, default="Awaiting Action")
accepted_by = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
completed_on = models.DateTimeField(("Completed"),auto_now=False, auto_now_add=False, blank=True, null=True)
def __unicode__ (self):
return u'%s %s' % (self.job_number, self.job_name)
class Admin:
pass
views.py
def edit_prdInboxEntry(request, id, userid):
if request.method == 'POST':
a=InboxEntry.objects.get(pk=id)
form = PrdInboxForm(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('/production-display/'+ userid +'/')
else:
a=InboxEntry.objects.get(pk=id)
form = PrdInboxForm(instance=a)
user = request.user
u = UserProfile.objects.get(pk=userid)
creativerecords = InboxEntry.objects.filter(box="Creative")
studiorecords = InboxEntry.objects.filter(box="Studio")
records = InboxEntry.objects.filter(assigned_to=u)
return render_to_response('production_InboxEntry.html', {'form': form, 'records': records, 'studiorecords': studiorecords, 'creativerecords': creativerecords, 'user': user}, context_instance=RequestContext(request))
template
<form action="." method="POST">{% csrf_token %}
<div style="display:table;">
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.assigned_by.errors }}
<label for="id_jassigned_by">Assigned by:</label>
{{ form.assigned_by }}
</div>
</div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> </div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.job_number.errors }}
<label for="id_job_number">job no:</label>
{{ form.job_number }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.cell_number.errors }}
<label for="id_cell_number">cell:</label>
{{ form.cell_number }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.job_name.errors }}
<label for="job_name">job name:</label>
{{ form.job_name }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.request.errors }}
<label for="request">request:</label>
{{ form.request }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.note.errors }}
<label for="id_note">note:</label>
{{ form.note }}
</div>
</div>
<div style="display:table-cell;">
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<label for="id_date_due">date_due:</label>
{{ form.date_due}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
</div> <!-- end css table-row -->
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.box.errors }}
<label for="id_box">inbox:</label>
{{ form.box }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.assigned_to.errors }}
<label for="assigned_to">assigned_to:</label>
{{ form.assigned_to }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.basecamp_link.errors }}
<label for="id_basecamp_link">basecamp:</label>
{{ form.basecamp_link }}
</div>
</div>
<div style="display:table-cell;">
<div class="accepted_by">
{{ form.accepted_by.errors }}
<label for="accepted_by">accepted_by:</label>
{{ form.accepted_by }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.status.errors }}
<label for="id_status">status:</label>
{{ form.status }}
</div>
</div>
<div style="display:table-cell;">
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker2'>
<label for="id_completed_on">completed:</label>
{{ form.completed_on}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker2').datetimepicker();
});
</script>
</div>
</div>
</div> <!-- end css table-row -->
<div style="display:table-row;">
<div style="display:table-cell;"> </div>
<div style="display:table-cell;">
<p><input id="inbox_submit_btn" type="submit" value="add" /></p>
</div>
<div style="display:table-cell;">
<p><input id="inbox_delete_btn" type="submit" value="delete" /></p>
</div>
</div>
</div> <!-- end css table -->
</form>
Can anyone help point me in the right direction. The docs seem to suggest that I need separate views and urls but I'm pretty sure this can be accomplished in a single view?
The docs seem to suggest that I need separate views and urls but I'm pretty sure this can be accomplished in a single view?
You could do this by passing in different POST parameters depending on which button was pressed, I guess. This is still quite a weird thing to want to do though. It's far neater and more maintainable to split them into different views.
You may be under a misapprehension when it comes to the views linking to templates. having two separate views (one for submissions, one for deletions) in no way affects your ability to display the two buttons that link to those view urls in your template.
If you're worried about code reuse (function-based views often share a lot of code, so this is a reasonable complaint) then I suggest looking into class based views
Edit:
It's just a case of linking to your second "delete" view with the button:
<div class="your-button-class" href="{% url "your-delete-view" pk=object.pk %}></div>

How to fix display data in manytomanyfield in my template in Django website

I am trying to display some data in my templates by passing it as context but i get 'add_students_by_manager' object is not iterable I noticed that the problem actually has to do with outputting the result on the template. how can i fix this please.
models.py
class add_courses(models.Model):
Course_Name = models.CharField(max_length=200, blank=True)
student = models.ManyToManyField(add_students_by_manager, blank=True)
def __str__(self):
return self.Course_Name
class add_students_by_manager(models.Model):
manager_ID = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE)
student_ID = models.CharField(max_length=200)
student_name = models.CharField(max_length=200)
def __str__(self):
return self.student_name
views.py
def assignment_page(request):
if request.method == "POST":
get_course_name = request.POST.get('get_course_name')
stu_course_all_stu = add_courses.objects.filter(Course_Name=add_courses_get)
for all_stu_details in stu_course_all_stu:
for stu_details in all_stu_details.student.all():
print(stu_details.student_ID)
# THIS PRINTS OUT ALL THE student_id
context3 = {"stu_course_all_stu": stu_course_all_stu, "stu_details": stu_details}
return render(request, 'assignment_page.html', context3)
else:
return redirect('/')
assignment_page.html
{% for m in stu_details %}
<div class="card mb-3 ml-5" style="max-width: 840px;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="/media/uploads/student_user.png" class="rounded profile-pic mt-2 ml-5 mb-2" alt="user" height="120" width="140">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">Student ID : {{m.student_ID}}</h5>
<h5 class="card-title">Student Name : {{m.student_name}}</h5>
<form action="{% url 'stu_id_details' %}" method="POST">{% csrf_token %}
<div class="form-group">
<input type="hidden" name="get_id_stu_details" value="{{m.student_ID}}" class="form-control" id="exampleInputPassword2">
</div>
<button type="submit" class="btn btn-outline-success btn-sm">See Student Details</button>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
because you are not rendering the queryset to template , you are only printing it in your views using a for loop.You can do it in this way
views.py
ls = []
for all_stu_details in stu_course_all_stu:
ls.append(all_stu_details)
context3 = {"stu_course_all_stu": stu_course_all_stu, "stu_details": ls}
return render(request, 'assignment_page.html', context3)
html
{% for m in stu_details.student.all %}
<h5 class="card-title">Student ID : {{m.student_ID}}</h5>
<h5 class="card-title">Student Name : {{m.student_name}}</h5>