When incorporating Vue.js <template> into Django HTML files, an error is thrown: Uncaught SyntaxError: Unexpected token '<'

Hello everyone, I am completely new to working with both django and vue.js. Currently, I am in the process of developing a project where I am utilizing django for the backend and integrating vue.js for the frontend. However, I am facing difficulties when it comes to calling vue.js functionalities or components in django html templates. I seem to be struggling to figure out the correct steps to successfully integrate vue into my django project. I'm not sure if I am on the right path or if there is a better way to approach this. I have also already installed node.js. Here are the snippets of my code.

Django Template located at users\templates\users\master.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Profile</title>  
    {% load bootstrap4 %}
    {% bootstrap_css %}
    {% bootstrap_javascript jquery='full' %}
</head> 

<body>

    <div class="container">

        {% block content %}
        {% endblock %}

    </div>

</body>

<script src="{% static 'js/vue.global.js' %}"></script>
<script src="{% static 'js/custom-vue.js' %}"></script>
<script src="{% static 'js/EventForm.vue' %}"></script>
    

Path: C:\Users\Jun\python-project\calendarproject\static\js

https://i.sstatic.net/yjxuC.png

EventForm.vue

<!-- EventForm.vue -->
<template>
<div class="event-form">
<!-- Your calendar event form HTML here -->
<form @submit.prevent="addEvent">
  <!-- Form fields for event details -->
  <!-- For example: Event title, date, time, description, etc. -->
  <!-- You can use Bootstrap or any other CSS framework for styling -->
 </form>
 </div>
 </template>

 <script>
   export default {
     methods: {
   addEvent() {
  // Logic to handle event creation and submission
  // You can use Axios or Django REST Framework API to save events to the server
  // Close the form or reset form fields as needed
    }
   }
  };
  </script>

   <style scoped>
   /* CSS styling for your event form */
   </style>

custom-vue.js

const app = Vue.createApp({
data() {
return {
  isEventFormVisible: false // Initially, the event form is hidden
  };
 },
 methods: {
  showEventForm() {
  this.isEventFormVisible = true;
  }
  }
});

app.mount('#app');

Error Screenshots

https://i.sstatic.net/CG62j.png

https://i.sstatic.net/7MyLc.png

Answer №1

Vue modules cannot be directly fetched as static files in Django; instead, they must undergo a "build" process to generate bundled JS and CSS files, which can then be included as static files in Django.

If you need more information, please check out this gist: https://gist.github.com/lsapan/bdc1fbba71058cc337abc181a84b4f49

Answer №2

Compiling SFC (Single-File Component) requires the use of building tools.

If opting for CDN usage directly, this resource can be beneficial.

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

Attempting to decode JSON data

I'm new to node.js and trying to learn how to send a JSON object using REST. However, I keep getting an error message that says "SyntaxError: Unexpected token  in JSON at position 0". I've checked the JSON using an online validator and it seem ...

It appears that the flex-grow property is not functioning as expected

I am working with a parent div and two children divs underneath it. I noticed that when I set the flex-grow property of the first child to zero, its width remains constant. However, if I add text to the second child, the width of the first child decreases. ...

An unexpected error occurred while attempting to establish a connection to a MySQL server using Sequelize: 'LRU is not a constructor'

I have been working on a web application that utilizes boardgame.io's Tic-Tac-Toe tutorial game, with a twist - the results of each match are saved to a MySQL database. The current state of my code involves trying to establish a connection to the data ...

Tips for maintaining user login sessions even after the browser is closed using nuxt auth

