Вам также нужно POST csrf_token в ajax:
$(document).on('submit','#new_user_form',function(e){
e.preventDefault();
$.ajax({
type : 'POST',
url : 'skill/create', // hardcoding urls? not a good idea, use {% url 'url_name' %}
data : {
name : $('#name').val(),
type : $('#type').val(),
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').val() // No traling comma
},
sucess:function(){}
});
});
Теперь исправьте ваш шаблон:
<form id="new_user_form">
{% csrf_token %} // add csrf token
<input type="text" id="name" placeholder="Name" name="name" value=""> // add name
<select class="" name="skill_type" id="type">
<option value="1">Developer</option>
<option value="2">Network</option>
<option value="3">System</option>
<option value="4">Database Analysis</option>
</select>
<input type="submit" value="submit"> // submit doesn't require name attribute
</form>
Имя элементов ввода из html используется на резервной копии для получения данных POSTтак что на ваш взгляд:
def create_skill(request):
if request.method == 'POST':
name = request.POST['name']
type = request.POST['skill_type'] # name for select in your html is 'skill_type' so use that
skill = Skill(skill_name=name,skill_type=type)
skill.save()
return HttpResponse('') # Empty HttpResponse doesn't makes much sense, also for ajax I would recommend JsonResponse