Django
Django E-learning Platform Closing
We want to modify the menu bar so that instructors can have options to Create new Courses and List All courses while students has options that they need.
Edit base.css
Add the following command.
...................
#header .menu2 {
list-style: none;
float: left;
margin: 0;
padding: auto;
}
...................
Create a new template tag.
We already have a folder named temaplatetags inside our course application. Inside that we do have course.py. Edit that file
from django import template
from django.contrib.auth.models import Group
register = template.Library()
@register.filter
def model_name(obj):
try:
return obj._meta.model_name
except AttributeError:
return None
@register.filter(name="has_group")
def has_group(user,group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
Base.html
Now update the base.html file as follow.
{% load static %}
{% load course %}
<!DOCTYPE html>
<html>
><head>
<meta charset="utf-8"/>
<title>
{% block title %}Educa{% endblock %}
</title>
<link href="{%static "css/base.css"%}" rel="stylesheet">
</head>
<body>
<div id="header">
<a href="/" class="logo">Educa</a>
<ul class="menu">
{% if request.user.is_authenticated %}
<li><a href="{% url 'logout'%}">
Sign Out
</a></li>
{% else %}
<li>
<a href="{%url 'login'%}">
Sign in
</a>
</li>
{% endif %}
</ul>
{% if request.user.is_authenticated %}
{% if request.user|has_group:"Instructors" %}
<ul class="menu2">
<li>
<a href="{% url 'manage_course_list'%}">
Your Courses
</a>
</li>
</ul>
{% else %}
<ul class="menu2">
<li>
<a href="{% url 'student_course_list'%}">
Your Courses
</a>
</li>
</ul>
{% endif %}
{% else %}
<ul class="menu2">
<li>
<a href="{% url 'student_registration' %}">
Registration
</a>
</li>
</ul>
{% endif %}
</div>
<div id="content">
{%block content%}
{%endblock%}
</div>
{% block include_js %}
{% endblock %}
<script>
document.addEventListener('DOMContentLoaded',(event)=>{
//DOM loaded
{% block domready %}
{% endblock %}
});
</script>
</body>
</html>
Course Enroll
Edit “courses/course/detail.html”
{% extends 'base.html' %}
{% block title %}
{{object.title}}
{% endblock %}
{% block content %}
{% with subject=object.subject %}
<h1>
{{object.title}}
</h1>
<div class="module">
<h2>Overview</h2>
<p>
<a href="{% url 'course_list_subject' subject.slug %}">
{{subject.title}}
</a>
{{object.modules.count}} modules
<br>
Instructor: {{object.owner}}
<br>
Students Enrolled: {{object.students.count}}
</p>
{{object.overview|linebreaks}}
{% if request.user.is_authenticated %}
{% if request.user not in object.students.all %}
<form action="{% url 'student_enroll_course' %}" method="post">
{{enroll_form}}
{% csrf_token %}
<input type="submit" value="Enroll now">
</form>
{% endif %}
{% else %}
<a href="{% url 'student_registration' %}" class="button">
Register to enroll
</a>
{% endif %}
</div>
{% endwith %}
{% endblock %}
The Output
Due to implementation of caching some items takes time to appear.
So go to settings.py and disable cache.
# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.redis.RedisCache',
# 'LOCATION': 'redis://127.0.0.1:6379', # Replace with your Memcached server's address
# }
# }
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
Instructor
Student
pontu
0
Tags :