Only see the 'error_new' variable in my view
this variable has taken two values: "ok" or "error1" (true or false, error
or not error,..etc)

def NuevoPac(request):
# Manda al formulario todos los campos vacios
variable1 = 'Agregando nueva Ficha de Paciente'
variable2 = "modifica_rut"
error_new = "ok"
form   =  PacientesForm(request.POST or None)
region    =  Param.objects.filter(tipo='REGI').order_by('descrip')
comuna    =  Param.objects.filter(tipo='COMU').order_by('descrip')
sexo      =  Param.objects.filter(tipo='SEXO').order_by('codigo')
context = {
'form':form,
'variable1':variable1,
'variable2':variable2,
'region':region,
'comuna':comuna,
'sexo':sexo,
'error_new':error_new,
}
if request.method == "POST":
paciente = Pacientes    # modelo
rut_x = request.POST.get('rut') # valor del template
existe = paciente.objects.filter(rut=rut_x).exists()
if existe == True:
error_new = 'error1'
context = {
'form':form,
'variable1':variable1,
'variable2':variable2,
'region':region,
'comuna':comuna,
'sexo':sexo,
'error_new':error_new,
}
else:
if form.is_valid():
paciente = Pacientes    # modelo
form.save()
return redirect('grid_pacientes')
return render(request, 'ficha_pacientes.html',context)

Remember that 'POST' is when you give a submit botton, and you must put
this cod lines in your template:

{% if error_new == "error1" %}
     <p class="error1">R.U.T. de Paciente ya existe !!</p>
{% endif %}

You should put it before of the disply text fields in your template.
Therefore the alert message will be showed at the top.

please tell me if your trouble has been fixed

Sorry for my english

Gabriel Araya Garcia
Chile




El sáb., 24 ago. 2019 a las 12:34, Kean (<kean...@gmail.com>) escribió:

> Hi,
>
> New to Django.
> I've created a user registration form, the issue is it does not run
> validations or report errors with the data entered. It simply routes to the
> redirect url.
> Please can I ensure the user sees the correct error in a post case scenari
> for both a django form, and customsied django form.
>
> forms.py
>
> class UserRegisterForm(UserCreationForm):
>     email = forms.EmailField()
>
>     class Meta:
>         model = User
>         fields = 'username', 'email', 'password1', 'password2'
>
> Views.py
>
> def register(request):
>     if request.method == 'POST':
>         form = UserRegisterForm(request.POST)
>         if form.is_valid():
>             form.save()
>             username = form.cleaned_data.get('username')
>             messages.success(request, f'Account created for {username}')
>         return HttpResponseRedirect('cprofile')
>     else:
>         form = UserRegisterForm()
>     context = {'form': form}
>     return render(request, "register.html", context,)
>
> template.html
>
> <head>
> <title>Registration</title>
> </head>
> <body>
> <br>
> <div class = "container">
> <form method = "POST">
> {% csrf_token %}
> <fieldset class="form">
> <legend class="border-bottom mb-2">Register</legend>
> {{ form|crispy }}
> {% if messages %}
> {% for messages in messages %}
> <div class="alert alert{{ message.tag }}">
> {{ messages }}
> </div>
> {% endfor %}
> {% endif %}
> </fieldset>
> <br>
> <div class = "form">
> <button class ="btn btn-outline-info" type="submit">Register</button>
>
> Any help would be much appreciated
>
> Best,
>
> K
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5a003506-de8d-4587-863d-3fc26e4c45c1%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/5a003506-de8d-4587-863d-3fc26e4c45c1%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
99.7721.15.70

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKVvSDDDhWAQz8pAXenCoeiOcPkVBZVR6oRXBHy21GT6UJv0Aw%40mail.gmail.com.

Reply via email to