-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_page_working_code.txt
105 lines (84 loc) · 1.68 KB
/
home_page_working_code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## Start with:
1. activate venv
2. cd blogs
3. python manage.py shell
## In the Django Shell, run these commands:
`
from boards.models import Board
Board.objects.all()
`
## Insert following code to models.py/Boards class:
`
def __str__(self):
return self.name
`
## QuerySet iteration:
### Get All Boards
`
boards_list = Board.objects.all()
for board in boards_list:
print(board.description)
`
### Get Boards by Id
`
django_board = Board.objects.get(id=1)
django_board.name
django_board.description
`
### throws error if id not found:
`
django_board = Board.objects.get(id=3)
`
## Add following code in boards/views.py
`
from .models import Board
def home(request):
boards = Board.objects.all()
boards_names = []
for board in boards:
boards_names.append(board.name)
response_html = '<br>'.join(boards_names)
return HttpResponse(response_html)
`
## Test Base_Dir path:
`
python manage.py shell
from django.conf import settings
settings.BASE_DIR
`
## ADD the following html codes to home.html:
`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Boards</title>
</head>
<body>
<h1>Boards</h1>
<table border="1">
<thead>
<tr>
<th>Board</th>
<th>Posts</th>
<th>Topics</th>
<th>Last Post</th>
</tr>
</thead>
<tbody>
{% for board in boards %}
<tr>
<td>
{{ board.name }}<br>
<small style="color: #888">{{ board.description }}</small>
</td>
<td>0</td>
<td>0</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
`