How do I pass the value from the inside of a for loop ( jinja2 ) to a bootstrap modal to display it in side the modal body?
here is my views.py file:
if request.method == 'GET':
driver = get_network_driver(device.napalm_driver)
with driver(device.IP_address, device.username, device.password) as device_conn:
interfaces = device_conn.get_interfaces()
context = {
'device': device,
'interfaces': interfaces,
}
return render(request, 'network/interface_list.html', context)
Please note that the the device_conn.get_interfaces() method returns a nested dictionary.
Here is the html template:
{% extends 'base/base.html' %}
{% block title %}iNet Interface List{% endblock %}
{% block content %}
<div class="section">
{% include 'network/include/interface_list_header.html' %}
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success' %}<div class="alert alert-success" role="alert">
{{ message }}
</div>{% elif message.tags == 'error' %}<div class="alert alert-danger" role="alert">
{{ message }}
</div>{% endif %}
{% endfor %}
{% endif %}
<div class="card mx-auto shadow rounded">
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col" style="text-align:center">Interface Name</th>
<th scope="col" style="text-align:center">Admin status</th>
<th scope="col" style="text-align:center">Protocol</th>
<th scope="col" style="text-align:center">Description</th>
<th scope="col" style="text-align:center">MAC Address</th>
<th scope="col" style="text-align:center">Last Flapped</th>
<th scope="col" style="text-align:center">Turn On/Off Port</th>
<th scope="col" style="text-align:center">Edit Description</th>
</tr>
</thead>
<tbody>
{% for interface_name, interface in interfaces.items %}
<tr>
<form action="{% url 'get_interfaces' device.id %}" method="post" id="{{ interface_name }}">
{% csrf_token %}
<input type="hidden" value="{{ interface_name }}" name="interface_name" />
<input type="hidden" value="{{ interface.is_enabled|yesno:'False,True' }}" name="enable" />
</form>
<td style="text-align:center">{{ interface_name }}</td>
<td style="text-align:center">{% if interface.is_enabled %}<i class="fa-solid fa-check text-success"></i>{% else %}<i class="fa-solid fa-xmark text-danger"></i>{% endif %}</td>
<td style="text-align:center">{% if interface.is_up %}<i class="fa-solid fa-check text-success"></i>{% else %}<i class="fa-solid fa-xmark text-danger"></i>{% endif %}</td>
<td style="text-align:center">{{ interface.description }} </td>
<td style="text-align:center">{{ interface.mac_address }}</td>
<td style="text-align:center">{{ interface.last_flapped }}</td>
<td style="text-align:center"><button class="btn common-button btn-primary" type="submit" form="{{ interface_name }}" value="Submit">{% if interface.is_enabled %}Turn Off{% else %}Turn On{% endif %}</button></td>
<!-- Modal Trigger button -->
<td style="text-align:center"><i class="fa-regular fa-clipboard"></i></td>
<!-- Modal container -->
<div class="modal fade viewModal" id="editDesc{{ interface_name }}" tabindex="-1" role="dialog" aria-labelledby="editDescLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 style="font-size: 20px;" class="modal-title" id="editDescLabel">View Console Output</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<textarea>{{ interface.description }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}
I wish to show the {{interface.description}} of the selected interface in the textarea so that I can edit it later. Any help would be appreciated.
My issue is - I am able to fetch {{interface.description}} outside the bootstrap modal, however I am unable to fetch the same thin inside the bootstrap modal. Eventhough both of them are inside the for loop.
Working for the first time on HTML and Djnago. I wrote a custom HTML
excluded_leagues.html
<h1>Excluded Leagues</h1>
<div class="excluded_leagues">
<form action="/superprofile/save_excluded_leagues/" method="POST">{% csrf_token %}
<table id="excluded_leagues_list">
<thread>
<tr>
<th scope="col">
<div class="text">
<span>Excluded</span>
</div>
</th>
<th scope="col">
<div class="text">
<span>League ID</span>
</div>
</th>
<th scope="col">
<div class="text">
<span>League Name</span>
</div>
</th>
<th scope="col">
<div class="text">
<span>Sport</span>
</div>
</th>
</tr>
</thread>
<tbody>
{% for league in object_list %}
{% if league.status == 'Active' %}
<tr>
<td><input type="checkbox" value="{{ league.id }}" name="selected_league"></td>
<td>{{ league.id }}</td>
<td>{{ league.name }}</td>
<td>{{ league.sport }}</td>
</tr>
{% else %}
{% endif %}
{% endfor %}
</tbody>
</table>
<div>
<button type="submit" name="apply">Save changes</button>
</div>
</div>
<ul>
I am trying to grab the league.id of all the selected checkboxes and use them in my views
views.py
def save_excluded_leagues(request):
if 'apply' in request.POST:
league_id_selected = request.POST.getlist('selected_league')
info_log.info(f"{league_id_selected } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1")
return HttpResponse("Hello")
I need to grab the values league.id of all the selected checkboxes, but I am unable to grab the value in my views.py file.
Right now it is not returning anything for the value request.POST.getlist('selected_league')
This is solved. I wrapped the checkbox part in
<td>
<div>
<input type="checkbox" value="{{ league.id }}" name="selected_league" {% if user_profile.is_free_money %} checked{% endif %}>
</div>
</td>
<td>{{ league.id }}</td>
<td>{{ league.name }}</td>
<td>{{ league.sport }}</td>
let me quickly explain what am I trying to do.
So I am making a small Django based Conference Management, where there can be multiple conferences, a single conference can have multiple talks and each talk will have a certain number of Speakers and Participants.
I am able to list the conferences and give the option to delete and edit a conference.
Problem:
I want an option where I click on a conference and it shows a list of talks for that conference.
but when I click on the conference it shows the following error.
NoReverseMatch at /Big Data/talks/
Reverse for 'conferenceedit' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<id>[^/]+)/edit$']
Request Method: GET
Request URL: http://127.0.0.1:8000/Big%20Data/talks/
Django Version: 3.2.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'conferenceedit' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<id>[^/]+)/edit$']
Exception Location: E:\python\PYTHON and GIT\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: E:\python\PYTHON and GIT\python.exe
Python Version: 3.9.1
Python Path:
['D:\\DjangoConferenceManagementSystem\\mysite',
'E:\\python\\PYTHON and GIT\\python39.zip',
'E:\\python\\PYTHON and GIT\\DLLs',
'E:\\python\\PYTHON and GIT\\lib',
'E:\\python\\PYTHON and GIT',
'C:\\Users\\AKS\\AppData\\Roaming\\Python\\Python39\\site-packages',
'E:\\python\\PYTHON and GIT\\lib\\site-packages',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\pip-21.0.1-py3.9.egg',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\win32',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\win32\\lib',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\Pythonwin']
Server time: Sun, 03 Oct 2021 15:47:10 +0000
I have included the following code to support the argument.
MODELS.PY
from django.db import models
from django.db.models.fields import CharField
from django.utils import timezone
# Create your models here.
class ConferenceModel(models.Model):
conference_title = models.TextField(null=False,primary_key=True)
conference_description = models.CharField(max_length=1000,null=False)
conference_start_date = models.DateField(null=False)
conference_end_date = models.DateField(null=False)
class TalkModel(models.Model):
talk_conference_title = models.ForeignKey(ConferenceModel,on_delete=models.CASCADE)
talk_title = models.TextField(null=False,primary_key=True)
talk_description = models.CharField(max_length=100,null=False)
talk_duration = models.DecimalField(max_digits=2,decimal_places=1)
talk_time = models.DateTimeField(null=False)
class SpeakerModal(models.Model):
speaker_talk_title = models.ForeignKey(TalkModel,on_delete=models.CASCADE)
speaker_username = models.CharField(max_length=25,null=False)
speaker_email = models.EmailField(max_length=100,primary_key=True,null=False)
class ParticipantModel(models.Model):
participant_talk_title = models.ForeignKey(TalkModel,on_delete=models.CASCADE)
participant_username = models.CharField(max_length=25,null=False)
participant_email = models.EmailField(max_length=100,primary_key=True,null=False)
URLS.PY
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.conferenceView,name='conference'),
path('delete/<str:id>',views.conferencedelete,name='conferencedelete'),
path('<str:id>/edit',views.conferenceedit,name='conferenceedit'),
path('<str:id>/talks/',views.talkView,name='talks'),
path('admin/', admin.site.urls),
]
VIEWS.PY
from django.shortcuts import render
from django import forms
from django.shortcuts import render,HttpResponsePermanentRedirect,HttpResponse
from myapp.forms import ConferenceForm,TalkForm,SpeakerForm,ParticipantForm
from myapp.models import ConferenceModel,TalkModel,SpeakerModal,ParticipantModel
# Create your views here.
def conferenceView(request):
if request.method == 'POST':
conferenceform = ConferenceForm(request.POST)
if conferenceform.is_valid():
conferenceform.save()
conferenceform = ConferenceForm()
else:
conferenceform = ConferenceForm()
conference = ConferenceModel.objects.all()
return render(request,'myapp/conference.html',{'conference':conference,'conferenceform':conferenceform})
def conferenceedit(request,id):
if request.method == 'POST':
uniqueconferencetitle = ConferenceModel.objects.get(pk=id)
requestconferencedetails = ConferenceForm(request.POST,instance=uniqueconferencetitle)
if requestconferencedetails.is_valid():
requestconferencedetails.save()
return HttpResponsePermanentRedirect('/')
else:
uniqueconferencetitle = ConferenceModel.objects.get(pk=id)
requestconferencedetails = ConferenceForm(instance=uniqueconferencetitle)
return render(request,'myapp/conferenceedit.html',{'requestconferencedetails':requestconferencedetails})
def conferencedelete(request,id):
if request.method == 'POST':
conferencedelete = ConferenceModel.objects.get(pk=id)
conferencedelete.delete()
return HttpResponsePermanentRedirect('/')
def talkView(request,id):
talk = ConferenceModel.objects.get(pk=id)
conferencetalks = talk.talkmodel_set.all()
return render(request,'myapp/talks.html',{'conferencetalks':conferencetalks})
FORMS.PY
from django import forms
from django.db import models
from django.db.models import fields
from django.forms import widgets
from myapp.models import ConferenceModel,TalkModel,SpeakerModal,ParticipantModel
class ConferenceForm(forms.ModelForm):
class Meta:
model = ConferenceModel
fields = ['conference_title','conference_description','conference_start_date','conference_end_date']
widgets = {
'conference_title' : forms.TextInput(attrs={'class':'form-control '}),
'conference_description' : forms.TextInput(attrs={'class':'form-control'}),
'conference_start_date' : forms.TextInput(attrs={'class':'form-control','placeholder':'YYYY-MM-DD'}),
'conference_end_date' : forms.TextInput(attrs={'class':'form-control','placeholder':'YYYY-MM-DD'})
}
class TalkForm(forms.ModelForm):
class Meta:
model = TalkModel
fields = ['talk_conference_title','talk_title','talk_description','talk_duration','talk_time']
widgets = {
'talk_conference_title' : forms.HiddenInput,
'talk_title' : forms.TextInput(attrs={'class':'form-control '}),
'talk_description' : forms.TextInput(attrs={'class':'form-control '}),
'talk_duration' : forms.TextInput(attrs={'class':'form-control '}),
'talk_time' : forms.TextInput(attrs={'class':'form-control '}),
}
class SpeakerForm(forms.ModelForm):
class Meta:
model = SpeakerModal
fields = ['speaker_talk_title','speaker_username','speaker_email']
widgets = {
'speaker_talk_title' : forms.HiddenInput,
'speaker_username' : forms.TextInput(attrs={'class':'form-control '}),
'speaker_email' : forms.TextInput(attrs={'class':'form-control '})
}
class ParticipantForm(forms.ModelForm):
class Meta:
model = ParticipantModel
fields = ['participant_talk_title' , 'participant_username' , 'participant_email']
widgets = {
'participant_talk_title' : forms.HiddenInput,
'participant_username' : forms.TextInput(attrs={'class':'form-control '}),
'participant_email' : forms.TextInput(attrs={'class':'form-control '})
}
CONFERENCE.HTML
{% extends 'myapp/base.html' %}
{% block content %}
<div class="col-sm-5 container">
<h4 class="alert alert-info">Add a new conference</h4>
<form class="form-control" method="POST">
{% csrf_token %}
{{ conferenceform }}
<p></p>
<input type="submit" class="btn btn-success " value="ADD" id="add">
</form>
</div>
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of confrences
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Options</th>
</tr>
</th>
{% for conferences in conference %}
<tr>
<th scope="col">{{conferences.conference_title}}</th>
<th scope="col">{{conferences.conference_description}}</th>
<th scope="col">{{conferences.conference_start_date}}</th>
<th scope="col">{{conferences.conference_end_date}}</th>
<td>
Edit
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
TALKS.HTML
{% extends 'myapp/base.html' %}
{% block content %}
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of Talks under this conference
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Talk Title</th>
<th scope="col">Talk Description</th>
<th scope="col">Talk Duration </th>
<th scope="col">Talk Time</th>
<th scope="col">Options</th>
</tr>
</th>
{% for talkss in conferencetalks %}
<tr>
<th scope="col">{{talkss.talk_title}}</th>
<th scope="col">{{talkss.talk_description}}</th>
<th scope="col">{{talkss.talk_duration}}</th>
<th scope="col">{{talkss.talk_time}}</th>
<!-- <td>
Edit
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
You loop through your queryset by naming the item conferences.
So to access the Id, since the URL expects an id. Look this path('<str:id>/edit',views.conferenceedit,name='conferenceedit'),.
You need to do this:
{% url 'conferenceedit' conferences.id %}
Or
{% url 'conferenceedit' conferences.pk %}
PS. Improve your path by updating '<str:id> to '<int:id>, since your model is integer.
urls.py
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.conferenceView,name='conference'),
path('delete/<int:id>',views.conferencedelete,name='conferencedelete'),
path('<int:id>/edit',views.conferenceedit,name='conferenceedit'),
path('<int:id>/talks/',views.talkView,name='talks'),
path('admin/', admin.site.urls),
]
talks.html
{% extends 'myapp/base.html' %}
{% block content %}
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of Talks under this conference
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Talk Title</th>
<th scope="col">Talk Description</th>
<th scope="col">Talk Duration </th>
<th scope="col">Talk Time</th>
<th scope="col">Options</th>
</tr>
</th>
{% for talkss in conferencetalks %}
<tr>
<th scope="col">{{talkss.talk_title}}</th>
<th scope="col">{{talkss.talk_description}}</th>
<th scope="col">{{talkss.talk_duration}}</th>
<th scope="col">{{talkss.talk_time}}</th>
<!-- <td>
Edit
<form action="{% url 'conferencedelete' talkss.talk_conference_title_id %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
conference.html
{% extends 'myapp/base.html' %}
{% block content %}
<div class="col-sm-5 container">
<h4 class="alert alert-info">Add a new conference</h4>
<form class="form-control" method="POST">
{% csrf_token %}
{{ conferenceform }}
<p></p>
<input type="submit" class="btn btn-success " value="ADD" id="add">
</form>
</div>
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of confrences
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Options</th>
</tr>
</th>
{% for conferences in conference %}
<tr>
<th scope="col">{{conferences.conference_title}}</th>
<th scope="col">{{conferences.conference_description}}</th>
<th scope="col">{{conferences.conference_start_date}}</th>
<th scope="col">{{conferences.conference_end_date}}</th>
<td>
Edit
<form action="{% url 'conferencedelete' conferences.pk %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
I got the problem was with my TALKS.HTML , I duplicated this HTML from the CONFERENCE.HTML and missed removing the following code.
<!-- <td>
Edit
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
though I commented it , but it seems it wasn't commented , I removed it and now it works
here in this project, i want to show the active bar when admin is logged in and show blue color and when any other user is logged in, i want to show the active bar on it and others on red color when the users are not active.
This is my html
<div class="card-body table-responsive p-0">
<table class="table table-hover text-nowrap" id="rooms">
<thead>
<tr>
<th>SN</th>
<th>Users</th>
<th>Email</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for user in object_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{user.username }}</td>
<td>{{user.email}}</td>
<td>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>
<span ><a class="btn btn-info mr-2" href="{% url 'dashboard:passwordreset' %}"
><i
class="fa fa-edit m-1"
aria-hidden="true"
></i>Reset Password</a
></span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
Here I want to show green when admin is logged in and other active when other user is logged in
I do not know what do you mean : active or inactive user. But there is authenticated user, or anonymous user in django. So you can do this:
{% if user.isauthenticated %} your css red or blue {% endif %}
This is the code for the page that is being viewed in those images, is there any changes that need to be made to make it more responsive in a mobile chrome browser?
<!---To Display Timetable Entries----->
<div class="container"><br>
<span style="font-family:'Futura';font-weight:bold;"><h2>TimetableMatch</h2></span>
<hr>
<div style="overflow-x:auto;">
<table class="table table-dark">
<thead>
<tr>
<th>Module</th>
<th>Day</th>
<th>Time</th>
<th>Area</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entry in entrys %}
<tr>
<td>{{ entry.module }}</td>
<td>{{ entry.day }}</td>
<td>{{ entry.time }}</td>
<td>{{ entry.location }}</td>
<td><a class="btn btn-info" href="../edit/{{ entry.id }}">Edit</a>
<br>
<br>
<a class="btn btn-danger" href="../delete/{{ entry.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-4 col-lg-2">
<a class="btn btn-primary" href="../add_entry/">Add Timetable Entry</a>
<br>
<br>
<a href="{% url 'homepage' %}">
<button type="button" class="btn btn-primary btn-xs"> Back to home</button></a>
<a href="{% url 'index' %}">
<br>
<br>
<button type="button" class="btn btn-primary">Logout</button> </a>
</div>
</div>
</body>
IOS chrome browser
Laptop chrome browser