I am attempting to create a router config generator using django forms. The idea is that the form takes input / choices and these are then displayed on a page called config_page.html.
Where I am getting stuck is being able to create a new variable based on input and displaying this in the config_page.html.
So for example lets say from the choices for ‘WAN_SubnetMask’ ‘30’ is selected and some numbers are entered for ‘WAN_Subnet’, I want to take the integers entered for ‘WAN_Subnet’, perform some very basic math e.g. add 2 and then create a new variable called ‘WAN_IP’.
I can display objects from my database in my config_page.html however the variable ‘WAN_IP’ does not display at all.
Here is what I have come up with so far, I have tried adding the logic to create the ‘WAN_IP’ variable in both forms and views.
forms.py
from django import forms
from .models import CPEconfig
Router = [
('2901', 'Cisco 2901'),
('1941', 'Cisco 1941'),
WAN_SubnetMask = [
('30', '30'),
('29', '29'),
]
LAN_SubnetMask = [
('30', '30'),
('29', '29'),
]
class ConfigForm(forms.ModelForm):
Router = forms.CharField(label='Select Router', widget=forms.Select(choices=Router))
WAN_Subnet = forms.GenericIPAddressField()
WAN_SubnetMask = forms.CharField(label='WAN Subnet Mask', widget=forms.Select(choices=WAN_SubnetMask))
LAN_SubnetMask = forms.CharField(label='LAN Subnet Mask', widget=forms.Select(choices=LAN_SubnetMask))
class Meta:
model = CPEconfig
fields = ('WAN_Subnet', 'WAN_SubnetMask', 'LAN_Subnet', 'LAN_SubnetMask', 'Router')
def clean(self):
cleaned_data = super().clean()
WAN_SubnetMask = cleaned_data.get('WAN_SubnetMask')
if (WAN_SubnetMask) == '30':
WAN_IP = WAN_Subnet + 2
models.py
from django.db import models
# Create your models here.
class CPEconfig(models.Model):
WAN_Subnet = models.CharField(max_length=50, null=True)
LAN_Subnet = models.CharField(max_length=50, null=True)
WAN_SubnetMask = models.IntegerField(null=True)
LAN_SubnetMask = models.IntegerField(null=True)
Router = models.CharField(max_length=50, null=True)
def __str__(self):
return str(self.Router)
views.py
from django.shortcuts import render
from django.http import HttpResponse
from CPEconfigs_app.models import CPEconfig
from .forms import ConfigForm
# Create your views here.
def CPEgen(request):
if request.method == 'POST':
form = ConfigForm(request.POST)
if form.is_valid():
WAN_IP = form.cleaned_data['WAN_SubnetMask']
form.save()
else:
form = DIAForm()
return render(request, 'CPEgen.html', {'DIAForm': form})
def config_page(request):
display_config = CPEconfig.objects.last
return render(request, 'config_page.html', {'display_config': display_config})
config_page.html
{% extends 'base.html' %}
{% block title %}
<title>Config_page</title>
{% endblock title %}
{% block content %}
<div class="container">
<h1>{{ display_config.SubnetMask }} {{ display_config.Router }} {{ WAN_IP }}</h1>
</div>
{% endblock content %}
<h2>{{ data }}</h2>