I need assistance with the configuration in my nuxt.config.js file: auth: { strategies: { local: { scheme: 'local', token: { property: 'meta.token', global: true, }, user: { property ...

What is the optimal method for navigating through a complex nested object in Angular?

Looking to navigate through a nested object structure called purchase. Within this structure, there is a sub-array named purchaseProducts which contains another sub-array called products along with additional data. What methods do you suggest for efficien ...

Error encountered: Angular ExpressionChangedAfterItHasBeenCheckedError Description:

I am currently developing an application that utilizes Angular on the frontend and Dropwizard on the backend. Within this application, there is a food intake list where I match product ids to corresponding product objects in a product list using the getPr ...

The clarity and completeness of images on canvas are lacking

I've experimented with numerous examples found on the internet, but none of them seem to be effective. My goal is to display a full image on a canvas without it being zoomed in and losing quality. In addition, I attempted to rotate images (from stand ...

When making an AJAX request to an ASP.NET web method, strange characters are appended to the end of the response text. This issue seems

I need assistance with the following code: $.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', url: location, data: JSON.stringify(ajaxData), dataType: 'xml', success: ca ...

Use the ng-repeat directive to display multiple items and let the user input a quantity for each item. Then, in AngularJs, gather all the form data, including the repeated items and their quantities, and

Hey there! I have a question related to AngularJs: How can I repeat pre-selected items using (ng-repeat) and allow users to enter a quantity for each item in a subsequent step, then submit all the form data? I'm wondering if adding $index to the repe ...

Is it possible to customize the default error message for a validator in Django?

I am working with a model field that utilizes a validator to limit the maximum value. The current error message states "Ensure this value is less than or equal to 40." Is there a way to customize this error message? from django.core.validators import MaxV ...

Is your ng-if not functioning properly with raw JavaScript?

I am attempting to verify the existence of a key within an object in an Angular $scope. Within my controller, I have defined this: $scope.the_object = {'the_key': 123}; And in my template, I have included the following: <span ng-if="'t ...

Loading javascript libraries that are contained within an appended SVG document

I am currently working on developing a browser-based SVG rasterizer. The unique aspect of this project is that the SVG files may contain JavaScript code that directly impacts the output, such as randomly changing element colors, and utilizes external libra ...

jQuery function to automatically close any other opened divs when a new div is opened

I have multiple elements that reveal a div when clicked. The issue is that if all elements are clicked, all divs open instead of just the one that was clicked. This is my jQuery code: <script> $(document).ready(function() { $('.servicemark ...

retrieve a response from the server during an ajax request when submitting a file using a multi-data form

I'm encountering a slight issue with my project. I have multiple JSPs and Java classes in my setup. In one of the JSP files, I've created a form consisting of only an input type="file" and type="submit". After that, I make an AJAX call to send al ...

It's important to understand how to correctly send state values from a parent component to a child component

I have a tab menu in my code and I am looking to implement a feature where tabs can be switched using a button. The challenge is passing the values of these tabs into the onclick function of the button in order to navigate between them. Here is an example ...

Why does the MEAN Stack continue to route using the '#' symbol in the URL?

Currently navigating the realm of back end development. Started off by following a guide on express from thinkster. In need of some clarification. Initially, I grasped that front-end and back-end routing serve different purposes. Front-end routing relates ...

What is the process of declaring a variable within a Vue template?

Issue I find myself in a situation where I must temporarily hold the output of a function call in Vue templates. This is a common scenario within loops, especially when computed properties are not easily applicable. <ul> <li v-for="vehicleType ...

Filtering data using Angular

Just starting out with angular and trying to figure out the best way to achieve this: I have some data in a json file that includes items of two types: 'course' and 'tutorial'. Tutorials are related to courses through a specific field ...

Updating Material-UI DataGrid Component with Fresh State Information

Hey there, We've integrated Material-UI into our project to build a DataGrid table of users where we can delete selected entries (checkbox) using a button. Whenever the button is clicked, it triggers an update in the object's state (removing th ...

Error: Attempting to access 'use' property of undefined object not allowed

I'm embarking on a new project using vue js and I believe I have successfully installed all the necessary dependencies via the terminal. The packages I've installed include npm, vue, vue-bootstrap, and vue-router. The error seems to be originatin ...