All Projects → synw → django-vitevue

synw / django-vitevue

Licence: MIT license
Manage Vitejs frontends for Django

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects
typescript
32286 projects

Projects that are alternatives of or similar to django-vitevue

electron-vue-template
A very simplistic Electron + Vue 3 template including ViteJS and Electron Builder
Stars: ✭ 60 (+33.33%)
Mutual labels:  vitejs
react-boilerplate
React 18, React-Router, Typescript, Vite, Babel 7, React-Testing-Library, Vitest
Stars: ✭ 27 (-40%)
Mutual labels:  vitejs
vite-plugin-ssr
Like Next.js / Nuxt but as do-one-thing-do-it-well Vite plugin.
Stars: ✭ 1,703 (+3684.44%)
Mutual labels:  vitejs
vue-next-admin
🎉🎉🔥基于vue3.x 、Typescript、vite、Element plus等,适配手机、平板、pc 的后台开源免费模板库(vue2.x请切换vue-prev-admin分支)
Stars: ✭ 1,002 (+2126.67%)
Mutual labels:  vitejs
jplayer-skin-audiocheck
A responsive HTML5 jPlayer skin with playlist.
Stars: ✭ 16 (-64.44%)
Mutual labels:  vitejs
preview-pro
Use pro-layout in vitejs. preview https://sendya.github.io/preview-pro/index.html
Stars: ✭ 71 (+57.78%)
Mutual labels:  vitejs
vitawind
Install and Setting Tailwindcss automatically for Vite
Stars: ✭ 46 (+2.22%)
Mutual labels:  vitejs
troisjs.github.io
📖 Examples and documentation for TroisJS ✨
Stars: ✭ 119 (+164.44%)
Mutual labels:  vitejs
vite-plugin-theme-preprocessor
css theme preprocessor plugin for vite
Stars: ✭ 144 (+220%)
Mutual labels:  vitejs
vite-plugins
🌱 为社区尽一份绵薄之力
Stars: ✭ 63 (+40%)
Mutual labels:  vitejs
js from routes
🛣️ Generate path helpers and API methods from your Rails routes
Stars: ✭ 75 (+66.67%)
Mutual labels:  vitejs
avatar
Simple online tool for cropping images from an URL, your clipboard, or your disk.
Stars: ✭ 63 (+40%)
Mutual labels:  vitejs
html5-blank-slate
A blank starter theme that incorporates Bootstrap 5 with WordPress and Vite for frontend tooling.
Stars: ✭ 21 (-53.33%)
Mutual labels:  vitejs
tailwind-dashboard-template
Mosaic Lite is a free admin dashboard template built on top of Tailwind CSS and fully coded in React. Made by
Stars: ✭ 1,662 (+3593.33%)
Mutual labels:  vitejs
jumpstart-vite
⚡️ Jumpstart a new Rails app with Vite.js + Turbo + Stimulus, and more
Stars: ✭ 67 (+48.89%)
Mutual labels:  vitejs
vui-vc-next
Vue 3 with Vite Playground - Mobile web UI components - (vue3+vite2).
Stars: ✭ 15 (-66.67%)
Mutual labels:  vitejs
dragonfly-dag
完全支持Vue3和Vitejs的DAG流程图组件
Stars: ✭ 54 (+20%)
Mutual labels:  vitejs
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+1544.44%)
Mutual labels:  vitejs
django-vite
Integration of ViteJS in a Django project.
Stars: ✭ 201 (+346.67%)
Mutual labels:  vitejs
vexip-ui
A Vue 3 UI library, Highly customizable property values, Full TypeScript, Performance should be good.
Stars: ✭ 488 (+984.44%)
Mutual labels:  vitejs

Django Vite Vue

pub package

Manage Vitejs frontends and compile them to Django static files and templates. Features

  • Configure Vitejs for Django: use a management command to help configuring Vitejs to compile to Django templates and static files
  • Typescript scaffolding: generate Typescript models from existing Django models
  • Api and views: api helper frontend class configured for Django and login/logout views with single page app support

Install

pip install django-vitevue

Add "vv", to INSTALLED_APPS

Make sure the basic Django template and static dirs settings are present. Run the vvcheck management command to see if the config is ok

Configuration of a Vitejs app to compile to Django templates and static files

Architecture and settings

The recommended file structure for a single page app is:

  • project_root_dir
    • django_project
    • vite_project

A management command is available to configure some Vitejs frontends compilation options and commands. First create a frontend in the parent folder of the Django project with a command like:

yarn create vite frontend --template=vue-ts

Or use and existing one.

The root directory can be configured by a setting. By default it is the parent directory of the Django's BASE_DIR, like in the file structure shown above. Example setting to put the frontend dev code directly in the django project directory:

VV_BASE_DIR = BASE_DIR

Generate the Vitejs config

