Answer the question
In order to leave comments, you need to log in
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
2) A list of masks that will need to be used to cut one network from the model above
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.
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",
})
)
from django.shortcuts import render
from .forms import SawnetForm
# Create your views here.
def sawnet(request):
if request.method == 'POST':
pass
else:
form = SawnetForm()
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question