


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:
- Node.js and npm: used to install and manage front-end dependencies.
- Python and pip: used to install and manage back-end dependencies.
- Vue CLI: Command line tool for creating Vue projects.
- 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
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
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
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
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>
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>
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
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)
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), ]
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 } } }
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
Execute the following command in the django_project directory:
$ python manage.py runserver
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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

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.

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.

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

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 <div>. 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.

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 <script> tag in the HTML file that refers to the Vue file.

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.
