I’m working on a school scheduling app using using django 4.1.6 and python 3.10.5. I have the app working but I need the schedules to reflect the users time zone.
Basically, if a teacher from Seattle creates a schedule for 7am, then a student in Tokyo should see that schedule as 11pm. I’d like the schedules to reflect the time zone automatically so if a student or teacher leaves town and are in a different time zone than their residence (lets say their traveling for some reason), they won’t need to manually change their time zone.
I have the pytz installed and the settings.py show:
TIME_ZONE = ‘UTC’
USE_I18N = True
USE_TZ = True.
If I’m correct I should be ready to use time zones in my app. I just don’t know how to get a saved time to reflect the individual users time zone.
I’ll include the relevant code here.
models.py:
class Student(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
englishName = models.CharField(max_length=200)
studentName = models.CharField(max_length=200)
studentId = models.CharField(max_length=200)
birthday = models.DateField()
gender = models.CharField(max_length=6)
gradeLevel = models.CharField(max_length=8)
since = models.DateField()
duration = models.CharField(max_length=3)
contactNumber = models.CharField(max_length=13, default=00000000)
email = models.EmailField(max_length=200, default=‘address@email.com’)
schedules = models.ManyToManyField(‘Schedule’, blank=True)
profile_picture = models.ImageField(upload_to=‘profile_pictures/’, default=‘profile_pictures/default.jpg’)
timezone = models.CharField(max_length=50, default=timezone.get_default_timezone_name)def get_first_note_or_none(self):
try:
return self.schedule.notes.filter(student=self).first()
except AttributeError:
return Nonedef __str__(self): return self.studentName
def student_index(request):
data=Student
context = {‘form’: data}
return render(request, ‘student-information.html’, context)class Teacher(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
teacherName = models.CharField(max_length=200)
teacherId = models.CharField(max_length=200)
birthday = models.DateField()
gender = models.CharField(max_length=6)
since = models.DateField()
contactNumber = models.CharField(max_length=13, default=00000000)
email = models.EmailField(max_length=200, default=‘address@email.com’)
profile_picture = models.ImageField(upload_to=‘profile_pictures/’, default=‘profile_pictures/default.jpg’)def __str__(self): return self.teacherName
def teacher_index(request):
data=Teacher
context = {‘form’: data}
return render(request, ‘teacher-information.html’, context)
views.py:
@login_required
@allowed_users(allowed_roles=‘student’)
def selectSchedule(request, schedule_id):
schedule = Schedule.objects.get(pk=schedule_id)
student = request.user.student
schedule_datetime = datetime.combine(schedule.date, schedule.time)
if schedule_datetime > datetime.now() + timedelta(hours=1):
if schedule.capacity > 0:
student.schedules.add(schedule)
schedule.capacity -= 1
schedule.save()
messages.success(request, ‘Schedule added’)
else:
messages.warning(request, ‘Schedule is full’)
else:
messages.warning(request, ‘You can only select a schedule more than 1 hour from the start time’)
return redirect(‘view-schedule’)
template (student to select a schedule):
{% extends ‘main.html’ %}
{% block content %}
<div class="student-schedule-title"> <h3 class="student-schedule-header">Select Your Schedule</h3> </div> <div class="student-schedule-area"> <div class="student-schedule-date-field"> </div> <table class="student-schedule-table"> <tr> <th></th> <th>Date</th> <th>Time</th> <th>Teacher</th> <th>Class Duration</th> </tr> {% for schedule in schedules %} {% if schedule.capacity > 0 %} <tr> <td> <form action="{% url 'select-schedule' schedule.id %}" method="POST"> {% csrf_token %} <input type="hidden" name="schedule_id" value="{{ schedule.id }}"> <input type="Submit" value="Select"> </form> </td> <td>{{ schedule.date }}</td> <td>{{ schedule.time }}</td> <td>{{ schedule.user.username }}</td> <td>{{ schedule.duration }}</td> </tr> {% endif %} {% endfor %} </table>
Save{% endblock content %}
If anyone can guide me through how to get this done I’d appreciate the help.
Thanks.