Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get data from forms?
#1
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']
Reply


Messages In This Thread
How to get data from forms? - by dmytruk - Apr-02-2019, 11:34 AM
RE: How to get data from forms? - by dmytruk - Apr-02-2019, 10:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am trying to space the registration forms using css. But it is not working. tree 0 1,410 Apr-11-2021, 03:31 AM
Last Post: tree
  How to fill text fields web forms Martinelli 1 2,047 Sep-25-2019, 11:14 PM
Last Post: Martinelli
  Forms, python, passlib and other questions marienbad 1 2,458 Feb-05-2019, 04:23 AM
Last Post: snippsat
  Forms to render "Guide in steps" or not? stablepeak 1 2,264 Nov-29-2018, 10:05 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020