Skip to content

Commit 9e29e02

Browse files
authored
Merge pull request #121 from anjlapastora/master
Add VUEJS to Django guide
2 parents e9500ed + dc11b97 commit 9e29e02

File tree

4 files changed

+808
-0
lines changed

4 files changed

+808
-0
lines changed

_sidebar.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
- [Creating Users module](django/04_create_users_module.md)
5858
- [Creating Users endpoints](django/05_endpoints.md)
5959
- [Creating Questions module](django/06_create_questions_module.md)
60+
- [Creating the frontend of the application](django/07_create_frontend.md)
61+
- [Adding VueJS to the application](django/08_add_vue.md)
62+
- [Adding VueJS to the application (continuation)](django/09_vue_cont.md)
6063

6164
- Resources
6265
- [References](resources/references.md)

django/07_create_frontend.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## Goals
2+
- [ ] Create a Registration Link on the Login Page
3+
- [ ] Create a Login Link on the Registration Page
4+
- [ ] Adding Margins to Layout
5+
- [ ] Adding a Border
6+
7+
## Create a Registration Link on the Login Page
8+
Yay! We are done setting-up the backend of our application! Now, we will be focusing on the front-end side of the app. We want to make it more `user-friendly` that is why we are going to modify or add a few things.
9+
10+
Open `templates/registration/login.html`. Add this line of code after `<h1>Login<h1>`:
11+
12+
```
13+
<p class="lead text-muted">Share Your Knowledge!</p>
14+
```
15+
16+
Also, add the registration link inside the class `login-form-container` after the `form` tag:
17+
18+
```
19+
<p class="mt-2">Or <a href="{% url 'django_registration_register' %}">Create an Account</a></p>
20+
```
21+
22+
The code should look like this:
23+
```
24+
{% extends 'auth_layout.html' %}
25+
{% load crispy_forms_tags %}
26+
27+
{% block content %}
28+
<h1>Login</h1>
29+
<p class="lead text-muted">Share Your Knowledge!</p>
30+
<div class="login-form-container">
31+
<form method="POST">
32+
{% csrf_token %}
33+
{{ form | crispy }}
34+
<button type="submit" class="btn btn-sm btn-outline-primary">Login</button>
35+
</form>
36+
<p class="mt-2">Or <a href="{% url 'django_registration_register' %}">Create an Account</a></p>
37+
</div>
38+
{% endblock %}
39+
```
40+
41+
Go to url `http://localhost:8000/accounts/login/` and see the changes.
42+
43+
## Create a Login Link on the Registration Page
44+
In this part, we are going to do the same steps. In `templates/django_registration/registration_form.html`, add this code inside the class `registration-form-container`, under the `form` tag.
45+
46+
```
47+
<p class="mt-2">Or <a href="{% url 'login' %}">Login</a></p>
48+
```
49+
The code should look like this:
50+
```
51+
{% extends 'auth_layout.html' %}
52+
{% load crispy_forms_tags %}
53+
54+
{% block content %}
55+
<h1>Create Your Account</h1>
56+
<div class="registration-form-container">
57+
<form method="POST">
58+
{% csrf_token %}
59+
{{ form | crispy }}
60+
<button type="submit" class="btn btn-sm btn-outline-primary">Create Account</button>
61+
</form>
62+
<p class="mt-2">Or <a href="{% url 'login' %}">Login</a></p>
63+
</div>
64+
{% endblock %}
65+
```
66+
67+
Then check the changes by accessing this url: `http://localhost:8000/accounts/register/`.
68+
69+
## Adding Margins to Layout
70+
In this part, we are going to add some margins to our `registration` page and `login` page.
71+
In `templates/auth_layout.html`, add a css class called `outer-area`. The style would look as follows:
72+
```
73+
.outer-area {
74+
margin-top: 100px;
75+
}
76+
```
77+
Then add a new div tag outside the div class `auth-box text-center`. In the newly added div tag, call the `outer-area` class. It should look like this:
78+
79+
```
80+
<body>
81+
82+
<div class="outer-area">
83+
<div class="auth-box text-center">
84+
{% block content %}
85+
{% endblock %}
86+
</div>
87+
</div>
88+
89+
</body>
90+
```
91+
92+
## Adding a Border
93+
To add a border on registration and login page, add a css class named `auth-box`. Then add these other details:
94+
```
95+
.auth-box {
96+
border: 3px solid lightgray;
97+
border-radius: 10px;
98+
padding-top: 25px;
99+
padding-bottom: 25px;
100+
width: 600px;
101+
margin: auto;
102+
}
103+
```

