D
D
Daniil Kotlyar2021-07-31 20:18:15
Django
Daniil Kotlyar, 2021-07-31 20:18:15

How to define a button for each form?

Good evening. I have created a user, and I am creating a page on which a logged in user could change his password, login or mail. There are 3 of these fields, each of them has its own button. If I include all 3 fields in the form, then for some reason I get invalid. Tried to make 3 forms for each field. Connected it all to the view, it seems to be working. But! out of 3 only one form works - I can change the username. It is changed and saved, there are no fields near other buttons. What to do? here is a link to pastebin: https://pastebin.com/RqGGe22W

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2021-07-31
@Daibend

In my opinion, you have a problem already in the very approach to the implementation of the task.
I would advise for each element (login/password...) to create separate forms. For example, to change the password, you can create a classic scheme:
- Old password;
- New;
- Confirmation;
Forms should also be used for mail/login.
All of these forms can be handled in the same view using prefixes

Example

if request.method == 'POST':
    if 'bannedphrase' in request.POST: # Имя кнопки
        bannedphraseform = BannedPhraseForm(request.POST, prefix='banned')
        if bannedphraseform.is_valid():
            bannedphraseform.save()
        expectedphraseform = ExpectedPhraseForm(prefix='expected')
    elif 'expectedphrase' in request.POST:
        expectedphraseform = ExpectedPhraseForm(request.POST, prefix='expected')
        if expectedphraseform.is_valid():
            expectedphraseform.save() 
        bannedphraseform = BannedPhraseForm(prefix='banned')
else:
    bannedphraseform = BannedPhraseForm(prefix='banned')
    expectedphraseform = ExpectedPhraseForm(prefix='expected')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question