0

views.py

class EventUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Event
    form_class = EventUpdateForm
    second_form_class= EventSingleAttendeeForm
    template_name = 'events/event_update.html'

    def form_valid(self, form):
        form.instance.creator = self.request.user
        return super().form_valid(form)

    def test_func(self):
        event = self.get_object()
        if self.request.user == event.creator:
            return True
        return False

    def get_context_data(self, *args, **kwargs):
        context = super(EventUpdateView, self).get_context_data(*args, **kwargs)
        context['form2'] = self.second_form_class
        return context

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        form2 = self.second_form_class(request.POST)

        if form.is_valid() and form2.is_valid():
            event_data = form.save(commit=False)
            # attendee_name = form2.attendee_name
            if form2.attendee_name != '' and form2.attendee_email != '' and form2.attendee_mobile:
                attendee = Attendee(name=form2.attendee_name, email=form2.attendee_email, mobile=form2.attendee_mobile)
                existing_attendee = Attendee.objects.filter(Q(email__exact=form2.attendee_email) | Q(mobile__exact=form2.attendee_mobile))
                if existing_attendee:
                    attendee = existing_attendee.first()
                else:
                    attendee.save()
                form.attendees.add(attendee)
            form.save()
            return redirect('some')

forms.py

class EventSingleAttendeeForm(forms.Form):
    attendee_name = forms.CharField(max_length=50, required=False)
    attendee_mobile = forms.IntegerField(required=False)
    attendee_email = forms.EmailField(required=False)



builtins.AttributeError
AttributeError: type object 'EventSingleAttendeeForm' has no attribute 'attendee_name'

I am getting this error I want to access the data in those fields but I am not able to. A little help will be appreciated.

2 Answers 2

1

After calling form.is_valid(), you can access the form's data from its cleaned_data.

if form2.cleaned_data['attendee_name'] != '' and form2.cleaned_data['attendee_email' != '' and form2.cleaned_data['attendee_mobile']:

Or, if it's a model form, you can use the instance that is returned by form.save(), or use form.instance.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you passing the fields to the Meta class? like this:

class EventSingleAttendeeForm(forms.Form):
    attendee_name = forms.CharField(max_length=50, required=False)
    attendee_mobile = forms.IntegerField(required=False)
    attendee_email = forms.EmailField(required=False)

    class Meta:
        fields = (
            'attendee_name',
            'attendee_mobile',
            'attendee_email'
        )

1 Comment

TypeError: quote_from_bytes() expected bytes in super(EventUpdateView, self).post(request, *args, **kwargs) any help will be appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.