Python Django Vue 項(xiàng)目創(chuàng)建過程詳解
1、創(chuàng)建項(xiàng)目
打開pycharm 終端,輸入如下,創(chuàng)建項(xiàng)目
# 進(jìn)入pycharm 項(xiàng)目目錄下 cd pyWeb django-admin startproject pyweb_dome # pyweb_dome 是django項(xiàng)目名稱
2、創(chuàng)建應(yīng)用
# 進(jìn)入項(xiàng)目根目錄 pyweb_dome 下 cd pyweb_dome python manage.py startapp webserver # webserver 為應(yīng)用名
3、創(chuàng)建前端項(xiàng)目
使用vue-cli在根目錄創(chuàng)建一個(gè)名稱叫【frontend】的Vue.js項(xiàng)目作為項(xiàng)目前端
# 使用vue-cli在根目錄創(chuàng)建一個(gè)名稱叫【webfront】的Vue.js項(xiàng)目作為項(xiàng)目前端 vue-init webpack webfront
4、打包vue項(xiàng)目
# 使用 webpack 打包vue項(xiàng)目 cd webfront npm install npm run build
此時(shí)直接運(yùn)行npm run dev也可以直接查看前端 vue界面
npm run build
構(gòu)建完成會(huì)生成一個(gè)文件夾,名字叫dist,里面有一個(gè) index.html 和一個(gè) 文件夾static。
5、使用Django的通用視圖 TemplateView修改靜態(tài)指向路徑(就是讓Django訪問目錄指向我們剛才打包的dist/index.html)
找到項(xiàng)目根 pyweb_demo/urls.py文件作出如下修改,注意1、2兩處修改。
"""pyweb_dome URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.views.generic.base import TemplateView # 1、增加該行 urlpatterns = [ path('admin/', admin.site.urls), path(r'',TemplateView.as_view(template_name='index.html')), #2、 增加該行 ]
6. 配置Django項(xiàng)目的模板搜索路徑和靜態(tài)文件搜索路徑 找到根目錄下 pyweb_demo/settings.py文件并打開,找到TEMPLATES配置項(xiàng),修改如下:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', #'DIRS': [], 'DIRS': ['webfront/dist'], # 修改1 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] # 新增2 # Add for vue.js STATICFILES_DIRS = [ os.path.join(BASE_DIR, "webfront/dist/static"), ]
7,到此基本就配置完成了,回退到根目錄 運(yùn)行命令就可以直接查看效果
python manage.py runserver
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于flask路由app.route及路由參數(shù)的各種用法解析
我們?cè)陂_發(fā)過程中,編寫項(xiàng)目時(shí)所使用的路由往往是指代了框架/項(xiàng)目中用于完成路由功能的類,這個(gè)類一般就是路由類,簡(jiǎn)稱路由,這篇文章主要介紹了有關(guān)flask路由app.route及路由參數(shù)的各種用法解析,需要的朋友可以參考下2024-03-03淺談keras 的抽象后端(from keras import backend as K)
這篇文章主要介紹了淺談keras 的抽象后端(from keras import backend as K),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python使用pytest-playwright的原因分析
pytest-playwright 是一個(gè) Python 包,它允許您使用 Microsoft 的 Playwright 庫在 Python 項(xiàng)目中進(jìn)行端到端測(cè)試,這篇文章主要介紹了Python為什么使用pytest-playwright,需要的朋友可以參考下2023-03-03python中pandas.DataFrame排除特定行方法示例
這篇文章主要給大家介紹了關(guān)于python中pandas.DataFrame排除特定行的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03Python自動(dòng)調(diào)用IE打開某個(gè)網(wǎng)站的方法
這篇文章主要介紹了Python自動(dòng)調(diào)用IE打開某個(gè)網(wǎng)站的方法,涉及Python調(diào)用系統(tǒng)win32組件的相關(guān)技巧,需要的朋友可以參考下2015-06-06Python安裝Imaging報(bào)錯(cuò):The _imaging C module is not installed問題解決
這篇文章主要介紹了Python安裝Imaging報(bào)錯(cuò):The _imaging C module is not installed問題解決方法,原來是PIL庫的庫文件沒有加到系統(tǒng)中導(dǎo)致老是提示這個(gè)錯(cuò)誤,需要的朋友可以參考下2014-08-08Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡(jiǎn)單實(shí)現(xiàn),具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10