DEV Community

Cover image for Fullstack? Why not (Django + Vue-js) - episode 2
aderayevans
aderayevans

Posted on

2 2

Fullstack? Why not (Django + Vue-js) - episode 2

Table of Contents

  1. Django side
  2. Vue.js side

Implement episode 2


Django side


{projectname}/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('shark/', include('shark.urls')),
]
Enter fullscreen mode Exit fullscreen mode

shark/urls.py

from django.urls import path
from . import views

app_name = 'shark'
urlpatterns = [
    # path('', views.IndexView.as_view(), name='index'),
    path('', views.index, name='index'),
]
Enter fullscreen mode Exit fullscreen mode

shark/views.py

from urllib import response
from django.shortcuts import render
from django.views import generic
from django.http import HttpResponse
from .models import mShark
from django.http import JsonResponse

def index(request):
    responseData = {
        'id': 4,
        'name': 'Test Response',
        'roles' : ['Admin','User']
    }

    return JsonResponse(responseData)
    # return HttpResponse("return this string")

# Create your views here.
# class IndexView(generic.ListView):
#     # template_name = 'shark/index.html'
#     model = mShark

#     def get_queryset(self):
#         return mShark.getIntro(self)
#         # return HttpResponse("return this string")
Enter fullscreen mode Exit fullscreen mode

shark/models.py

from django.db import models

# Create your models here.
class mShark(models.Model):
    intro = "Hello, This is Shark from Django"

    def getIntro(self) -> set[str]:
        return { mShark.intro }
Enter fullscreen mode Exit fullscreen mode

Vue.js side


frontend/src/router/index.js or .ts

import Vue from "vue";
import VueRouter from "vue-router";
import Shark from "../components/Shark.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/shark",
    name: "Shark",
    component: Shark,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

export default router;

Enter fullscreen mode Exit fullscreen mode
  • Create new file frontend/src/components/Shark.vue
<template>
  <div>
    <p>{{ msg }}</p>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "vShark",
  data() {
    return {
      msg: "",
    };
  },
  methods: {
    getResponse() {
      const path = "http://127.0.0.1:8000/shark/";
      axios
        .get(path)
        .then((res) => {
          console.log(res.data);
          this.msg = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  created() {
    this.getResponse();
  },
};
</script>

Enter fullscreen mode Exit fullscreen mode

Now everytime we go to path http://localhost:8080/shark which is frontend side

Vue will ask the server at http://127.0.0.1:8000/shark/ and get the response
Then print all the messages in the frontend web page

first_result

Additional commands:

  • Run npm run lint --fix to fix linting errors (recommend setting Prettier extension when using VSCode) npm run lint --fix

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay