Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

440
Visualizações
How to connect a Django view with angular js Front end?

I'm fairly new to Django rest and angularjs. I'm trying to create a Django view which will have a function and this function has to be called via a button in angular js. Can anyone please help me out? Thanks

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This might help: Django - Angular Tutorial

General answer would be: 1) Using for example Django REST expose your method (endpoint) 2) From Angular application send request to the previously exposed endpoint.

about 4 years ago · Santiago Trujillo Relatório

0

say this is your Django/python api end point (assume controller file to be random.py)

def getRandomInfo(request):
try:
    result = database.getRandomInfo()
    return JsonResponse(result, content_type="application/json", safe=False)
except Exception as e:
    import traceback
    traceback.print_exc(e)
    return JsonResponse({error:'sorry'}, content_type="application/json", safe=False)

Via angular, provided you have resolved all the dependencies and have a functioning angular app and/or controller set up in your html/js

you can call the api using below code

app.controller("appControllerName", function ($scope, $http) {
    $http.get("{% url 'getRandomInfo' %}")
            .success(function (response, status) {
                response_data = response
                $scope.data = response_data;
            }).error(function (response, status) {
        $scope.status = status;
    });
  }

if you notice the {% url 'getRandomInfo' %} , this is defined in your urls.py , u'll have to add a line like this over there where getRandomInfo will have to be the 'name' of the url

url(r'^random/getRandomInfo/$',random.getRandomInfo, name='getRandomInfo'),

Over here you controller name is random and a function within that controller is def getRandomInfo, so django allows two ways to access this endpoint, either via the url r'^random/getRandomInfo/$ (yes regex is allowed here) or via the name as stated in the example here.

Best of Luck :)

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda