This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SECRET_MESSAGE=This is a very very secret message. Do not expose. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import environ #add this | |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
# Add All lines given below | |
env = environ.Env( | |
DEBUG=(bool, False) | |
) | |
environ.Env.read_env(os.path.join(BASE_DIR, '.env')) | |
# MESSAGE variable is getting its value from .env file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.http import HttpResponse | |
from django.conf import settings | |
def hello_world(request): | |
# READ value from settings.py | |
message = settings.SECRET_MESSAGE | |
return HttpResponse(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import dispatch | |
user_visit_signal = dispatch.Signal(["date_time","ip","device"]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.dispatch import receiver | |
from helloapp import signals | |
@receiver(signals.user_visit_signal) | |
def handle_user_visit(sender, date_time,ip,device, **kwargs): | |
message = "Visit to view {sender}, at {date_time} , IP is {ip} , Device is {device}".format(sender=sender,date_time=date_time,ip=ip,device=device) | |
print(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.apps import AppConfig | |
class HelloappConfig(AppConfig): | |
default_auto_field = "django.db.models.BigAutoField" | |
name = "helloapp" | |
def ready(self): | |
from helloapp import receivers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
from helloapp import signals | |
from django.utils import formats | |
from rest_framework.decorators import api_view | |
from django.http import JsonResponse | |
import datetime | |
def hello_world(request): | |
message = "Hello World" | |
date_time = datetime.datetime.now() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from helloapp import signals | |
class Notice(models.Model): | |
title = models.CharField(max_length=80) | |
description = models.CharField(max_length=255) | |
def save(self, *args, **kwargs): | |
if self.title=="Oops": | |
signals.invalid_notice_signal.send(sender=None) | |
return | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.dispatch import receiver | |
from helloapp import signals | |
from .models import Notice | |
from django.db.models import signals as modelSignals | |
#Predefined Model signal | |
@receiver(modelSignals.post_save, sender=Notice) | |
def valid_notice_created(sender, instance, created, **kwargs): | |
if created: | |
message = "Notice Created : id = {id} ".format(id=instance.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Consultant(models.Model): | |
name = models.CharField(max_length=255) | |
specialization = models.CharField(max_length=64) | |
def __str__(self): | |
return self.name | |
class Patient(models.Model): | |
name = models.CharField(max_length=255) |