Struggling to get Angular to properly load routes

I've been working on a local web page recently, following this helpful tutorial that shows how to integrate Django and Angular for user authentication. However, I've encountered an issue where the "register" button doesn't display anything except changing the directory to /register. It seems like it could be a routing problem, as there are no errors being thrown. I've tried debugging with no success, and this is my first attempt at creating a website.

The main reason for the difficulty I'm facing is that I didn't start with the same project template from the tutorial. My goal was to learn by building everything from scratch, resulting in newer packages like Bootstrap and Django being used. If more information is needed, please let me know. Thank you.

/templates/index.html

<!DOCTYPE html>
<html ng-app="hawk">

<head>
  <title>Hawk</title>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>

<body>
  {% include 'navbar.html' %}

  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-12 ng-view"></div>
    </div>
  </div>

  {% include 'javascripts.html' %}
</body>

</html>

/static/javascripts/hawk.routes.js

(function () {
    'use strict';

    angular
        .module('hawk.routes')
        .config(config);

    config.$inject = ['$routeProvider'];

    function config($routeProvider) {
        $routeProvider.when('/register', {
            controller: 'RegisterController',
            controllerAs: 'vm',
            templateUrl: '/static/templates/authentication/register.html'
        }).otherwise('/');
    }
})();

/static/javascripts/authentication/controllers/register.controller.js

(function () {
    'use strict';

    angular
        .module('hawk.authentication.controllers')
        .controller('RegisterController', RegisterController);

    RegisterController.$inject = ['$location', '$scope', 'Authentication'];

    function RegisterController($location, $scope, Authentication) {
        var vm = this;
        console.log("\n\nregister\n\n");
        vm.register = register;

        function register() {
            Authentication.register(vm.email, vm.password, vm.username);
        }
    }
})();

/static/javascripts/hawk.js

(function () {
  'use strict';

  angular
    .module('hawk', [
      'hawk.routes',
      'hawk.authentication',
      'hawk.config',
    ]);

  angular
    .module('hawk.routes', [require('angular-route')]);
  angular
    .module('hawk.config', []);
  angular
    .module('hawk')
    .run(run);
  run.$inject = ['$http'];

  function run($http) {
    $http.defaults.xsrfHeaderName = 'X-CSRFToken';
    $http.defaults.xsrfCookieName = 'csrftoken';
  }
})();

/templates/javascripts.html

{% load compress %} {% load staticfiles %} {% compress js %}
<script type="text/javascript" src="{% static '../node_modules/jquery/dist/jquery.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/bootstrap/dist/js/bootstrap.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/bootstrap-material-design/dist/js/material.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/bootstrap-material-design/js/ripples.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/underscore/underscore.js' %}"></script>

<script type="text/javascript" src="{% static '../node_modules/angular/angular.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/angular-route/angular-route.js' %}"></script>
<script type="text/javascript" src="{% static '../node_modules/angular-cookies/angular-cookies.js' %}"></script>

<script type="text/javascript" src="{% static '../node_modules/ng-dialog/js/ngDialog.js' %}"></script>
<script type="text/javascript" src="{% static 'lib/snackbarjs/snackbar.min.js' %}"></script>

<script type="text/javascript" src="{% static 'javascripts/hawk.js' %}"></script>

<script type="text/javascript" src="{% static 'javascripts/hawk.config.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/hawk.routes.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/authentication/authentication.module.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/authentication/services/authentication.service.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/authentication/controllers/register.controller.js' %}"></script> 
{% endcompress %}

/static/javascripts/authentication/services/authentication.service.js

(function () {
    'use strict';
    angular
        .module('hawk.authentication.services')
        .factory('Authentication', Authentication);
    Authentication.$inject = ['$cookies', '$http'];

    function Authentication($cookies, $http) {
        var Authentication = {
            register: register
        };

        return Authentication;

        function register(email, password, username) {
            return $http.post('/api/v1/accounts/', {
                username: username,
                password: password,
                email: email
            });
        }
    }
})();

/HawkProject/urls.py

from django.contrib import admin
from django.urls import path, re_path, include
from rest_framework_nested import routers
from authentication.views import AccountViewSet
from HawkProject.views import IndexView

router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^api/v1/', include(router.urls)),

    re_path(r'^.*$', IndexView.as_view(), name='index')
]

Answer №1

Although this response may not directly answer the question, it offers a different approach to the problem.

The tutorial provided seems lengthy and lacks video content. In my experience working with Django and AngularJS for an e-commerce website, I recommend keeping them separate to avoid conflicts. Here's a brief overview of how I implemented User Registration:

  1. I maintained the User form separately from AngularJS forms since Django handles User Management differently (Sign in, Sign Up, session handling).

  2. I added a

    <a href="/sign_up">Register</a>
    link in the nav-bar. Note: The url should be mapped in the urls.py file.

     Insert code snippet here...
    

Referencing

url(r'^sign_up/', ('ecommerce.views.register_user')),

In the views.py

 Insert code snippet here...

