Django select option - django

I have a form which has a select field (your color). In front of the select field, I have a button that produces a popup which allows users to create new color before they submit. I am submitting that color form via Ajax. After adding a new color to the database, the popup closes.
I want the newly added color to show in the select list without reloading the page.
Is this possible?

I think you can do this by the same ajax where the success triggers. Code below may help a little :
$(".submit").click(function () {
$.ajax({
url: 'url where your view waits',
data: {
'post': "your data on post",
},
type: 'post',
cache: false,
success: function (data) {
// here you can do DOM manipulation add new object to the list.
// You can also get data from backend in json format an then
// add it to your DOM.
},
});
});
I hope this helps.

Related

item validation is not working properly in oracle apex interactive grid page

I have an interactive grid in APEX page under same region I have created an Item: "P300_ADD_MODIFY_REASON".
I want to put a validation for that item, which when I did with a PLSQL expression (:P300_ADD_MODIFY_REASON is not null) and Server side condition in validation for Item is null. It is working when I do modification in report and add reason but it doesn't work if I miss to add reason and do modification later, post then when I add reason it gives error. To resolve it I have do some change in report with added reason then it works.
What I understand is, that item which holds value doesn't capture in validation when I try to save page 2nd time.
I am sure there should be some other method to put validation on Item as with column I am able to.
TIA
The issue is that the "Save" button of the interactive grid is used. That will just cause a submit of the interactive grid region, not a full page submit.
The fastest workaround is to create an additional button on the page that submits the page and hide the default save button on the IG. This will also submit the interactive grid changes and submit the page item P4_NEW which will then fire the validation.
To hide the default save button on the interactive grid (since that shouldn't be used), set the IG Region > Attributes > Initialization JavaScript Function to
function (config)
{
var $ = apex.jQuery,
toolbarData = $.apex.interactiveGrid.copyDefaultToolbar(),
toolbarGroup = toolbarData.toolbarFind( "actions2" );
//Hide save button
toolbarGroup.controls.splice(toolbarGroup.controls.indexOf("save"),1);
config.toolbarData = toolbarData;
return config;
}
A cleaner solution is to remove the original "Save" button from the interactive grid and add a new "Save" button with a custom action to submit the page. For that use the following Initialization Javascript Function instead of the one above:
function (config)
{
var $ = apex.jQuery,
toolbarData = $.apex.interactiveGrid.copyDefaultToolbar(),
toolbarGroup2 = toolbarData.toolbarFind( "actions2" );
//Hide original save button (last array element)
toolbarGroup2.controls.pop();
// Add new button with page submit action
toolbarGroup2.controls.push(
{
type: "BUTTON",
label: "Save",
action: "save",
//icon: "icon-ig-save",
iconBeforeLabel: true,
hot: true,
action: "custom-ig-save"
});
config.initActions = function( actions ) {
actions.add( {
name: "custom-ig-save",
action: function(event, focusElement) {
apex.submit('SAVE');
}
});
}
config.toolbarData = toolbarData;
return config;
}

How to handle multiple actions in Django-Template

I have created a web app in which page which consist of choose file input and Now I need to preprocess the data with one button and that preprocessed data should be used for next button called forecast to show the result in that page.Is it possible with Django.
Does it compulsory to take any action in page with url changes in django?
The question is not completely clear, but from what I understand you can use Ajax. On first button click send an ajax request, process it in the view logic, send the appropriate data using maybe JsonResponse, and in the success block of your ajax request set the appropriate data for your forecast button using jquery. Something like -
$.ajax({
url: <url_of_data_processing_logic>,
type: 'post',
data: <required data>,
dataType: 'json',
success: function (data) {
$("#<forecast_button_Id>").html(data.forecast_html);
},
error: function (e) {
console.log(e);
}
});

can't access to the POST variable that sent from register form in django

I have a register form in html that has a button that type of submit and the name of registerSubmit like this:
<input class="RegisterButton right green" name="registerSubmit" type="submit" value="Continue">
I want to check the form with Ajax and then if every thing is valid then redirect the user to her/his profile.But when I want to redirect the user to his/her profile I can't send the submit input that its name is registerSubmit. here is a section of JQuery code that related to this event:
$("#mainRegisterForm").submit(function(event){
var thisRegisterForm = $(this);
$(".ajaxLogoRegister").show();
event.preventDefault();
var firstName = $("input[name='firstname']").val();
var lastName = $("input[name='lastname']").val();
var emailAddress = $("input[name='email']").val();
var password = $("input[name='password']").val();
var rePass = $("input[name='rePass']").val();
var sex = "NULL";
if ($("input[name='gender']").is(":checked")){
sex = $("input[name='gender']:checked").val();
}
var data ={
firstname:firstName,
lastname:lastName,
emailaddress:emailAddress,
password:password,
repass:rePass,
sex:sex
};
$.ajax({
url: 'ajax/registercheck',
data: data,
dataType: 'json',
success:function(result){
if(result.fn==0){
//do some stuff
}
//another if conditions that checks the validity of every field of refister form
...
...
$(".ajaxLogoRegister").hide();
if(result.isOK=='1'){ //isOK = 1 when every filed is valid and user must redirect to his/her profile
thisRegisterForm.unbind('submit').submit();
}
}
});
});
My main problem is that when I click the registerSubmit button in views.py the request.POST.get('registerSubmit') returns None object instead of Continue value.
when I don't use Ajax and send the form normally registerSubmit value is Continue and every thing is OK.where I am wrong?
By default, $.ajax(); will issue a GET request, not a POST. You need to specify that in your ajax options. Second, I don't see where you're accessing the value of the element named "registerSubmit". I see an "rePass", but you're not including the value you're expecting to find in the request.
You need to either specifically set the "type" ajax option type to "POST" or retrieve the method property from the form element:
$ajax({
url: 'ajax/registercheck',
data: data,
dataType: 'json',
type: 'POST',
....
});

HTML FORM - have form action="" occur before onsubmit=""

I've got a pop-up form, onsubmit it closes itself and also refreshes the parent screen.
It uses action="x.php" to transfer session data.
My problem is that in the form onsubmit="js()" is occuring before action="x.php", so the data doesn't get sent before the window closes.
How can I switch the order that these are executed?
Use jQuery AJAX to submit the form and perform your onsubmit code in the success callback.
$("#formID").submit(function() {
/*
Do client side validation before you submit the form with AJAX
*/
var formData = $("#formID").serialize();
$.ajax({
type: "POST",
url: "x.php",
data: formData,
success: function() {
/*
The actions you want to perform
window.close(); // etc etc
*/
}
});
return false; // stop default form submit because we're using ajax
});

Why does my Django request.method does not match POST

$.ajax({
type :'GET',
url : geturl(a),
// type: $(this).attr('method'),
dataType : 'json',
views.py:
if request.method=="POST":
if request.POST.get('monyrsubmit'):
monthform=MonthForm(request.POST)
if monthform.is_valid():
selected_month=monthform.cleaned_data["Month"]
selected_year=monthform.cleaned_data["Year"]
print selected_month
print selected_year
can i have both GET and POST requests in the type field of ajax. im using a form and only when the submit button is clicked im trying to display information based on the data submitted. if request.POST.get('monyrsubmit') does not work.
Help will be appreciated
It's very simple. You have to abstract the events.
function event_page_load() {
function ajax_request('GET')
}
function click_submit_button() {
function ajax_request('POST')
}
function ajax_request(type) {
$.ajax({
type : type,
......
......
})
}
You can also consider the follwoign general guidelines.
GET and POST should be used based on the type of the request to the server
- If you are reading the existing data(without modification) from the server, use GET
- if you are writing/modifying any data in the server, use POST
in jQuery, you can use these simple methods.
For GET requests
$.get(
url,
{param1: "value1", param2: "value2"},
function(responseText){
// todo ;
},
"html"
);
for POST requests
$.post(
url,
{param1: "value1", param2: "value2"},
function(responseText){
// todo ;
},
"html"
);
Make sure that you have disable the browser caching.
$.ajaxSetup ({
cache: false
});
In django side, you can use request.is_ajax() method to verify the ajax call and you can filter based on request.method property.
You can refer all the possible usages of AJAX with Djano at https://github.com/sivaa/django-jquery-ajax-exmaples