0
votes

I am learning django and I get thes error when I try to enter a value in an input text and pass it as argument to other form.

Forbidden (403) CSRF verification failed. Request aborted.

Reason given for failure: CSRF token missing or incorrect.

This are my snippets

forms.py

class ConfigForm(forms.ModelForm):
    def __init__(self,*args, **kwargs):
        id_provider = kwargs.pop('id_provider')
        super(ConfigForm,self).__init__(*args, **kwargs)
        self.fields['id_provider'].widget= forms.TextInput(attrs='id_provider:Id_provider'})
    id_provider = forms.CharField()

    class Meta:
       model = Config

views.py

def configView(request):
    a = request.session.get('a', None)
    if request.method == 'POST':
       form = ConfigForm(request.POST, instance=a)
       if form.is_valid():
          save_it = form.save(commit=False)
          save_it.save()
    else:
       form = ConfigForm(instance = a)

    return render_to_response("config.html",{'id_providor':id_provider},context_instance=RequestContext(request))

html code where I try to send the value to the argument:

<form method='post' action="/config/">
<input type="text" class="form-control" name="id_provider" id="id_provider" value="{{ id_providor }}"/>
<input class="btn btn-danger" type="submit" value="Config">

config.html (form where I send the value)

{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-4">
<form method='POST' action='' class='form'>
   <div class="form-group">
        {% csrf_token %}
        {{ form.as_p }}
   </div>
   <button type='submit' class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
{% endblock %}

I don't know if the error if in the view or the way I try to send the parameter. Help please Thanks in advance

UPDATE I resolve the issue with the flag @csrf_exempt above the ConfigView(views.py) Now I have this error:

KeyError at /config/

'id_proveedor'

Request Method:     POST
Request URL:    http://192.168.0.219:8080/config/
Django Version:     1.6.5
Exception Type:     KeyError
Exception Value:    

'id_provider'

Exception Location:     /home/pyc/DjangoProjects/monitor/prov/forms.py in __init__, line 19
Python Executable:  /usr/local/bin/python
Python Version:     2.7.3
Python Path:    

['/home/pyc/DjangoProjects/monitor',
 '/usr/local/lib/python2.7/site-packages/simplejson-3.6.0-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages']

I don't understand what is wrong. Any advice??

2
Where does this html snippet belong to? It is not part of the config.html template, is it?Norman8054
yes is in the config.html template. I put the flag @csrf_exempt in my views.py and the error is gon, now I have a new error: KeyError at /config/ 'id_provider' Exception Location: /home/pyc/DjangoProjects/monitor/prov/forms.py in init, line 19Joseleg

2 Answers

0
votes

You probably don't want to use @csrf_exempt, unless you have a really good reason for disabling the CSRF protection.

It's not 100% clear, but I think you are not including the {% csrf_token %} in the form that you are actually submitting. You need to ensure this tag is used in every HTMl form that will be submitted using POST.

0
votes

Try this:

class ConfigForm(forms.ModelForm):
    def __init__(self,*args, **kwargs):
        id_provider = kwargs.pop('id_provider')
        super(ConfigForm,self).__init__(*args, **kwargs)
        if self.fields.has_key('id_provider'):
            self.fields['id_provider'].widget= forms.TextInput(attrs='id_provider:Id_provider'})
    id_provider = forms.CharField()

    class Meta:
       model = Config

Tell me if it works.