J
J
JonGalt2017-03-21 23:12:22
Django
JonGalt, 2017-03-21 23:12:22

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);
        }
    })
    }

There is a view
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')


When I send a get request via ajax, the template comes, but via Post it doesn't want
Console
[21/Mar/2017 19:55:56] "GET /company/find/ HTTP/1.1" 200 860
[21/Mar/2017 19: 56:00] "POST /company/find/ HTTP/1.1" 200 734
Method Not Allowed (POST): /user/1/contacts
[21/Mar/2017 19:56:01] "POST /user/1/contacts HTTP/1.1" 405 0

For some reason, after "POST /company/find/, it immediately redirects to POST /user/1/contacts (this is the page on which everything happens)
Tell me how to be, so that the template I need is returned with a Post request

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ratnikoff_St, 2017-04-25
@Ratnikoff_St

I don't even connect json !!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question