-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSetup.txt
More file actions
389 lines (304 loc) · 12.9 KB
/
Setup.txt
File metadata and controls
389 lines (304 loc) · 12.9 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
================================================================
VOTING WEB APPLICATION — SETUP GUIDE
Django (Backend) + React (Frontend)
================================================================
WHAT IS THIS PROJECT?
---------------------
A full-stack digital voting application with 3 roles:
- Admin : Creates elections, manages voters and candidates
- Voter : Registers, logs in, and casts votes
- Candidate : Views their election stats and manages profile
================================================================
STEP 1 — INSTALL REQUIRED SOFTWARE (do this first)
================================================================
Before anything, make sure these are installed on your PC:
1. Python 3.10 or higher
Download from: https://www.python.org/downloads/
During install: CHECK the box "Add Python to PATH"
2. Node.js 18 or higher
Download from: https://nodejs.org/
Download the LTS version
3. VS Code (recommended editor)
Download from: https://code.visualstudio.com/
To verify everything is installed, open PowerShell and run:
python --version (should show 3.10+)
node --version (should show v18+)
npm --version (should show 9+)
================================================================
STEP 2 — EXTRACT THE PROJECT
================================================================
1. Extract the ZIP file to any folder on your PC
Example: C:\Projects\voting\
2. After extracting you should see:
voting\
backend\
frontend\
README.txt
================================================================
STEP 3 — SETUP THE BACKEND (Django)
================================================================
Open PowerShell and run these commands ONE BY ONE:
--- Navigate to backend folder ---
cd C:\Projects\voting\backend
(change this path to wherever you extracted the project)
--- Create virtual environment ---
python -m venv .venv
--- Activate virtual environment ---
.venv\Scripts\activate
You should see (.venv) appear at the start of the line.
This means the virtual environment is active.
--- Install all Python packages ---
pip install django djangorestframework djangorestframework-simplejwt django-cors-headers pillow
--- Create missing files (run each block separately) ---
Copy and run this in PowerShell to create accounts/serializers.py:
$s = @'
from rest_framework import serializers
from django.contrib.auth.password_validation import validate_password
from .models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id','username','email','first_name','last_name','role','phone','photo','voter_id','is_verified','bio']
read_only_fields = ['id','role','is_verified']
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password2 = serializers.CharField(write_only=True, required=True)
class Meta:
model = User
fields = ['username','email','first_name','last_name','password','password2','role','phone']
def validate(self, attrs):
if attrs['password'] != attrs['password2']:
raise serializers.ValidationError({"password": "Passwords do not match."})
return attrs
def create(self, validated_data):
validated_data.pop('password2')
password = validated_data.pop('password')
if validated_data.get('role') == 'admin':
validated_data['role'] = 'voter'
user = User(**validated_data)
user.set_password(password)
user.save()
return user
class ChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True, validators=[validate_password])
'@
$s | Out-File -FilePath "accounts\serializers.py" -Encoding UTF8
Write-Host "accounts/serializers.py created!"
Copy and run this to create elections/serializers.py:
$s2 = @'
from rest_framework import serializers
from .models import Election, Candidate, Vote
from accounts.models import User
class UserMiniSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id','username','first_name','last_name','photo']
class CandidateSerializer(serializers.ModelSerializer):
user = UserMiniSerializer(read_only=True)
user_id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), source='user', write_only=True)
vote_count = serializers.ReadOnlyField()
class Meta:
model = Candidate
fields = ['id','election','user','user_id','party','manifesto','photo','vote_count']
class ElectionSerializer(serializers.ModelSerializer):
candidates = CandidateSerializer(many=True, read_only=True)
total_votes = serializers.ReadOnlyField()
created_by = UserMiniSerializer(read_only=True)
class Meta:
model = Election
fields = ['id','title','description','election_type','status','start_date','end_date','banner','candidates','total_votes','created_by','created_at']
read_only_fields = ['created_by','created_at']
def create(self, validated_data):
validated_data['created_by'] = self.context['request'].user
return super().create(validated_data)
class VoteSerializer(serializers.ModelSerializer):
class Meta:
model = Vote
fields = ['id','candidate','election','timestamp']
read_only_fields = ['timestamp']
'@
$s2 | Out-File -FilePath "elections\serializers.py" -Encoding UTF8
Write-Host "elections/serializers.py created!"
--- Run database migrations ---
python manage.py makemigrations accounts
python manage.py makemigrations elections
python manage.py migrate
--- Create the Admin account ---
python manage.py createsuperuser
It will ask for:
Username : choose any (e.g. admin)
Email : any email (e.g. admin@vote.com)
Password : choose a strong password (min 8 chars)
Remember these — you will use them to log in as Admin!
--- Set the admin role (IMPORTANT) ---
python manage.py shell
Then type these lines one by one:
from accounts.models import User
u = User.objects.get(username='admin')
u.role = 'admin'
u.is_staff = True
u.is_superuser = True
u.save()
print("Role set to:", u.role)
exit()
Replace 'admin' with whatever username you chose above.
--- Start the Django server ---
python manage.py runserver
Keep this terminal open and running!
Django is now running at: http://127.0.0.1:8000
================================================================
STEP 4 — SETUP THE FRONTEND (React)
================================================================
Open a NEW PowerShell window (keep the Django one running).
--- Navigate to frontend folder ---
cd C:\Projects\voting\frontend
(change path to match where you extracted the project)
--- Install Node packages ---
npm install
--- Start the React server ---
npm run dev
Keep this terminal open and running!
React is now running at: http://localhost:5173
================================================================
STEP 5 — OPEN THE APP IN BROWSER
================================================================
Open your browser and go to:
Main App → http://localhost:5173
Login Page → http://localhost:5173/login
Register → http://localhost:5173/register
Django Admin Panel → http://127.0.0.1:8000/admin
(use the superuser username and password you created)
================================================================
STEP 6 — HOW TO USE THE APP
================================================================
AS ADMIN:
1. Go to http://localhost:5173/login
2. Click the "Admin" tab
3. Enter your superuser username and password
4. You will be taken to the Admin Dashboard
5. Create elections, add candidates, manage voters
AS VOTER:
1. Go to http://localhost:5173/register
2. Select "Voter" and fill in the form
3. Ask the Admin to verify your account
4. Log in and vote in active elections
AS CANDIDATE:
1. Go to http://localhost:5173/register
2. Select "Candidate" and fill in the form
3. Admin adds you to an election
4. Log in and view your stats
================================================================
EVERY DAY — HOW TO RUN THE PROJECT
================================================================
You only need to do Steps 3-4 once.
Every day after that, just do this:
Terminal 1 (Backend):
cd C:\Projects\voting\backend
.venv\Scripts\activate
python manage.py runserver
Terminal 2 (Frontend):
cd C:\Projects\voting\frontend
npm run dev
Then open: http://localhost:5173
================================================================
COMMON ERRORS AND FIXES
================================================================
ERROR: ModuleNotFoundError: No module named 'rest_framework'
FIX: Your virtual environment is not activated.
Run: .venv\Scripts\activate
Then: pip install djangorestframework
ERROR: ModuleNotFoundError: No module named 'accounts.serializers'
FIX: The serializers.py file is missing.
Run the PowerShell commands in Step 3 above.
ERROR: django.db.utils.OperationalError: no such table
FIX: Migrations haven't been run yet.
Run: python manage.py makemigrations
python manage.py migrate
ERROR: CORS error / Network Error in browser console
FIX: Open backend/voting_backend/settings.py
Make sure this line exists:
CORS_ALLOWED_ORIGINS = ['http://localhost:5173']
ERROR: 404 Not Found on /api/auth/register/
FIX: Open backend/voting_backend/urls.py
Make sure these lines are NOT commented out:
path('api/auth/', include('accounts.urls')),
path('api/elections/', include('elections.urls')),
ERROR: White screen on http://localhost:5173
FIX: Open browser, press F12, go to Console tab.
Check for red errors.
Most likely fix: cd frontend && npm install
ERROR: Port 8000 is already in use
FIX: Run Django on a different port:
python manage.py runserver 8001
Then open frontend/src/api/axios.js and change:
baseURL: 'http://localhost:8001/api'
ERROR: Admin login not working
FIX: The admin role may not be set. Run:
python manage.py shell
>>> from accounts.models import User
>>> u = User.objects.get(username='your_username')
>>> u.role = 'admin'
>>> u.save()
>>> exit()
ERROR: npm run dev gives "vite not found"
FIX: cd frontend
npm install
npm run dev
================================================================
PROJECT STRUCTURE (for reference)
================================================================
voting/
├── README.txt ← this file
├── backend/ ← Django REST API
│ ├── manage.py
│ ├── requirements.txt
│ ├── voting_backend/ ← project settings
│ │ ├── settings.py
│ │ └── urls.py
│ ├── accounts/ ← user auth app
│ │ ├── models.py
│ │ ├── serializers.py
│ │ ├── views.py
│ │ └── urls.py
│ └── elections/ ← voting logic app
│ ├── models.py
│ ├── serializers.py
│ ├── views.py
│ ├── urls.py
│ └── permissions.py
└── frontend/ ← React app
├── src/
│ ├── App.jsx
│ ├── main.jsx
│ ├── index.css
│ ├── api/axios.js
│ ├── store/authStore.js
│ ├── components/layouts/
│ └── pages/
│ ├── auth/
│ ├── admin/
│ ├── voter/
│ └── candidate/
└── package.json
================================================================
TECH STACK
================================================================
Backend : Python 3.12 + Django 5 + Django REST Framework
Auth : JWT (JSON Web Tokens) via djangorestframework-simplejwt
Database : SQLite (file-based, no extra setup needed)
Frontend : React 18 + Vite + plain CSS
State : Zustand
HTTP : Axios
================================================================
NEED HELP?
================================================================
If you get stuck, check the error message carefully.
Most errors are caused by:
1. Virtual environment not activated (.venv\Scripts\activate)
2. Missing serializers.py file (run the PowerShell commands)
3. Migrations not run (python manage.py migrate)
4. Both servers not running at the same time
================================================================
GOOD LUCK!
================================================================