Solution 1 :

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 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-data="{{ value_i_need }} 
        data-target="#list_dom" >{{ value }}

  $("#ajax-caller").on('click', function (){
    var url = $(this).getattr('data-url');
    var data = $(this). getattr ('data-data');
    var target = $(this). getattr ("data-target");

      url: URL,
      method: get,
      data: data,
      success: function (response){
        target.html = response;

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.


    <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 %}
        {% endfor %}
        The unblocked IP addresses are:
        {% for i in unblocked_ip %}
        {% endfor %}

def get_ip(request):
    if request.method == 'POST':
        input_email = request.POST.get('emailvalue')
        three_days_ago = - datetime.timedelta(days = 30)
        time_in_format = three_days_ago.strftime('%Y-%m-%dT00:00:00.000Z')
        security_domain = '' + 'q=' + input_email + '&since=' + str(time_in_format)

        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:

        threat_ip_list = ['', '', '']
        unblocked_ip = []

        for i in threat_ip_list:
            if i in nonduplicate_list:

    return render(request, 'index.html', {'nonduplicate_list': nonduplicate_list, 'unblocked_ip': unblocked_ip})

urlpatterns = [
    path('', views.button),
    path('output/', views.get_ip, name = 'script')