Code Monkey home page Code Monkey logo

django-with-vuejs's Introduction

django-with-vuejs

Fast and clear in DevOps. 中文 README.md

Simple is better.

Why Django Project dir and Frontend dir are the same level in example/django-auth-with-react dir,but it's not in project dir?

That is because the django-auth-with-react project is completely a front-end separation project.

Step 1: Install frontend dependencies

In the directory where the package.json file is located

cd django-with-vuejs/project/vue2_frontend
npm install

Step 2: Build the front end

In the frontend directory

npm run build

Step 3: Start the project with Django's own server

In the directory where manage.py is located

pip install -r requirements.txt
python manage.py runserver

All done.


but if you want to know the principle, keep reading

Principle

  • index.html
  • static/*

First. index.html

Handle the index.html file with django template engines

In settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # 'DIRS': [],
        'DIRS': ['vue2_frontend/dist'],
        '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',
            ],
        },
    },
]

We change the TEMPLATES.DIRS so that django template engines know where to find the index.html.

Second. static/*

In settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "vue2_frontend/dist/static"),
]

We add STATICFILES_DIRS setting in settings.py, this can make django find the resource

<script type=text/javascript src=/static/js/vendor.677ef0c9485c32b4f6a9.js></script>

in vue2_frontend/dist/static directory, but it worked only in debug mode.


Production

We use Nginx to handle the frontend:

  • index.html
  • static/*

Django only handle the API which Content-Type is application/json.


Any questions?

New issues

django-with-vuejs's People

Contributors

tmpbook avatar raincle avatar timtech4u avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.