How to get data from forms? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: How to get data from forms? (/thread-17210.html) |
How to get data from forms? - dmytruk - Apr-02-2019 How can I get data from a form (ProductCreateForm)? If I write form = self.get_form(), then I just get a form template, where some data is selected, and some are not (select especially). If I write form = ProductCreateForm(request.POST), then I get an error saying that the request was not found. Perhaps this is due to the fact that I set the request in get_context_data() and work with them in the __init__ method in the forms.py. I process the data in the clean method in the forms.py. class ProductsCreate(CreateView): model = Product form_class = ProductCreateForm http_method_names = ['get', 'post'] def get_context_data(self, *args, **kwargs): ctx=super(ProductsCreate, self).get_context_data(*args, **kwargs) ctx['special_form'] = SpeciallyPriceForm() return ctx def get(self, request, *args, **kwargs): self.object = None if kwargs.get('slug'): category = Category.objects.filter(slug=kwargs.get('slug')).first() self.initial.update({'category': category}) return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): self.object = None form = self.get_form() #Тут что-то не то, видимо... special_form = SpeciallyPriceForm(self.request.POST) if form.is_valid() and special_form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form) class ProductCreateForm(forms.ModelForm): #.... def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') #... user = self.request.user provider = Provider.objects.filter(user=user.id).last() self.fields['category'] = ModelMultipleChoiceField(queryset=provider.category.all()) #... def clean(self): cleaned_data = super(ProductCreateForm, self).clean() cd_category = cleaned_data.get('category') #... class SpeciallyPriceForm(forms.ModelForm): class Meta: model = SpeciallyPrice fields = ['adittional_specially_price', 'adittional_specially_number'] RE: How to get data from forms? - dmytruk - Apr-02-2019 There are 2 forms on one page. There are 2 models: 1. Product. 2. SpeciallyPrice. SpeciallyPrice is linked via FK to Product. At the same time, SpeciallyPrice is Inline model in Product. The fields of the SpecialPriceForm are automatically created using JS. That is, them may be the n-th number. It is necessary to create a record for each created field. In principle, I guess how to do it - use the cycle to drive the values obtained. But the problem is that for some reason None comes from the form. And really come the last two empty (None's) created with JS fields. How to solve this issue? Maybe Ajax should be connected and it somehow process the request? Or is there a more Django's option? Please help. I added to views def get_initial(self): initial = super(ProductsCreate, self).get_initial() initial['request'] = self.request return initialChanged post and form_valid functions def post(self, request, *args, **kwargs): self.object = None form = ProductCreateForm(request.POST, request.FILES, initial={'request': request}) special_form = SpeciallyPriceForm(request.POST) print(special_form) #Template of form, wit two last empty fields. Code at bottom. if form.is_valid() and special_form.is_valid(): return self.form_valid(form, special_form) else: return self.form_invalid(form, special_form) def form_valid(self, form, special_form): product = form.save(commit=False) product.user = self.request.user product.save() special = special_form.save(commit=False) #Here I think, depending on the number of list items, to cycle through it and create the corresponding number of `special` records associated with this` product`. Is the logic correct? special.product = product special.save() for spec_price in special_form.cleaned_data.get('adittional_specially_price'): print(spec_price) special.adittional_specially_price = spec_price for spec_numb in special_form.cleaned_data.get('adittional_specially_number'): print(spec_numb) special.adittional_specially_number = spec_numb class SpeciallyPriceForm(forms.ModelForm): class Meta: model = SpeciallyPrice fields = ['adittional_specially_price', 'adittional_specially_number'] def clean(self): cleaned_data = super(SpeciallyPriceForm, self).clean() cd_adittional_specially_price = cleaned_data.get('adittional_specially_price') print(cd_adittional_specially_price) #None cd_adittional_specially_number = cleaned_data.get('adittional_specially_number') print(cd_adittional_specially_number) #None <html><body> Special price from {{ special_form.adittional_specially_price }} kg {{ special_form.adittional_specially_number }} usd <script> (function(){ var copy = document.querySelector('.field.inline.specially').cloneNode(true); document.querySelector('html').addEventListener('input', function(e){ if(e.target.classList.contains('event') && e.target.tagName == 'INPUT'){ var error = 0; for(var evt of document.querySelectorAll('.field.inline.specially input.event')){ evt.value = evt.value.replace(/[^\d]/,''); if(!evt.value || +evt.value < 1) error++; } if(!error){ var last = document.querySelectorAll('.field.inline.specially'); last[last.length-1].insertAdjacentHTML('afterEnd', copy.outerHTML); } } }); })(); </script> </body></html> |