· 2 years ago · May 16, 2023, 04:20 PM
1"""
2
31.Manage.py + command: startsapp 'name of the app'.
42.Move the app to the main project dir.
53.Create new py-file(urls.py) in the app folder -> urlpatterns = () ->(must be tuple! or list!).
64.In settings.py add the app in the 'INSTALLED_APPS'.
75.Add path to main project urlpatterns -> path('', include('folder_name.my_app_name.urls')),
86.Run app to see if everything works?.
9
107.Insert the templates in the projects dir
118.Create more folders with names like -> for example: core/profile/items and move the templates into.
12
139.Create a new folder inside the main projects dir with name 'staticfiles'.
1410.In settings.py add additional setting for static dir ->
15 STATICFILES_DIRS = (
16 BASE_DIR / 'staticfiles',
17 )
18
1911.test to run one of the staticfiles to test if works fine
20 for example-> http://127.0.0.1:8000/static/styles/details.css
21
2212.In settings.py config database
23 -THIS CAN BE DONE IN THE END OF THE TASK(django got default db(sqlite3) connected and works same way!
24 with postgres settings ->
25 https://docs.djangoproject.com/en/4.2/ref/settings/#databases
26
27 DATABASES = {
28 "default": {
29 "ENGINE": "django.db.backends.postgresql",
30 "NAME": "name of the database",
31 "USER": "postgres user",
32 "PASSWORD": "mypassword",
33 "HOST": "127.0.0.1",
34 "PORT": "5432",
35 }
36 }
37
3813.Top right corner Database menu -> add new -> Data Source -> PostgreSQL -> fill username and password
39 then test the connection and connect.
40 This is default and do not change it.
41 Must be like this ->
42 Host: localhost
43 Port: 5432
44 URL: jdbc:postgresql://localhost:5432/postgres
45
46 Now right mouse click on the postgres@localhost and create a new database
47 with the correct db base from the settings.
48
4914.Now start the app -> may be you will see error ->
50
51 "raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
52 django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module"
53
5415.Need to install "psycopg" or "psycopg2" to work correctly.
5516.In terminal execute this -> pip install psycopg2
56
5717.After EVERY! pip install is good to execute-> pip freeze - requiments.txt
58 This will create a txt file in your main projects dir
59 with name-> 'requiments.txt'
60 looking like this ->
61 asgiref==3.6.0
62 Django==4.2.1
63 psycopg2==2.9.6
64 sqlparse==0.4.4
65 tzdata==2023.3
66
6718.You can upgrade your pip -> 'pip install --upgrade pip' (for Windows).
68
6919.Execute in terminal -> "python manage.py migrate"
70 or
71 in manage.py window(run it with-> "CTRL+ALT+R") -> "migrate"
72 its the same , but in manage.py window you have autocomplete and it`s easier.
73 Now you can see in your DB the tables filled in. In "db/public/tables"
74
7520.Run to see if everything works well.Good job :D !
76
7721.Next step is to add urls paths and config with callable func-views.
78 for example->
79
80 http://localhost:8000/ - home page
81 http://localhost:8000/dashboard/ - dashboard page
82
83 http://localhost:8000/game/create/ - create game page
84 http://localhost:8000/game/details/<id>/ - details game page
85 http://localhost:8000/game/edit/<id>/ - edit game page
86 http://localhost:8000/game/delete/<id>/ - delete game page
87
88 http://localhost:8000/profile/create - create profile page
89 http://localhost:8000/profile/details/ - details profile page
90 http://localhost:8000/profile/edit/ - edit profile page
91 http://localhost:8000/profile/delete/ - delete profile page
92
93
94 In urls.py looking like this ->
95
96 urlpatterns = (
97 path('', show_index, name='show index'),
98 path('dashboard/', show_dashboard, name='show dashboard'),
99
100 path('profile/', include([
101 path('create/', create_profile, name='create profile'),
102 path('details/', details_profile, name='details profile'),
103 path('edit/', edit_profile, name='edit profile'),
104 path('delete/', delete_profile, name='delete profile'),
105 ])),
106
107 path('game/', include([
108 path('create/', create_game, name='create game'),
109 path('details/<int:pk>/', details_game, name='details game'),
110 path('edit/<int:pk>/', edit_game, name='edit game'),
111 path('delete/<int:pk>/', delete_game, name='delete game'),
112 ])),
113 )
114
115 now in views.py ->
116
117
118 open -> http://localhost:8000/
119 -this is without PK/ID->
120 def show_index(request):
121 return render(request, 'core/home-page.html')
122
123
124 open -> http://localhost:8000/game/details/1/
125 -And this is with PK/ID->
126 def details_game(request, pk):
127 return render(request, 'game/details-game.html')
128
129
130 Run to try if working well.
131 The pages must visualization without any functionality but rendering.
132
13322.Now it`s time to make a superuser(admin) You can reach the link -> http://127.0.0.1:8000/admin/ , and you will see
134 the admin panel for loging into the admin panel, but we still miss the superuser.
135 Execute in manage.py->
136 createsuperuser and follow the instructions -> fill username,mail,password,repeat password! It`s done.
137 Now you can log into admin side.You can create groups and users and giving them permissions and organize all,
138 but for now this is enough.
139
14023.Time to fix the templates using inheritance.
141 In the template directory create a single file with name -> base.html
142 This will be the main/base html who will have the base html elements like navigations and footer
143 which is repeating in every html.
144
145 Use this -> to extend the base.html
146
147 {% extends 'base.html' %}
148 {% load static %}
149 {% block content %}
150 {% endblock %}
151
152 and this in base.html to be the parent to all html elements ->
153 {% block content %}
154 {# information here #}
155 {% endblock %}
156
157
158
159 base.html must be looking something like this ->
160 base.html STARTS HERE ->
161 {% load static %}
162 <!DOCTYPE html>
163 <html lang="en">
164 <head>
165 <meta charset="UTF-8">
166 <meta name="viewport" content="width=device-width, initial-scale=1.0">
167 <link rel="stylesheet" href="{% static '/styles/style.css' %}">
168 <title>GamesPlay</title>
169 </head>
170
171 <body>
172 <div id="box">
173 <header>
174
175 <!-- Navigation Bar -->
176 <h1><a class="home" href="{% url 'show index' %}">GamesPlay</a></h1>
177 <nav>
178 <!-- if the user has not created a profile -->
179 <a href="{% url 'create profile' %}">Create Profile</a>
180 <!-- if the user has created a profile -->
181 <a href="{% url 'show dashboard' %}">Dashboard</a>
182 <a href="{% url 'create game' %}">Create Game</a>
183 <a href="{% url 'details profile' %}">Profile</a>
184 </nav>
185 </header>
186
187 {% block content %}
188 {# information here #}
189 {% endblock %}
190
191 <footer>
192 SoftUni Team 2022. All rights reserved.
193 </footer>
194
195 </div>
196 </body>
197 </html>
198 base.html ENDS HERE !
199
200
201
202
203 Example of some the html files which must be looking something like this after inheritance ->
204 home-page.html STARTS HERE ->
205
206 {% extends 'base.html' %}
207 {% load static %}
208 {% block content %}
209
210 <section id="welcome-world">
211 <div class="welcome-message">
212 <h2>ALL new games are</h2>
213 <h3>Only in GamesPlay</h3>
214 </div>
215 <img src="{% static '/images/four_slider_img01.png' %}" alt="hero">
216 </section>
217
218 {% endblock %}
219
220 home-page.html ENDS HERE !
221
222 """
223
224from pathlib import Path
225
226BASE_DIR = Path(__file__).resolve().parent.parent
227SECRET_KEY = 'django-insecure-s&yb=i*j3&w37cevb_ip&bijzi7sw^&ukc7((x9^u%+d!lqi3r'
228DEBUG = True
229ALLOWED_HOSTS = []
230
231INSTALLED_APPS = [
232 'django.contrib.admin',
233 'django.contrib.auth',
234 'django.contrib.contenttypes',
235 'django.contrib.sessions',
236 'django.contrib.messages',
237 'django.contrib.staticfiles',
238
239 'games_play_app.my_web',
240]
241
242MIDDLEWARE = [
243 'django.middleware.security.SecurityMiddleware',
244 'django.contrib.sessions.middleware.SessionMiddleware',
245 'django.middleware.common.CommonMiddleware',
246 'django.middleware.csrf.CsrfViewMiddleware',
247 'django.contrib.auth.middleware.AuthenticationMiddleware',
248 'django.contrib.messages.middleware.MessageMiddleware',
249 'django.middleware.clickjacking.XFrameOptionsMiddleware',
250]
251
252ROOT_URLCONF = 'games_play_app.urls'
253
254TEMPLATES = [
255 {
256 'BACKEND': 'django.template.backends.django.DjangoTemplates',
257 'DIRS': [BASE_DIR / 'templates']
258 ,
259 'APP_DIRS': True,
260 'OPTIONS': {
261 'context_processors': [
262 'django.template.context_processors.debug',
263 'django.template.context_processors.request',
264 'django.contrib.auth.context_processors.auth',
265 'django.contrib.messages.context_processors.messages',
266 ],
267 },
268 },
269]
270
271WSGI_APPLICATION = 'games_play_app.wsgi.application'
272
273# DATABASES = {
274# 'default': {
275# 'ENGINE': 'django.db.backends.sqlite3',
276# 'NAME': BASE_DIR / 'db.sqlite3',
277# }
278# }
279
280DATABASES = {
281 "default": {
282 "ENGINE": "django.db.backends.postgresql",
283 "NAME": "app_name_db",
284 "USER": "postgres",
285 "PASSWORD": "some_password",
286 "HOST": "127.0.0.1",
287 "PORT": "5432",
288 }
289}
290
291AUTH_PASSWORD_VALIDATORS = [
292 {
293 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
294 },
295 {
296 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
297 },
298 {
299 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
300 },
301 {
302 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
303 },
304]
305
306LANGUAGE_CODE = 'en-us'
307
308TIME_ZONE = 'UTC'
309
310USE_I18N = True
311
312USE_TZ = True
313
314STATIC_URL = 'static/'
315
316STATICFILES_DIRS = (
317 BASE_DIR / 'staticfiles',
318)
319
320DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
321