K
K
Karina2014-05-18 16:25:31
Django
Karina, 2014-05-18 16:25:31

How to check for the identity of two fields without a POST request? Django

In short, after a thousand years, I again return to this issue. I haven't been able to solve it yet.
To do it the way I want - you need to fasten AJAX.
I don't want to do it and I don't know how.
What I want to do right now: so that this check is not carried out instantly, but when the button is pressed, which activates the POST request.
What I have: I have overridden the clean_filename methods:

def clean_password(self):
        value_password = self.cleaned_data.get('password')
        if len(str(self.cleaned_data['password'])) < 6:
            raise forms.ValidationError({'password': ["Password must been more then 6 symbols"]})
        elif len(str(self.cleaned_data['password'])) > 45:
            raise forms.ValidationError({'password': ["Password must been less 45 symbols"]})
        elif value_password is None:
            raise forms.ValidationError({'password': ["Field of password can't are blank"]})
        return value_password

I similarly redefined the clean_password2 method.
I check the method for identity:
def clean(self):
        super(Form_registration, self).clean()
        data = self.cleaned_data
        if data["password"] != data["password2"]:
            raise ValidationError({'password2': ["Password must been are equals"]})
        return data

Here's what's in templates:
{{ form_registration.non_field_errors }}
#всякая ерундистика
<div class="fieldWrapper">
        {{ form_registration.password }}
        <label for="id_password"></label>
      {{ form_registration.password.errors }}
    </div>
    <div class="fieldWrapper">
        {{ form_registration.password2 }}
        <label for="id_password2"></label>
        {{ form_registration.password2.errors }}
    </div>

ValidationError, if it happens, it is written right at the top of the page :-S. Though in a template - at first a field, and then errors. - How to fix it?
Also, if the field is empty, but it should not be, an error pops up - Required field. I need to rewrite this text urgently. It's from the errorlist, by default it looks like this:
class DivErrorList(ErrorList):
    def __str__(self):              # __unicode__ on Python 2
            return self.as_divs()
    def as_divs(self):
        if not self:
            return ''
        return '<div class="errorlist">%s</div>' % ''.join(['<div class="error">%s</div>' % e for e in self])

I have no idea how to rewrite here.
But the main problem: I enter some data into my fields, I click on the button. my url changes to: site_address/saves/. I again enter some data (let's say correct), click on the button - site_address / saves - The data is not entered into the database. I don't really know what to do. It is necessary to reload the page somehow (although this does not help either). - In general, if something is saved, then only on the first try. Maybe, of course, I'm doing something wrong when saving:
def saves_data_user_on_registration (request):
    c = {}
    form_registration = Form_registration(request.POST, request.FILES, error_class=DivErrorList)
    c['form_registration'] = form_registration
    if request.method == 'POST':
        c.update(csrf(request))
        if form_registration.is_valid():
            form_registration.save()
            return render_to_response('see_you_later.html', c,
                                      context_instance=RequestContext(request))
        else:
            print form_registration
            return render_to_response('first_page.html', c,
                                      context_instance=RequestContext(request))

Thanks everyone for your help :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Solovyov, 2014-05-18
@pavel_salauyou

use this lib https://github.com/agiliq/Django-parsley here and there is validation on the client and on the server.
Errors need to be sent to the template and output there and not through
print form_registration.errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question