Vue.js在Django的HTML中无法正常工作,请帮助我。

huangapple go评论126阅读模式
英文:

Vue.js does't work in HTML with Django, please assist me

问题

以下是您要翻译的代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
    <!--javascript extensions-->
    <script src='../static/js/vue.min.js'></script>
    <script src='../static/js/axios.min.js'></script>
    <script src="../static/js/jquery-3.6.1.min.js"></script>

    <!--Css-->
    <link rel="stylesheet" href='../static/css/home.css'>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">

    <!--Bootstrap 5-->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
    <div id="register">
        <div class="container my-5">
            <div class="row justify-content-center">
              <div class="col-lg-6">
                <div class="card">
                  <div class="card-body">
                    <form method="post">
                      {% csrf_token %}
                      <a href="{% url 'home' %}">
                        <h2 class="text-center mb-4" style="text-decoration: none; color: black; font-size:2em;">
                            MoHome
                        </h2>
                      </a>
                        <div class="mb-3">
                            <label for="username" class="form-label">User Name</label>
                            <input v-bind:value="username" type="text" class="form-control" id="username" required="required">
                        </div>
                        <h3>{[ 10/2 ]}</h3>
                        <div class="mb-3">
                            <label for="firstname" class="form-label">First Name</label>
                            <input v-bind:value="firstname" type="text" class="form-control" id="firstname" required="required">
                        </div>
                        <div class="mb-3">
                            <label for="lastname" class="form-label">Last Name</label>
                            <input v-bind:value="lastname" type="text" class="form-control" id="lastname" required="required">
                        </div>

                        <div class="mb-3">
                            <label for="email" class="form-label">Email</label>
                            <input v-bind:value="email" type="email" class="form-control" id="email" required="required">
                        </div>

                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <input v-bind:value="password" type="password" class="form-control" id="password" required="required">
                        </div>

                        <div class="mb-3">
                            <label for="confirmPassword" class="form-label">Confirm Password</label>
                            <input v-bind:value="confirmPassword" type="password" class="form-control" id="confirmPassword", required="required">
                        </div>
                        <div class="text-center">
                            <button type="submit" class="btn btn-primary" v-on:click="register">Register</button>
                        </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
    </div>
</body>
      <script>
          var register = new Vue({
              el: '#register',
              data: {
                  firstname: "",
                  lastname: "",
                  username: "",
                  email: "",
                  password: "",
                  confirmPassword: "",
              },
              methods: {
                  register: function (){
                      console.log("register start");
                      if (this.password !== this.confirmPassword) {
                          alert("The Password IS DIFFERENT THAN CONFIRM PASSWORD!");
                      } else {
                          const data = {
                              firstname: this.firstname,
                              lastname: this.lastname,
                              username: this.username,
                              email: this email,
                              password: this.password,
                              confirmPassword: this.confirmPassword
                          }
                          axios.post("/register", data)
                              .then(res => {
                                  console.log(res);
                              })
                              .catch(error => {
                                  console.log(error);
                              })
                          }
                      }
                  },
                  delimiters: ["{[", "]}"],
              }
          ).$mount("#register")
      </script>
</html>

希望这可以帮助您找到问题所在。如果还有其他问题,请随时提出。

英文:

The code run

Here is the code:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Register&lt;/title&gt;
&lt;!--javascript extensions--&gt;
&lt;script src=&#39;../static/js/vue.min.js&#39;&gt;&lt;/script&gt;
&lt;script src=&#39;../static/js/axios.min.js&#39;&gt;&lt;/script&gt;
&lt;script src=&quot;../static/js/jquery-3.6.1.min.js&quot;&gt;&lt;/script&gt;
&lt;!--Css--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&#39;../static/css/home.css&#39;&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
&lt;link href=&quot;https://fonts.googleapis.com/css2?family=Ubuntu&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
&lt;!--Bootstrap 5--&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;register&quot;&gt;
&lt;div class=&quot;container my-5&quot;&gt;
&lt;div class=&quot;row justify-content-center&quot;&gt;
&lt;div class=&quot;col-lg-6&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;form method=&quot;post&quot;&gt;
{% csrf_token %}
&lt;a href=&quot;{% url &#39;home&#39; %}&quot;&gt;
&lt;h2 class=&quot;text-center mb-4&quot; style=&quot;text-decoration: none; color: black; font-size:2em;&quot;&gt;
MoHome
&lt;/h2&gt;
&lt;/a&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;username&quot; class=&quot;form-label&quot;&gt;User Name&lt;/label&gt;
&lt;input v-bind:value=&quot;username&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;username&quot; required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;h3&gt;{[ 10/2 ]}&lt;/h3&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;firstname&quot; class=&quot;form-label&quot;&gt;First Name&lt;/label&gt;
&lt;input v-bind:value=&quot;firstname&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;firstname&quot; required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;lastname&quot; class=&quot;form-label&quot;&gt;Last Name&lt;/label&gt;
&lt;input v-bind:value=&quot;lastname&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot; required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;email&quot; class=&quot;form-label&quot;&gt;Email&lt;/label&gt;
&lt;input v-bind:value=&quot;email&quot; type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;email&quot; required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;password&quot; class=&quot;form-label&quot;&gt;Password&lt;/label&gt;
&lt;input v-bind:value=&quot;password&quot; type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;password&quot; required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;confirmPassword&quot; class=&quot;form-label&quot;&gt;Confirm Password&lt;/label&gt;
&lt;input v-bind:value=&quot;confirmPassword&quot; type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;confirmPassword&quot;, required=&quot;required&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;text-center&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; v-on:click=&quot;register&quot;&gt;Register&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;script&gt;
var register = new Vue({
el: &#39;#register&#39;,
data: {
firstname: &quot;&quot;,
lastname: &quot;&quot;,
username: &quot;&quot;,
email: &quot;&quot;,
password: &quot;&quot;,
confirmPassword: &quot;&quot;,
},
methods: {
register: function (){
console.log(&quot;register start&quot;);
if (this.password !== this.confirmPassword) {
alert(&quot;The Password IS DIFFERENT THAN CONFIRM PASSWORD!&quot;);
} else {
const data = {
firstname: this.firstname,
lastname: this.lastname,
username: this.username,
email: this.email,
password: this.password,
confirmPassword: this.confirmPassword
}
axios.post(&quot;/register&quot;, data)
.then(res =&gt; {
console.log(res);
})
.catch(error =&gt; {
console.log(error);
})
}
}
},
delimiters: [&quot;{[&quot;, &quot;]}&quot;],
}
).$mount(&quot;#register&quot;)
&lt;/script&gt;
&lt;/html&gt;

I had check my code as best as I can and even use ChatGpt to assit me. Also I had check my path of the Vue.js file. And there are other two HTML file with Vue.js that have successfully work with my django. Please, I couldn't find the problem of my code even I had compared with my other successed HTML file with Vue.js. By the way, I had changed the input tag<> from v-model to v-bind:value but it still doesn't work. The result of {[ 10 / 5 ]} is still showing {[ 10 / 5 ]}, not 2 instead. Thus, it has reveal that the Vue.js doesn't work successfully. Please help me!

答案1

得分: 0

After id="confirmPassword", you have an unnecessary comma that generates a parsing error.

英文:

After id=&quot;confirmPassword&quot;, you have an unnecessary comma that generates a parsing error.

huangapple
  • 本文由 发表于 2023年3月23日 11:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定