0
0
0ldn0mad2020-11-01 14:36:21
Django
0ldn0mad, 2020-11-01 14:36:21

How to process data from a Django form?

I'm doing my own project - I'm learning Django at the same time.
The purpose of the project: To make a convenient mechanism for cutting IP networks under the necessary masks. Keep records (card index) of the use of sliced ​​subnets.
As initial data, I have three models:
1) A list of subnets that will need to be divided into small ones
5f9dcc4a7ea36810140941.png
2) A list of masks that will need to be used to cut one network from the model above
5f9dcccc28850823933533.png
3) A table of ready-made subnets that were formed after cutting the network from point 1 with a mask subnet selected from point 2. Further, these subnets will be distributed and taken into account.
5f9dce86ef4d2012362471.png
As the subnets from point 3 run out, they will again be increased by cutting networks from point 1 with the selected mask from point 2.
I'm making a form that specifies the network that will be cut with a drop-down list of suggested masks.

from django import forms
from mask.models import Mask


class SawnetForm(forms.Form):
    ip_lan_24 = forms.CharField(
        max_length=50,
        label='Введите подсеть класса C, которую будем делить ',
        widget=forms.TextInput(attrs={
            "class": "form-control",
            "placeholder": "AAA.BBB.CCC.DDD",
        })
    )
    mask = forms.ModelChoiceField(
        queryset=Mask.objects.all(),
        label='Маска ',
        empty_label='Выберите маску',
        widget=forms.Select(attrs={
            "class": "form-control",
        })
    )


But in the view I don’t know how to describe the action

from django.shortcuts import render
from .forms import SawnetForm
# Create your views here.


def sawnet(request):
    if request.method == 'POST':
        pass
    else:
        form = SawnetForm()


According to the idea, I need to somehow observe the relationship - if, for example, we chose a subnet with the mask "ABCD / 27 - 255.255.255.224 - 30 hosts", then in the same line I need to somehow put the value from the same line into the variable - /27
Then apply the ipaddress method, and so on.

Now I'm stuck on how, when selecting some data from the drop-down list in the form, take the data from another field of the selected row into a variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2020-11-02
@buslay

In view, you can use a special class to work with the form - CreateView.
In this view, you can override the logic of the form_valid method, create a ready-made subnets table object. In this method, the saved and validated form data will be available.
Instead of overriding the form_valid method, you can use the save method on the SawnetForm, like this:

def save(self, commit=True):
    instance = super(SawnetForm, self).save(commit=False)
    # тут логика получения данных для сохранения в БД
    if commit:
        ReadySubnets.objects.update_or_create(
            ip_net=self.cleaned_data['ip_lan_24'],
            mask=self.cleaned_data['mask'],
        )
    return instance

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question