# display variable from form on html page

**URL:** <https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929>\
**Category:** Using Django\
**Created:** [October 23, 2020, 1:24pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929 "2020-10-23T13:24:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Py-InSky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/py-insky/32/2289_2.png) [@Py-InSky](https://forum.djangoproject.com/u/Py-InSky)\
**Post date:** [October 23, 2020, 1:24pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/1 "2020-10-23T13:24:34Z")

</div>

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.

```auto
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

```

```auto
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)

```

```auto
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})

```

```auto
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>

```

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [October 23, 2020, 3:53pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/2 "2020-10-23T15:53:04Z")

</div>

Hi!

I think the problem is **WAN\_IP** is assigned a value here but it’s not saved with the form. (It’s not defined in the model)

> [@Py-InSky](#):
>
> ```py
> if form.is_valid():
> WAN_IP = form.cleaned_data['WAN_SubnetMask']
> form.save()
> 
> ```

One way this could be done is by calculating the WAN\_IP like a derived attribute also called _calculated field_.

```auto
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)

   def WAN_IP(self):
        return self.WAN_SubnetMask + 2

```

Then in your template you can call it like this:  
`{{ display_config.WAN_IP }}`

---

<div class="post-metadata">

**Author:** ![Py-InSky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/py-insky/32/2289_2.png) [@Py-InSky](https://forum.djangoproject.com/u/Py-InSky)\
**Post date:** [October 23, 2020, 9:28pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/3 "2020-10-23T21:28:07Z")

</div>

Hi, and thanks for reply!

So I added this to models.py:

def WAN\_IP(self):  
return self.WAN\_SubnetMask + 2

and attempted to call it with {{ display\_config.WAN\_IP }} in my template but it still does not display.

I then tried defining WAN\_IP in my model as below but still no luck.

WAN\_IP = models.IntegerField(null=True)

Sorry if I missed something, I am new to Django and thanks once again.

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [October 23, 2020, 10:51pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/4 "2020-10-23T22:51:49Z")

</div>

If you do it this way take note `WAN_SubnetMask` must have a value.

> [@Py-InSky](#):
>
> ```py
> def WAN_IP(self):
> return self.WAN_SubnetMask + 2
> 
> ```

One thing I noted now is that you’re calling last withouth the `()`.  
I think what you want is to assign the last `CPEconfig` object to the `display_config` variable.

```diff
def config_page(request):
+ display_config = CPEconfig.objects.last()
- display_config = CPEconfig.objects.last

```

* * *

Have you updated the form? Run the migrations after adding the field?  
Check those steps which are frequently forgotten (at least to me 😅)

---

<div class="post-metadata">

**Author:** ![Py-InSky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/py-insky/32/2289_2.png) [@Py-InSky](https://forum.djangoproject.com/u/Py-InSky)\
**Post date:** [October 24, 2020, 10:41am UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/5 "2020-10-24T10:41:02Z")

</div>

I’ve not added **WAN IP** to forms as it’s supposed to be a variable that’s created based on what the user chooses for **WAN\_SubnetMask** rather than a field for the user to fill in directly.

When you mentioned a _calculated field_ earlier is that something that needs to be created in forms for **WAN IP** rather than a standard field?

The only change I made to forms was removing:

def clean(self):  
cleaned\_data = super().clean()  
WAN\_SubnetMask = cleaned\_data.get(‘WAN\_SubnetMask’)  
if (WAN\_SubnetMask) == ‘30’:  
WAN\_IP = WAN\_Subnet + 2

As clean was being done on views it seemed like this section was redundant. Other than that my forms.py is the same as it was to start with.

I have run migrations after adding WAN\_IP to models and am also calling CPEconfig.objects.last() with brackets now.

One other thing to note is that now that WAN\_IP is in models, I am not seeing an entry for it after the form has been submitted when I view my database entries in the admin section. Again I am assuming that this section in views is supposed to be creating an entry for WAN\_IP:

if form.is\_valid():  
WAN\_IP = form.cleaned\_data[‘WAN\_SubnetMask’]  
form.save()

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [October 24, 2020, 11:15pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/6 "2020-10-24T23:15:12Z")

</div>

I’m not sure I understand if you’re using a **model field** or a **calculated field**.

What I meant is that you can store **WAN\_IP** in the model or you can calculate it based on another value, **not both**.

> [@Py-InSky](#):
>
> When you mentioned a _calculated field_ earlier is that something that needs to be created in forms for **WAN IP** rather than a standard field?

Not really. This is just a [normal model method](https://docs.djangoproject.com/en/2.2/topics/db/models/#model-methods) that _behaves_ like a field.

> [@Py-InSky](#):
>
> One other thing to note is that now that WAN\_IP is in models, I am not seeing an entry for it after the form has been submitted when I view my database entries in the admin section.

Maybe you need to update the ModelAdmin to include that field?

---

<div class="post-metadata">

**Author:** ![Py-InSky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/py-insky/32/2289_2.png) [@Py-InSky](https://forum.djangoproject.com/u/Py-InSky)\
**Post date:** [October 26, 2020, 8:48pm UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/7 "2020-10-26T20:48:59Z")

</div>

Just wanted to let you l got this working and say thanks at the same time.

I’d done something stupid with the formatting of the functions and had them set to the left rather than indented. It didn’t produce an error so I didn’t realise at the time, once it was indented it started working.

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [October 27, 2020, 12:50am UTC](https://forum.djangoproject.com/t/display-variable-from-form-on-html-page/4929/8 "2020-10-27T00:50:31Z")

</div>

Good to know and nevermind, it happens to everyone. 😅
