Skip to content

Commit eddfe0a

Browse files
Create Lab.md
1 parent e97b2e5 commit eddfe0a

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

Lab.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
```python
3+
pip install django
4+
```
5+
6+
```python
7+
django-admin startproject myproject
8+
cd myproject
9+
python manage.py startapp myapp
10+
11+
```
12+
13+
2. Configure settings.py
14+
In myproject/settings.py, add myapp to INSTALLED_APPS:
15+
16+
```python
17+
INSTALLED_APPS = [
18+
'django.contrib.admin',
19+
'django.contrib.auth',
20+
'django.contrib.contenttypes',
21+
'django.contrib.sessions',
22+
'django.contrib.messages',
23+
'django.contrib.staticfiles',
24+
'myapp', # Add this line
25+
]
26+
27+
```
28+
29+
3. Create a View in myapp/views.py
30+
```python
31+
32+
from django.shortcuts import render
33+
34+
def home(request):
35+
users = [
36+
{"name": "Alice", "age": 25},
37+
{"name": "Bob", "age": 30},
38+
{"name": "Charlie", "age": 22},
39+
]
40+
return render(request, "index.html", {"title": "User List", "users": users})
41+
```
42+
43+
4. Set Up URLs
44+
Modify myproject/urls.py:
45+
46+
```python
47+
from django.contrib import admin
48+
from django.urls import path, include
49+
50+
urlpatterns = [
51+
path('admin/', admin.site.urls),
52+
path('', include('myapp.urls')), # Include app URLs
53+
]
54+
55+
56+
```
57+
58+
Create myapp/urls.py:
59+
```python
60+
61+
from django.urls import path
62+
from .views import home
63+
64+
urlpatterns = [
65+
path('', home, name='home'),
66+
]
67+
68+
```
69+
70+
71+
5. Create a Template
72+
Create a folder templates/ inside myapp/, then add index.html.
73+
74+
File: myapp/templates/index.html
75+
76+
```python
77+
<!DOCTYPE html>
78+
<html lang="en">
79+
<head>
80+
<meta charset="UTF-8">
81+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
82+
<title>{{ title }}</title>
83+
</head>
84+
<body>
85+
<h1>{{ title }}</h1>
86+
<ul>
87+
{% for user in users %}
88+
<li>{{ user.name }} (Age: {{ user.age }})</li>
89+
{% endfor %}
90+
</ul>
91+
</body>
92+
</html>
93+
94+
```
95+
96+
2. Configure TEMPLATES in settings.py
97+
Open myproject/settings.py and update the TEMPLATES setting:
98+
```python
99+
import os
100+
101+
TEMPLATES = [
102+
{
103+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
104+
'DIRS': [os.path.join(BASE_DIR, 'myapp/templates')], # Add this line
105+
'APP_DIRS': True,
106+
'OPTIONS': {
107+
'context_processors': [
108+
'django.template.context_processors.debug',
109+
'django.template.context_processors.request',
110+
'django.contrib.auth.context_processors.auth',
111+
'django.contrib.messages.context_processors.messages',
112+
],
113+
},
114+
},
115+
]
116+
```
117+
118+
6. Run the Django Server
119+
Apply migrations and start the server:
120+
```python
121+
python manage.py migrate
122+
python manage.py runserver
123+
```

0 commit comments

Comments
 (0)