If the Vite app project folder is named frontend the command can run without arguments:

python {django_project}/manage.py viteconf

Otherwise add the app folder name as an argument:

python {django_project}/manage.py viteconf --app=my_frontend_app_folder_name

This command will do the following things:

  • Generate compilation options for the vite.config.js or vite.config.ts file
  • Generate npm build commands for package.json
  • Check if all the required npm dependencies are installed

The command runs in dry mode and outputs the config. To write to config files use the -w flag:

python {django_project}/manage.py viteconf -w

Options

Configure templates and static files destination

The npm build command will be configured to output to the Django static file folder and an index.html template. To change these options use these command flags:

--template=mytemplate.html: the template to compile to. Relative to the django templates dir --static=myfolder: the static folder to compile assets to. Relative to the first staticfiles dir

Example to compile the template to templates/myapp/mytemplate.html and the static assets to static/myapp:

python {django_project}/manage.py viteconf --template=myapp/mytemplate.html --static=myapp

Compile to a partial template

By default it will compile a full index page, in single page app mode. It is possible to compile to a static partial template, without the html tags. Use the partial flag:

-p: the template will not have html tags and can be included in a parent Django template

To configure Vitejs to compile an app in partial mode to a specific template and static folder:

python {django_project}/manage.py viteconf -p --app=partialapp --template=mytemplate.html --static=myfolder

Typescript models

Generate Typescript models from Django models

The tsmodels command can generate Typescript models from Django models:

python {django_project}/manage.py tsmodels my_django_app

To write the models to the frontend app:

python {django_project}/manage.py tsmodels my_django_app -w
Example output:

These Django models:

class Market(models.Model):
    name = models.CharField(max_length=255)

class Instrument(models.Model):
    name = models.CharField(max_length=255)

class Trade(models.Model):
    date = models.DateTimeField()
    price = models.FloatField()
    quantity = models.FloatField()
    market = models.ForeignKey(Market, on_delete=models.CASCADE)
    instrument = models.ForeignKey(Instrument, on_delete=models.CASCADE)
    side = models.CharField(max_length=4, choices=SIDE)

Output these Typescript models:

// Model Market

import MarketContract from "./contract";

export default class Market {
	id: number;
	name: string;

	constructor ({id, name}: MarketContract) {
		this.id=id;
		this.name=name
	}

	static fromJson(data: Record<string, any>): Market {
		return new Market(data as MarketContract)
	}
}

// -------------- Interface -------------- 

export default interface MarketContract {
	id: number,
	name: string,
}

// Model Instrument

import InstrumentContract from "./contract";

export default class Instrument {
	id: number;
	name: string;

	constructor ({id, name}: InstrumentContract) {
		this.id=id;
		this.name=name
	}

	static fromJson(data: Record<string, any>): Instrument {
		return new Instrument(data as InstrumentContract)
	}
}

// -------------- Interface -------------- 

export default interface InstrumentContract {
	id: number,
	name: string,
}

// Model Trade

import MarketContract from "../market/contract";
import InstrumentContract from "../instrument/contract";
import TradeContract from "./contract";

export default class Trade {
	id: number;
	date: string;
	price: number;
	quantity: number;
	market: MarketContract;
	instrument: InstrumentContract;
	side: string;

	constructor ({id, date, price, quantity, market, instrument, side}: TradeContract) {
		this.id=id;
		this.date=date;
		this.price=price;
		this.quantity=quantity;
		this.market=market;
		this.instrument=instrument;
		this.side=side
	}

	static fromJson(data: Record<string, any>): Trade {
		return new Trade(data as TradeContract)
	}
}

// -------------- Interface -------------- 

import MarketContract from "../market/contract";
import InstrumentContract from "../instrument/contract";

export default interface TradeContract {
	id: number,
	date: string,
	price: number,
	quantity: number,
	market: MarketContract,
	instrument: InstrumentContract,
	side: string,
}

Add an api to the generated frontend models

To scaffold an api for an existing frontend model:

python {django_project}/manage.py tsapi my_django_app_name

This will create an api for the Typescript models and copy an api helper in the frontend src directory

Example output

Methods will be added to models. Ex:

export default class Market {
	// ...

	static async load(id: number | string): Promise<Market> {
		const res = await api.get<Record<string, any>>(`/api/market/${id}/`);
		return Market.fromJson(res)
	}
}

Login views

Some login/logout views are available from the backend, and supported by the frontend api helper class. Add the urls in urls.py:

urlpatterns = [
    path("vv/", include("vv.urls")),
		#...
]

Two api views will be available: /vv/auth/login/ and /vv/auth/logout/. The frontend api helper class have support for these views example code

Example

Example repository: https://github.com/synw/django-vitevue-example

Run the tests

Clone and run:

make install
make test-initial

To run the code quality checker install Pycheck and run:

make quality
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].