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??