M
M
maestro072017-04-05 08:12:24
Django
maestro07, 2017-04-05 08:12:24

How to add pnotify to django project?

messages.py

# -*- coding: utf-8 -*-
SUCCESS = u"Регистрация выполнена успешно. Вам на почту выслано сообщение. Пройдите по ссылке для активации"
EXIST_USER = u"Email с таким названием уже существует"
PASSWORD_ERROR = u"Длина пароля меньше 5 символов"
DIFFERENT_PASSWORD = u"Пароли не совпадают"

views.py
def registration(request):
    template = "main/registration.html"
    params = dict()
    if request.method == "POST":
        email = request.POST.get("email", "").lower()
        password = request.POST.get("password", "")
        password2 = request.POST.get("password2", "")

        try:
            validate_email(email)
        except Exception as error:
            return render(request, template, params)        

        try:
            len(password) >= 5
        except Exception as error:
            messages.add_message(request, messages.ERROR, PASSWORD_ERROR)
            return render(request, template, params)        
        
        if password != password2:
           messages.add_message(request, messages.ERROR, DIFFERENT_PASSWORD)

        if User.objects.filter(username=email, is_active=True).exists():
            messages.add_message(request, messages.ERROR, EXIST_USER)
            return render(request, template, params)

        #User.objects.get(username=email).delete()

        new_user = User.objects.create_user(username=email, password=password)
        activation_key = Activation.objects.generate(user=new_user)

        messages.add_message(request, messages.ERROR, SUCCESS)

        #url = request.scheme + "://" + reverse('main:activate', kwargs={"code":str(activation_key)})
        url = "http://127.0.0.1:8000/main/activate/?code=" + str(activation_key)
        print url
        send_mail('user activation in JustSolve.kz', render_to_string('emails/activation_email.txt', 
            context={'activation_url': url}), settings.EMAIL_HOST_USER, [email])
        # TODO: add celery
        # TODO: send verification email
    return render(request, template, params)

base.html
{% if messages %}
        <script>
        '{% for message in messages %}'
            $(document).ready(function() {
                new PNotify({
                    title: '',
                    text: "{{ message|safe }}.",
                    type: '{{ message.tags }}',
                    icon: false,
                    styling: 'bootstrap3',
                });
            });
        '{% endfor %}'
        </script>
    {% endif %}

How does it display a notification that comes from the server?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shamsudin Serderov, 2017-04-05
@Steein

And what is wrong with such a design?

<script>
{% for message in messages %}
$(document).ready(function() {
    $.pnotify({
        title: '{{ message.tags|upper }}',
        text: '{{ message }}.',
        type: '{{ message.tags }}',
        hide: false,
        styling: 'bootstrap',
        closer_hover: false,
        sticker_hover: false
    });
});
{% endfor %}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question