Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit a form with an href in Django?

Tags:

python

django

I have a link that when clicked, should submit a form that contains an invisible field via a post request. I then should be able to get the field value like:

var = request.POST.get('name_of_var', '')

After looking at a few posts, I can't seem to get this to work. This is what I have so far:

<form id="form-id" method="post">
    <li> 
        <input type="hidden" value="{{ obj }}" name="name_of_var">
        <a href="/activities" onclick="document.getElementById('form-id').submit();"> {{obj}} </a>
    </li>
</form>

In my view I have something like this, but a POST request is never triggered. What could be the issue here?:

if request.POST:
        var = request.POST.get('name_of_var', '')
        return render_to_response('activities/display_activities.html', var)

EDIT: Here is my views.py:

def index(request):
    if request.method == "POST":
        var = request.POST.get('name_of_var', '')
        return render_to_response('activities/display_activities.html', var)

    category1 = Service.objects.filter(category = 'Sports')
    category2 = Service.objects.filter(category = 'Dance')
    category3 = Service.objects.filter(category = 'Music')
    category4 = Service.objects.filter(category = 'Academics')
    category5 = Service.objects.filter(category = 'Art')
    category6 = Service.objects.filter(category = 'College')

    subcat1 = []
    subcat2 = []
    subcat3 = []
    subcat4 = []
    subcat5 = []
    subcat6 = []

    for obj in category1:
        subcat1.append(obj.subcategory)
    subcat1 = list(set(subcat1)) 

    for obj in category2:
        subcat2.append(obj.subcategory)
    subcat2 = list(set(subcat2)) 

    for obj in category3:
        subcat3.append(obj.subcategory)
    subcat3 = list(set(subcat3)) 

    for obj in category4:
        subcat4.append(obj.subcategory)
    subcat4 = list(set(subcat4)) 

    for obj in category5:
        subcat5.append(obj.subcategory)
    subcat5 = list(set(subcat5)) 

    for obj in category6:
        subcat6.append(obj.subcategory)
    subcat6 = list(set(subcat6)) 

    return render_to_response('activities/activities.html', {'user': request.user,  
        'category1':category1, 'category2':category2, 'category3':category3, 
        'category4':category4, 'category5':category5, 'category6':category6,  
        'subcat1':subcat1,'subcat2':subcat2, 'subcat3':subcat3, 
        'subcat4':subcat4, 'subcat5':subcat5,'subcat6':subcat6  })
like image 655
Michael Smith Avatar asked Dec 16 '14 02:12

Michael Smith


People also ask

How do I submit a form to href?

You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.

What is form AS_P in Django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.

How do you customize a form in Django?

So, we have to just design the HTML with css, and use it in Django by just replacing the input tag with render_field and form. field_name with {% %} at the end and design it with our choice.

Is a Django form field that takes in a text input?

CharField() is a Django form field that takes in a text input. It has the default widget TextInput, the equivalent of rendering the HTML code <input type="text" ...> .


2 Answers

This is how I ended up resolving this. Below is an example that is completely different from my original post. Note: replace the id and action with your relevant code. In my example, {{form4}} is passed in through the view and is a custom form in my forms.py with one field.

<form id = "form-id" action="/events/attending" method="post">{% csrf_token %}
    <a href="#" onclick="document.forms['form-id'].submit();" >  Submit </a> 
    {{form4.going}} I am attending this event!
</form>
like image 83
Michael Smith Avatar answered Oct 06 '22 02:10

Michael Smith


You should use <a> like this to prevent auto jump:

<a href="javascript:void(0);" onclick="document.getElementById('form-id').submit();"> {{obj}} </a>

This assume that you submit the form to the current url, if you want to submit to to some other url I suggest you use jquery instead of <a> like this:

<form id="form-id" method="post">
<li>
    <input type="hidden" value=" obj " name="name_of_var">
    <a id="sub" href="javascript:void(0);" onclick="document.getElementById('form-id').submit();"> tttttttttt</a>
</li>

<script type="text/javascript">
$(document).delegate("#sub", "click", function () {
    $.ajax({
        type: "POST",
        url: "/your/url/",
        data: {name_of_var: value_of_var},
        dataType: "json",
        success: function () {
            //do something
        }
    })
});

like image 38
shellbye Avatar answered Oct 06 '22 01:10

shellbye