Table of Contents
{{ message }}
Home Web Front-end Vue.js Practical experience sharing: Vue3+Django4 full-stack project development guide

Practical experience sharing: Vue3+Django4 full-stack project development guide

Sep 09, 2023 pm 05:57 PM
vue django Full stack project development

Practical experience sharing: Vue3+Django4 full-stack project development guide

Practical experience sharing: Vue3 Django4 full-stack project development guide

Introduction:
In today's era of highly developed Internet, full-stack development has become more and more A direction that many developers pay attention to and learn from. The Vue framework is currently one of the most popular front-end frameworks, and Django is a powerful Python back-end framework. Their combination can provide us with a comprehensive full-stack development experience. This article will introduce how to use Vue3 and Django4 to build a complete full-stack project, and share some development experience and code examples.

1. Environment preparation
Before starting the project, you need to prepare the environment. Make sure you have the following software installed:

  1. Node.js and npm: used to install and manage front-end dependencies.
  2. Python and pip: used to install and manage back-end dependencies.
  3. Vue CLI: Command line tool for creating Vue projects.
  4. Django: used to create and manage backend projects.

2. Create a Vue3 project
First, we use Vue CLI to create a Vue3 project. Open the command line interface and execute the following command:

$ vue create vue_project
Copy after login

Follow the command line prompts, select the configuration you need, and wait for the project to be created.

3. Create a Django project
Next, we use Django to create a back-end project. Execute the following command on the command line interface:

$ django-admin startproject django_project
Copy after login

This will create a folder named django_project in the current directory and generate the skeleton of a project.

4. Configure the front-end and back-end connections
In this step, we need to configure the front-end and back-end connections so that the front and back ends can communicate with each other. First, create a file .env.development in the vue_project/src directory and add the following content:

VUE_APP_BACKEND_URL=http://localhost:8000
Copy after login

Herehttp://localhost:8000 is The address where the Django backend is running.

Next, open the vue_project/src/main.js file and add the following code before createApp:

import axios from 'axios'
axios.defaults.baseURL = process.env.VUE_APP_BACKEND_URL
Copy after login

This code snippet sets the default base URL of axios to The backend address we just configured.

5. Develop the front-end page
Now, we can start developing the front-end page. The syntax of Vue3 is slightly different from Vue2, but generally similar. Vue3 provides a more powerful combined API that can better manage code logic. Below is a simple example.

First, create a component file named HelloWorld.vue in the vue_project/src/components directory and add the following content:

<template>
  <div class="hello">
    <h1 id="message">{{ message }}</h1>
  </div>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'HelloWorld',
  setup() {
    const message = ref('Hello, Vue3!')
    return {
      message
    }
  }
}
</script>

<style scoped>
h1 {
  color: red;
}
</style>
Copy after login

This component displays a The red title, the title content is set through the responsive variable defined by ref.

In order to use this component in the page, we need to introduce it in vue_project/src/App.vue. First, delete the original content, and then add the following code:

<template>
  <div id="app">
    <HelloWorld/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Arial', sans-serif;
}
</style>
Copy after login

Here we import the HelloWorld.vue component we just created and reference it in the page.

6. Develop back-end API
In the Django project, we need to create an API to provide back-end services. Take creating a simple user API as an example.

First, execute the following command in the django_project directory to create an application named users:

$ python manage.py startapp users
Copy after login

Create an application named views in the users directory. py file and add the following code:

from django.http import JsonResponse

def get_users(request):
    users = [
        {"id": 1, "name": "Alice"},
        {"id": 2, "name": "Bob"},
        {"id": 3, "name": "Charlie"}
    ]
    return JsonResponse(users, safe=False)
Copy after login

This simple view function returns a JSON response with user information.

Next, open the django_project/django_project/urls.py file and add the following code:

from django.urls import path
from users.views import get_users

urlpatterns = [
    path('api/users', get_users),
]
Copy after login

This code snippet maps the get_users view function to the path /api/users.

7. Front-end and back-end communication
In order for the front-end to access the back-end API, we need to use axios to send HTTP requests. Go back to the vue_project/src/components/HelloWorld.vue file and add the following code in the setup function:

import axios from 'axios'
export default {
  name: 'HelloWorld',
  setup() {
    const message = ref('Hello, Vue3!')
    axios.get('/api/users').then((response) => {
      console.log(response.data)
    })
    return {
      message
    }
  }
}
Copy after login

This code snippet uses axios to send a GET request to /api/ users and print the returned data.

8. Run the project
Finally, we only need to run the front-end and back-end projects separately.

Execute the following command in the vue_project directory:

$ npm install
$ npm run serve
Copy after login

Execute the following command in the django_project directory:

$ python manage.py runserver
Copy after login

Now, open the browser and visit http:// localhost:8080, if everything goes well, you should be able to see the data returned by the backend API in the console.

Summary:
This article introduces how to use Vue3 and Django4 to build a complete full-stack project, and shares some practical experience and code examples. Through this full-stack development approach, we can build web applications with front-end and back-end separation more efficiently. I hope this article can help developers who are learning full-stack development.

The above is the detailed content of Practical experience sharing: Vue3+Django4 full-stack project development guide. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

Vue realizes marquee/text scrolling effect Vue realizes marquee/text scrolling effect Apr 07, 2025 pm 10:51 PM

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with &lt;div&gt;. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

How to query the version of vue How to query the version of vue Apr 07, 2025 pm 11:24 PM

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the &lt;script&gt; tag in the HTML file that refers to the Vue file.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

See all articles