The solution I ended up with is separating it to two different functions, one to take in a form and another for a button. I passed the html form variable by using request.session
between the two functions.
Solution 1 :
Solution 2 :
I’m not sure if I get the idea but I want to issue to mine.
instead of form, make a button group, then give each one some attribute like data-url, data-data, data-target,…
then use ajax to be able to change the target doms HTML value and data in time.
<button id="ajax-caller"
data-url="my_list_returner_url"
data-data="{{ value_i_need }}
data-target="#list_dom" >{{ value }}
</button>
<script>
$("#ajax-caller").on('click', function (){
var url = $(this).getattr('data-url');
var data = $(this). getattr ('data-data');
var target = $(this). getattr ("data-target");
$.ajax({
url: URL,
method: get,
data: data,
success: function (response){
target.html = response;
};
)};
)};
</script>
I’m not good at js, so search for correcting the js part 🙂
Problem :
I’m extremely new to Django, but I have been trying to figure out the solution for hours. I want to pass a variable from a html form to the backend and then use the variable there to make an api request. I want the API result to be showing on the html file as well as it being checked against a list(in the backend) and having the result from the comparison on the html file as well.
I would like onClick of submitting the variable on HTML (which is a email) that everything works without having multiple buttons/form.
I believe I have multiple errors.
Any help is greatly appreciated.
index.html
<body>
<form action="#" method="post">
{% csrf_token %}
<input type="text" class="form-control" id="emailvalue" placeholder="" value=" "name="emailvalue">
<input type="submit" value="Submit" onclick="location.href={% url 'script' %}"><hr>
The Associated IP addresses are:
{% for j in nonduplicate_list %}
<li>{{j}}</li>
{% endfor %}
<hr>
The unblocked IP addresses are:
{% for i in unblocked_ip %}
<li>{{i}}</li>
{% endfor %}
</form>
</body>
views.py
def get_ip(request):
if request.method == 'POST':
input_email = request.POST.get('emailvalue')
three_days_ago = datetime.datetime.now() - datetime.timedelta(days = 30)
time_in_format = three_days_ago.strftime('%Y-%m-%dT00:00:00.000Z')
security_domain = 'https://security.com/api/v1/logs?' + 'q=' + input_email + '&since=' + str(time_in_format)
print(security_domain)
r = requests.get(security_domain)
data = json.loads(r.content)
data1 = str(data)
ip_pattern = re.compile ('d{1,3}.d{1,3}.d{1,3}.d{1,3}')
ip_result = re.findall(ip_pattern, data1)
nonduplicate_list = []
for i in ip_result:
if i not in nonduplicate_list:
nonduplicate_list.append(i)
print(nonduplicate_list)
threat_ip_list = ['1.1.1.1', '168.213.156.142', '2.2.2.2']
unblocked_ip = []
for i in threat_ip_list:
if i in nonduplicate_list:
unblocked_ip.append(i)
print(unblocked_ip)
return render(request, 'index.html', {'nonduplicate_list': nonduplicate_list, 'unblocked_ip': unblocked_ip})
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.button),
path('output/', views.get_ip, name = 'script')
]