Answer the question
In order to leave comments, you need to log in
Base Classes Django + Ajax not working POST request?
ajax request in template
<a onclick="company_find()" class="btn btn-sm btn-success">Add</a>
function company_find() {
$.ajax({
url: '{% url 'find_company' %}',
type: 'GET',
dataType: 'html',
success: function (response) {
$('#detail').html(response);
}
})
}
<form onsubmit="company_find_1()" id="form" class="smart-form client-form" method="post">
function company_find_1() {
var msg = $('#form').serialize();
$.ajax({
url: '{% url 'find_company' %}',
type: 'POST',
data: msg,
dataType: 'html',
success: function (response) {
$('#company_dt').html(response);
}
})
}
class CompanyFindView(FormView):
form_class = InnForm
template_name = 'company_find.html'
def form_valid(self, form):
print(self.request)
context = self.get_context_data()
inn = form.cleaned_data['inn']
company = Company.objects.filter(inn=inn)
if company:
context['companies'] = company
return render_to_response('company_find_dt.html', context)
else:
new_companies = requests.get('https://ru.rus.company/интеграция/компании/?инн=' + str(inn)).json()
try:
for new_company in new_companies:
full_company = requests.get('https://ru.rus.company/интеграция/компании/' + str(new_company['id']) + '/').json()
new_company = Company(name=full_company['shortName'],
inn=full_company['inn'],
ogrn=full_company['ogrn'],
adress=full_company['address']['fullHouseAddress'],
index=full_company['address']['postalIndex'],
city=full_company['address']['region']['name'])
new_company.save()
full_companies = Company.objects.filter(inn=inn)
if full_companies:
context['companies'] = full_companies
return render_to_response('company_find_dt.html', context)
return redirect('company_create')
except KeyError:
return redirect('company_create')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question