django/08_add_vue.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
## Goals
2+
- [ ] Install NPM
3+
- [ ] Install VueCLI
4+
- [ ] Add Vue to application
5+
6+
## Install NPM
7+
Go to `https://nodejs.org/en/download/`, download the latest or the LTS version of Node. Install node with the recommended settings. Check if Node and NPM is installed by checking in your terminal:
8+
9+
```
10+
> npm --version
11+
6.14.14
12+
> node --version
13+
v14.17.4
14+
```
15+
16+
## Install VueCLI
17+
Once the NodeJS has been installed in your computer, you may now install the VueCLI. In your terminal, type this command:
18+
```
19+
sudo npm i -g @vue/cli
20+
```
21+
Then test if its working by typing
22+
```
23+
vue create hello-vue
24+
Vue CLI v4.5.13
25+
? Please pick a preset: Manually select features
26+
? Check the features needed for your project: Choose Vue version, Babel, Router, Linter
27+
? Choose a version of Vue.js that you want to start the project with 2.x
28+
? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
29+
? Pick a linter / formatter config: Prettier
30+
? Pick additional lint features: Lint on save
31+
? Where do you prefer placing config for Babel, ESLint, etc.? In package.json
32+
? Save this as a preset for future projects? (y/N) N
33+
```
34+
35+
Wait for a few minutes, then once completed, will show a prompt like this:
36+
37+
```
38+
📄 Generating README.md...
39+
40+
🎉 Successfully created project hello-vue2.
41+
👉 Get started with the following commands:
42+
43+
$ cd hello-vue
44+
$ npm run serve
45+
```
46+
47+
Go to `hello-vue` and type `npm run serve`. It will remind you to access to `http://localhost:<port>/`. Go to that url and check if Vue is showing. This indicates that Vue is properly installed to your computer.
48+
49+
50+
## Add Vue to application
51+
We are going to create a simple Vue project in our Django project. Ensure that the project name is `frontend`, like so:
52+
```
53+
vue create frontend
54+
```
55+
56+
Set the config like the ones below:
57+
```
58+
vue create hello-vue
59+
Vue CLI v4.5.13
60+
? Please pick a preset: Manually select features
61+
? Check the features needed for your project: Choose Vue version, Babel, Router, Linter
62+
? Choose a version of Vue.js that you want to start the project with 2.x
63+
? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
64+
? Pick a linter / formatter config: Prettier
65+
? Pick additional lint features: Lint on save
66+
? Where do you prefer placing config for Babel, ESLint, etc.? In package.json
67+
? Save this as a preset for future projects? (y/N) N
68+
```
69+
70+
Then, we're going to install a package `webpack-bundle-tracker` to keep track of the code in our frontend application.
71+
Go to `frontend` folder then install the webpack tracker:
72+
```
73+
cd frontend
74+
75+
npm install --save-dev webpack-bundle-tracker@0.4.3
76+
```
77+
78+
Next, create a file inside the folder `frontend`. Name it `vue.config.js`.
79+
```
80+
const BundleTracker = require("webpack-bundle-tracker");
81+
82+
module.exports = {
83+
// on Windows you might want to set publicPath: "http://127.0.0.1:8080/"
84+
publicPath: "http://127.0.0.1:8081/",
85+
outputDir: './dist/',
86+
87+
chainWebpack: config => {
88+
89+
config
90+
.plugin('BundleTracker')
91+
.use(BundleTracker, [{filename: './webpack-stats.json'}])
92+
93+
config.output
94+
.filename('bundle.js')
95+
96+
config.optimization
97+
.splitChunks(false)
98+
99+
config.resolve.alias
100+
.set('__STATIC__', 'static')
101+
102+
config.devServer
103+
// the first 3 lines of the following code have been added to the configuration
104+
.public('http://127.0.0.1:8080')
105+
.host('127.0.0.1')
106+
.port(8080)
107+
.hotOnly(true)
108+
.watchOptions({poll: 1000})
109+
.https(false)
110+
.disableHostCheck(true)
111+
.headers({"Access-Control-Allow-Origin": ["\*"]})
112+
113+
},
114+
115+
// uncomment before executing 'npm run build'
116+
// css: {
117+
// extract: {
118+
// filename: 'bundle.css',
119+
// chunkFilename: 'bundle.css',
120+
// },
121+
// }
122+
123+
};
124+
125+
```
126+
127+
Next, we are going to install a webpack loader in Django environment. This will help us read the changes given by the bundle tracker.
128+
```
129+
pip install django-webpack-loader==0.7.0
130+
```
131+
132+
Update the requirements.txt file.
133+
```
134+
pip freeze > requirements.txt
135+
```
136+
137+
Then add the webpack_loader library to `settings.py`.
138+
```
139+
INSTALLED_APPS = [
140+
'webpack_loader',
141+
]
142+
143+
// on the bottom of settings.py file
144+
WEBPACK_LOADER = {
145+
'DEFAULT': {
146+
'BUNDLE_DIR_NAME': 'dist/',
147+
'STATS_FILE': BASE_DIR / 'frontend' / 'webpack-stats.json'
148+
}
149+
}
150+
```
151+
152+
Add this code inside the file `templates/index.html`, on the first line of the code:
153+
```
154+
{% load render_bundle from webpack_loader %}
155+
```
156+
Also, add this inside the body of the code, after the div id app:
157+
```
158+
{% render_bundle 'app' %}
159+
```
160+
161+
The index file should look this this:
162+
```
163+
{% load render_bundle from webpack_loader %}
164+
<!DOCTYPE html>
165+
<html lang="en">
166+
<head>
167+
<meta charset="UTF-8">
168+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
169+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
170+
<title>Forum App</title>
171+
</head>
172+
<body>
173+
174+
<h1> Vue JS</h1>
175+
176+
<div id="app"></div>
177+
178+
{% render_bundle 'app' %}
179+
180+
</body>
181+
</html>
182+
```
183+
184+
Run server of both django and vue separately.
185+
```
186+
python manage.py runserver
187+
188+
npm run serve
189+
```
190+
191+
192+
(Optional) You may also explore the vue project by typing this command.
193+
```
194+
vue ui
195+
```

0 commit comments

Comments
 (0)