Note: The code within def register_user leverages a core Django feature for user registration. Further resources can be found online.

  1. If you need to render an Order address form, create an Angular form and pass values to Django using $http. Make sure to map the corresponding /url_called in urls.py.

  2. Use {% verbatim %} and {% endverbatim %} when incorporating Angular variables to avoid clashes with Django template syntax. Prefer ng-bind where possible.

  3. For beginners, refrain from utilizing $locationProvider initially as it may cause issues with Django integration. Start small by testing basic examples like the one in this plunkr code.

Explore a simple example of ngRoute here.

If you encounter any challenges, feel free to ask another question and share the link in the comments for assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extends the base.html file of myapp specifically, without relying on any other app within my Django project

I have developed a Django project with multiple apps. In the users app, I created a base.html file, but now I want to use the base.html from the articles app instead. I tried using {% extends 'base.html' %} in general, and experimented with: {% ...

Hover over parts of an image to bring attention to them

I am interested in developing a webpage featuring a black and white image of 5 individuals. When hovering over each person, I would like them to illuminate and display relevant information in a dialog box next to them. Do you have any tips on how I can ac ...

`AngularFire services/factories that utilize nested models`

I am currently exploring how to nest models using factory-model/classes in combination with Firebase for data storage. For instance, imagine I am developing a task management app where Lists contain multiple Tasks, and each Task has various Details associa ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

Commitment and the worth of pointing

I am trying to create a promise chain in Angular to execute a series of queries: for(variableIndex in _editableVariable.values) { var v = _editableVariable.values[variableIndex]; if(v.value != v.old_value) { ...

Using CSS/HTML to showcase rows in a vertical table format (or similar)

Can someone please help me with formatting my tabular data in a specific way? column A | column B | column C | column D | ------------------------------------------- 1-1 | 2-1 | 3-1 | 4-1 | ------------------------------------------- 1 ...

Utilize the arrow keys to navigate through the search suggestions

I am facing an issue with my search bar where I am unable to navigate through the suggestions using arrow keys. I have been struggling with this problem for days and would appreciate any help in resolving it. var searchIndex = ["404 Error", "Address Bar ...

Placing a table within a div causes the div to expand in width beyond 100%

A situation has arisen where a table with a large number of columns (30 columns) is being affected by the white-space:nowrap style set on each th tag, causing the parent div to stretch unnaturally. How can this issue be resolved and allow for a horizonta ...

Position a button to be aligned with the bottom of its container

My goal is to have the button positioned at the bottom of the red div, nested inside it. .grand-parent { display: flex; flex-direction: row; flex-wrap: wrap; height: 300px; background-color: green; } .pa ...

Creating a static line or separator based on the content using HTML and CSS

Is there a way to adjust the height of a line or divider based on the content of either the left or right section? For instance, if we have a left panel and a right panel, and we draw a line or divider on the left side panel, is it possible for the line ...

Troubleshooting the issue of installing mysqlclient on Python 3.7 through CloudLinux Python selector to fix errors

This is my first post on StackOverflow. A few days ago, I tried setting up a Python app on my cPanel server for a Django project. I encountered an issue when trying to install mysqlclient using the pip tool. My setup includes: cPanel, CloudLinux OS, Late ...

Show the Canvas element at the back of the DIV

This particular question requires a clear explanation. My goal is to have the canvas act as a background element on the page, while the main content of the page starts in its usual position. I attempted using separate DIVs with their own Z-index values, bu ...

CSS background images struggle to display properly in Safari, often not appearing even when checked in the debugger

I'm currently working on a website that I want to make responsive. I have set up a div ID that is supposed to appear once the width of a mobile device reaches a certain point. Here is the CSS code: #mobilenavbuttons1 { max-width: 750px; padding-top: ...

Getting JSON with duplicate keys in JavaScript can be achieved by parsing the data using a custom function

I am attempting to retrieve JSON from a URL, but I have encountered an issue where the response object is removing duplicate keys. Is there a way to fetch the JSON without eliminating these duplicates? Below is my JavaScript code: $('document'). ...

Inject the ng-repeat variable into a personalized directive

When working with an ng-repeat and a custom directive, I am facing the challenge of passing the "item" variable from ng-repeat to the directive. Here is an example code snippet illustrating this situation: <li ng-repeat="item in list"> <div c ...

Display/Modify HTML form

Looking for advice on how to create an interactive HTML form that displays data and allows users to edit it by clicking 'Edit' before submitting changes. Any suggestions on how to implement this functionality? ...

Exploring ways to validate the existence of a class in HTML table elements

If I want to verify if each element has a specific class when creating tables and clicking on the corresponding cells above them, This is what I have tried. However, it did not work as expected. How can I make this work correctly? What am I missing her ...

Struggling to set the background color for a div element

I am dealing with the following HTML and CSS: <div id="com_loaded"> <div id="com_loaded_userpic"><a href="#" class="tooltip"><img src="pic.jpg" class="img_poster" /><span>Username</span></ ...

Transforming jquery code into Angularjs code

I am currently working on a jQuery method that adds a 'selected' class to table rows and handles click events. Here is the code: $('.niGridTable table tr').addClass('selected').end().click(function (event) { event = ...

Manage user interface elements on mobile devices

When viewing my label and two textbox controls on a desktop, they are aligned horizontally. However, I want these textboxes to stack on top of each other when the user views the site on mobile. I am using a media query to adjust to a mobile view, and all o ...