<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hezekiah Morgan Chukwudi on Medium]]></title>
        <description><![CDATA[Stories by Hezekiah Morgan Chukwudi on Medium]]></description>
        <link>https://medium.com/@morganhezekiah111?source=rss-fc429b86080d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*pfh16pQiB2_JPHpc</url>
            <title>Stories by Hezekiah Morgan Chukwudi on Medium</title>
            <link>https://medium.com/@morganhezekiah111?source=rss-fc429b86080d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:35:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@morganhezekiah111/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to setup Django with a virtual environment]]></title>
            <link>https://medium.com/@morganhezekiah111/how-to-setup-django-with-a-virtual-environment-1dfc9bc41a1f?source=rss-fc429b86080d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dfc9bc41a1f</guid>
            <dc:creator><![CDATA[Hezekiah Morgan Chukwudi]]></dc:creator>
            <pubDate>Mon, 31 Oct 2022 20:57:16 GMT</pubDate>
            <atom:updated>2022-10-31T20:57:16.536Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are virtual environments in Django</h3><p>Django virtual environments are isolated Python interpreters, Scripts, and Libraries separated from other virtual environments and/or the Python environment installed on the system.</p><p>When we create Django projects the basic approach might be to use the Python and Python packages installed on the machine for such a project. But with virtual environments, we are sure to use separate python and python versions, packages, and package versions for different applications.</p><h3>Importance of using Virtual environments for Django</h3><ol><li>You can use any package you want for your Django application without worrying about conflict or collision in the future.</li><li>The virtual environment comes with Python by default and no other setups are necessary.</li><li>A virtual environment also saves your system python library from lots of unnecessary or not-so-in-use packages.</li><li>It is generally an acceptable approach to set up a Django project. If you are looking at working for a company or a team, you most probably need to use virtual environments.</li></ol><h3>Steps on how to setup Django with a virtual environment</h3><p>Although we already have an article on <a href="https://alwaysdjango.com/how-to-create-a-project-in-django/">how to create a Django project</a>, today we are going to create one with a virtual environment. This is actually how I recommend creating a Django project.</p><ol><li><a href="#install_the_package">Install the <strong>python virtualenv</strong> package on your system.</a></li><li><a href="#create_folder">Create a folder to house your project.</a></li><li><a href="#create_virtual_env">Create a virtualenv.</a></li><li><a href="#activate_virtual_environment">Activate the virtual environment.</a></li><li><a href="#create_project">Create a Django project.</a></li><li><a href="#install_django">Install Django</a></li><li><a href="#start_project">Start your project.</a></li></ol><h4>Install the python virtualenv package on your system.</h4><p>To make use of virtual environment package commands on your system, you need to install the <a href="https://pypi.org/project/virtualenv/">virtualenv </a>package.</p><p>Simply open your command line and enter the below pip install command</p><pre>pip install virtualenv</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/0*qvslfOb-Hv8w7-Yy.jpg" /></figure><h4>Create a folder to house your project.</h4><p>On a directory you prefer, you can create a folder to house your Django project. In this folder, we will also set up the virtual environment.</p><h4>Create a virtualenv for your Django project</h4><p>After creating the project folder, open the terminal to the directory and create your virtualenv.</p><pre>python -m venv environ</pre><p>In the above command, <strong><em>environ </em></strong>is the name of the virtual environment. Notice how on the folder you created in <a href="#create_folder">step 3</a>, an <strong><em>environmental </em></strong>folder will be created also.</p><p>Virtual environment folder structure</p><pre>\virtual_env_folder<br>   \Include<br>    \Lib<br>        \site-packages<br>     \Scripts<br>      pyvenv</pre><p>Above is a brief description of the virtual environment directory structure,</p><ul><li><strong><em>Include</em></strong>: Contains extra contents that do not necessarily fit into other sibling folders.</li><li><strong><em>Lib</em></strong>: In this folder, we have the site packageswhich house all the packages this project is using.</li><li><strong><em>Scripts</em></strong>: These are python activation, deactivation, and process scripts. Note, that the python interpreter exists in this folder.</li></ul><h3>Activate the virtual environment.</h3><p>At this point, your virtual environment is created and is ready to be used. Firstly, we have to activate it.</p><p><strong>Note:</strong> Without activating your virtual environment, any command you run will be executed on the system python environment and interpreter.</p><p>Generally, to activate a Django virtual environment, you simply execute the <strong><em>activate.bat</em></strong> file in the virtual environment Scripts folder.</p><p>In your command line opened to the project folder where the created virtual environment is,</p><pre>environ\Scripts\activate</pre><p><strong>Note:</strong> Here <strong>environ</strong> is the name of the virtual environment you specified in <a href="#create_virtual_env">step 3</a></p><p>After activation of your virtual environment, your command linewill have an indicator before the directory path to show you are using a virtual environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/0*TIxYVXtwdHBaS20p.png" /></figure><h3>Install Django</h3><p>After installation and activation of the virtual environment, we could now install packages for our project. Since we are using Django(obviously), we could now install Django.</p><p>In the terminal opened to the created folder, enter the below pip installation code;</p><pre>pip install Django</pre><h3>Create a Django project.</h3><p>Since we have Django installed on our virtual environment, we could now use the <strong>Django-admin</strong> command to create a project.</p><p>In the terminal opened to the created folder, enter the below Django project creation code;</p><pre>Django-admin start project project_name .</pre><p><strong>Note: </strong>Notice the full stop or period after the <strong>django-admin start projectproject_name</strong> command. This is used to tell the process of not creating a separate folder for the Django project. This is necessary as we want the <strong>manage.py</strong> file and the virtual environment folder in the same directory.</p><h3>Start your project.</h3><p>At this point, our project and its virtual environment are ready for use. So we simply need to start the project.</p><p>In the terminal opened to the created folder, enter the below pip installation code;</p><pre>python manage.py runserver</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NmkrJhPDBv4kNkav.jpg" /></figure><h3>How to deactivate Django virtual environment</h3><p>For any reason you want to deactivate your virtual environment, maybe continue with the system-installed python.</p><p>In the terminal opened to the created folder, enter the below pip installation code;</p><pre>deactivate</pre><p>This <strong><em>deactivate </em></strong>command will destroy the virtual environment usage for your project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dfc9bc41a1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HOW TO USE .ENV FILES IN DJANGO]]></title>
            <link>https://medium.com/@morganhezekiah111/how-to-use-env-files-in-django-e7348c05343c?source=rss-fc429b86080d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7348c05343c</guid>
            <dc:creator><![CDATA[Hezekiah Morgan Chukwudi]]></dc:creator>
            <pubDate>Mon, 24 Oct 2022 22:17:52 GMT</pubDate>
            <atom:updated>2022-10-24T22:17:52.850Z</atom:updated>
            <content:encoded><![CDATA[<h3>HOW TO USE .ENV FILES IN DJANGO</h3><h3>What Are .Env(Dotenv) Files</h3><p><strong><em>.env </em></strong>files as known as environment variable files. They offer information about a process’s operating system. These operating systems could be pipelines, development or production environment, etc.</p><p>With .env files, you could store project(in this case Django project) password, Database credentials, API credentials, etc. Any information you do not want to be open to any other person.</p><h3>Benefits Of Using. Env Files In Your Django Projects</h3><p>1. <strong>Security</strong></p><p>This is probably the basic importance of the <strong>.env </strong>file. As mentioned earlier, .env files keep project credentials and configurations away from others. This is so important especially when you are using version control systems like <a href="https://alwaysdjango.com/how-to-use-env-files-in-django/#using_github"><strong>Github</strong></a>.</p><p>The security <strong><em>.env</em></strong> files offer is further improved when multiple settings.py files are used. Learn <a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/">how to use multiple settings.py files in Django</a>.</p><p>In your Django application, you will no longer have your email credentials, Database configurations, security key, etc stored in the settings.py files.</p><p>2. <strong>Better Configuration</strong></p><p>With these <strong><em>.env</em></strong> files, it’s very easy for you to adapt to different environments and stages of your application. Most times, there are configurations you want during production and others during development. For instance, you may want to disable caching for development and enable it for production.</p><p>With your .env files since you have a designated place for these setting variables, changing them becomes relatively easier.</p><p><strong>Note:</strong> For this tutorial, am assuming you know how to create a Django project. If you do not, you can check this tutorial on <a href="https://alwaysdjango.com/how-to-create-a-project-in-django/">creating a Django project</a>.</p><p>Now, How To Use. Env Files In Django?</p><h3>Using Python-Decouple To Set Up. Env Files In Django</h3><p><a href="https://pypi.org/project/python-decouple/"><strong><em>python-decouple</em></strong></a>, this is a python module used basically to separate different parts of your application. As the name suggests, it decouples different sections of your project.</p><h4>Why Use Python-Decouple</h4><p>Probably, you have seen most developers set up .env files in Django using the <a href="https://django-environ.readthedocs.io/en/latest/">Django-environ</a> module or something else.</p><p>So why are we using python-decouple;</p><ul><li>It can be used for any other python project type, not just Django.</li><li>Easy of setting up. With python-decouple, you do not have to go through lots of setups. Just install the package and start using it.</li><li>Even as we are discussing .env files, <strong><em>python-decouple</em></strong> allows you to also use it <strong><em>in </em></strong>files too.</li><li>It also allows you to use different <strong><em>.env</em></strong> files in a single project.</li></ul><p>So, why not?</p><h4>Steps To Setup. Env Files In Django Using Python-Decouple</h4><ol><li><a href="https://alwaysdjango.com/how-to-use-env-files-in-django/#install_python_decouple">Install python-decouple in your project.</a></li><li><a href="https://alwaysdjango.com/how-to-use-env-files-in-django/#create_env">Create a <strong><em>.env</em></strong> file.</a></li><li><a href="https://alwaysdjango.com/how-to-use-env-files-in-django/#copy_your_security">Copy your security credentials to the <strong>.env</strong> file.</a></li><li><a href="https://alwaysdjango.com/how-to-use-env-files-in-django/#import_config">Import <strong><em>config </em></strong>to use your environment variables.</a></li></ol><h4>Install Python-Decouple In Your Project.</h4><p>To install this package on your Django project, simply open your command line to your project directory and enter the below pip install command.</p><pre>pip install python-decouple</pre><h4>Create. Env File.</h4><p>After installing the python-decouple package, create a .env in your project root directory.</p><p><strong>Note:</strong> This <strong><em>.env</em></strong> must be on the same directory path as the <strong><em>manage.py</em></strong> file for your project.</p><p><strong>Note:</strong> The <strong><em>.env</em></strong> file does not have an extension. It simply <strong>“.env”</strong></p><h4>Copy Your Security Credentials To The. Env File.</h4><p>At this point, your project is ready to use the .env file. You could now copy your project credentials, email configurations, etc to this file.</p><p>Here, we are simply going to copy <strong>SECRET_KEY </strong>and <strong>DEBUG</strong> to the .env file.</p><p>In .env file,</p><pre>SECRET_KEY=django-insecure-f+9=4jdwb2aw0$dt22)0b050mx$8+1&amp;a6((oo3**9p9zxysde%<br>DEBUG=True</pre><p><strong>Note:</strong> There should be no space between the variable key, =, and the variable value.</p><p><strong>Note:</strong> There should be quotationmarks markaround variable value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m49SzmbABrIQqQqO.png" /></figure><h4>Import Config To Use Your Environment Variables.</h4><p>To use your variables in the <strong>.env</strong> files, import the <strong><em>config </em></strong>method from <strong><em>decoupling</em></strong>. This method accepts an argument which is the variable key name.</p><p>In our <strong><em>settings.py</em></strong> files, we are going to use the variables in the <strong><em>.env</em></strong> file.</p><p>In <strong><em>settings.py</em></strong> files,</p><pre>from decoupling import config</pre><pre>SECRET_KEY = config(&#39;SECRET_KEY &#39;)<br>DEBUG = config(&#39;DEBUG&#39;)</pre><h3>Type-Casting Arguments In Python-Decouple</h3><p>In the above example, we used the <strong><em>DEBUG </em></strong>variable in our environment variable. This variable in our settings.py files is a boolean. The <strong><em>config</em></strong> returns its values as string values unless we tell it otherwise.</p><p>To type-cast python-decouple variable you pass in the <strong><em>type </em></strong>argument</p><pre>from decoupling import config<br></pre><pre>SECRET_KEY=config(&quot;SECRET_KEY&quot;, <em>type</em>=<em>str</em>)<br>DEBUG = config(&quot;DEBUG&quot;, <em>type</em>=<em>bool</em>)</pre><p>In the above example, we have used <strong><em>str </em></strong>and <strong><em>bool </em></strong>types. Other available types include <strong><em>int</em></strong>, <strong><em>list</em></strong>, <strong><em>dict</em></strong>, <strong><em>tuple</em></strong>, <strong>bytes, </strong>etc.</p><h3>Providing Default Values To Python-Decouple</h3><p>Sometimes, we may desire to provide default values for our variables in case it does not exist in the <strong><em>.env</em></strong> file. To do this we pass in the <strong><em>default </em></strong>argument to the <strong><em>config </em></strong>method.</p><pre>from decoupling import config</pre><pre>DEBUG = config(&quot;DEBUG&quot;, <em>type</em>=<em>bool</em>, default=True)</pre><h3>Environment Variables And Version Control</h3><p>It’s very important to note that <strong><em>.env</em></strong> files should not be pushed to any version control like GitHub. This will defer the importance of the <strong><em>.env</em></strong> file since it makes our application credentials public.</p><p>To before pushing your project codes to <a href="https://github.com/">GitHub</a>, you must include <strong><em>.env</em></strong> in the <strong><em>.gitignore</em></strong> file for your project.</p><h4>What Is The? Gitignore? How Can I Use It In My Django Application</h4><p><strong><em>.gitignore</em></strong> file is a file used to tell GitHub commands which files in your project should not be committed or pushed repositories.</p><p>A file like virtual environments, <strong><em>.env’s,</em></strong> .<strong><em>ini, </em></strong>etc should not be pushed to GitHub repos.</p><p>In your Django application, before using GitHub, you should have this <strong><em>.gitignore</em></strong> file in the root location(same location as <strong><em>manage.py</em></strong> and <strong><em>.env</em></strong>). In this file, you can now add the relative path to files you want to be ignored by Git.</p><p>Sample .gitignore file for Django application.</p><pre># Created by https://www.toptal.com/developers/gitignore/api/django<br># Edit at https://www.toptal.com/developers/gitignore?templates=django</pre><pre>### Django ###<br>*.log<br>*.pot<br>*.pyc<br>__pycache__/<br>local_settings.py<br>db.sqlite3<br>db.sqlite3-journal<br>media</pre><pre># If your build process includes running collectstatic, then you probably don&#39;t need or want to include static files/<br># in your Git repository. Update and uncomment the following line accordingly.<br># &lt;django-project-name&gt;/static files/</pre><pre>### Django.Python Stack ###<br># Byte-compiled / optimized / DLL files<br>*.py[cod]<br>*$py.class</pre><pre># C extensions<br>*.so</pre><pre># Distribution / packaging<br>.Python<br>build/<br>develop-eggs/<br>dist/<br>downloads/<br>eggs/<br>.eggs/<br>lib/<br>lib64/<br>parts/<br>sdist/<br>var/<br>wheels/<br>share/python-wheels/<br>*.egg-info/<br>.installed.cfg<br>*.egg<br>MANIFEST</pre><pre># PyInstaller<br>#  Usually these files are written by a python script from a template<br>#  before PyInstaller builds the exe, inject date/other info into it.<br>*.manifest<br>*.spec</pre><pre># Installer logs<br>pip-log.txt<br>pip-delete-this-directory.txt</pre><pre># Unit test/coverage reports<br>htmlcov/<br>.tox/<br>.nox/<br>.coverage<br>.coverage.*<br>.cache<br>nosetests.xml<br>coverage.xml<br>*.cover<br>*.py,cover<br>.hypothesis/<br>.pytest_cache/<br>cover/</pre><pre># Translations<br>*.mo</pre><pre># Django stuff:</pre><pre># Flask stuff:<br>instance/<br>.webassets-cache</pre><pre># Scrapy stuff:<br>.scrapy</pre><pre># Sphinx documentation<br>docs/_build/</pre><pre># PyBuilder<br>.pybuilder/<br>target/</pre><pre># Jupyter Notebook<br>.ipynb_checkpoints</pre><pre># IPython<br>profile_default/<br>ipython_config.py</pre><pre># pyenv<br>#   For a library or package, you might want to ignore these files since the code is<br>#   intended to run in multiple environments; otherwise, check them in:<br># .python-version</pre><pre># pipenv<br>#   According to papa/pipenv#598, it is recommended to include Pipfile. lock in version control.<br>#   However, in case of collaboration, if having platform-specific dependencies or dependencies<br>#   having no cross-platform support, pipenv may install dependencies that don&#39;t work, or not<br>#   install all needed dependencies.<br>#Pipfile. lock</pre><pre># poetry<br>#   Similar to Pipfile. lock, it is generally recommended to include poetry. lock in version control.<br>#   This is especially recommended for binary packages to ensure reproducibility, and is more<br>#   commonly ignored for libraries.<br>#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control<br>#poetry.lock</pre><pre># pdm<br>#   Similar to Pipfile. lock, it is generally recommended to include PDM. lock in version control.<br>#pdm.lock<br>#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it<br>#   in version control.<br>#   https://pdm.fming.dev/#use-with-ide<br>.pdm.toml</pre><pre># PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm<br>__pypackages__/</pre><pre># Celery stuff<br>celery beat-schedule<br>celery boat. PID</pre><pre># SageMath parsed files<br>*.sage.py</pre><pre># Environments<br>.env<br>.venv<br>env/<br>venv/<br>ENV/<br>env.bak/<br>venv.bak/</pre><pre># Spyder project settings<br>.spyderproject<br>.spyproject</pre><pre># Rope project settings<br>.ropeproject</pre><pre># mkdocs documentation<br>/site</pre><pre># mypy<br>.mypy_cache/<br>.dmypy.JSON<br>dmypy.json</pre><pre># Pyre type checker<br>.pyre/</pre><pre># pytype static type analyzer<br>.pytype/</pre><pre># Cython debug symbols<br>cython_debug/</pre><pre># PyCharm<br>#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can<br>#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore<br>#  and can be added to the global gitignore or merged into this file.  For a more nuclear<br>#  option (not recommended) you can uncomment the following to ignore the entire idea folder.<br>#.idea/</pre><pre># End of <a href="https://www.toptal.com/developers/gitignore/api/django">https://www.toptal.com/developers/gitignore/api/django</a></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7348c05343c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HOW TO SCHEDULE DJANGO COMMANDS WITH WINDOWS TASK SCHEDULER]]></title>
            <link>https://medium.com/@morganhezekiah111/how-to-schedule-django-commands-with-windows-task-scheduler-db83f9c9fde4?source=rss-fc429b86080d------2</link>
            <guid isPermaLink="false">https://medium.com/p/db83f9c9fde4</guid>
            <dc:creator><![CDATA[Hezekiah Morgan Chukwudi]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 08:02:36 GMT</pubDate>
            <atom:updated>2022-10-20T08:02:36.018Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, we are going to discuss scheduling Django commands with the windows task scheduler.</p><h3>What Is The Windows Task Scheduler?</h3><p>Windows task scheduler is a system software tool used to automatically run task commands periodically at a specific time. For instance, you could have your windows machine run a command that sends you crypto price changes after querying an <a href="https://alwaysdjango.com/how-to-make-api-requests-from-a-django-app/#what_is_an_api">API</a>.</p><h3>How To Open Windows Task Scheduler On Windows 10</h3><p>To open the windows task scheduler on windows 10, follow these steps below;</p><ol><li>Holding your window button, press <strong><em>X.</em></strong></li><li>On the list outputted, select and click on <strong><em>Computer Management</em></strong>.</li><li>From the Computer Management dialog select <strong><em>Task scheduler</em></strong>.</li></ol><p>1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/0*t3PDju5ZMw3gAOw2.jpg" /></figure><p>2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ByDov2X-Yi6Vtr1L.jpg" /></figure><p>To learn more about Windows task scheduler, <a href="https://www.computerhope.com/jargon/t/tasksced.htm">you could check this article</a>.</p><p>After successfully opening the windows task scheduler, we can now relate Django commands to the ith windows task scheduler.</p><h3>What Are Django Commands</h3><p>Django commands are python commands used to execute a common task on our applications. These commands could be <a href="https://alwaysdjango.com/django-built-in-commands/">inbuilt</a> or <a href="https://alwaysdjango.com/how-to-create-custom-commands-in-django/">custom</a>.</p><p>We are going to look at scheduling custom Django commands on the windows task scheduler.</p><p>To create a <a href="https://alwaysdjango.com/django-built-in-commands/">Django command</a>, you could read this article where I discussed <a href="https://alwaysdjango.com/how-to-create-custom-commands-in-django/">custom Django commands</a> in detail.</p><p>Here, we are making a custom command that sends a greeting email to a friend every morning by 8:00 am.</p><p><strong>Note:</strong> For this tutorial, am assuming you know how to create a Django project. If you do not, you can check this tutorial on <a href="https://alwaysdjango.com/how-to-create-a-project-in-django/">creating a Django project</a>.</p><h3>Steps To Adding Django Command To Windows Task Scheduler.</h3><ol><li>Create the Django command.</li><li>Open the windows task scheduler.</li><li>Set up thetask name, ac,tion, and triggers.</li></ol><h4>Create The Django Command</h4><p>You have to make sure the command you want to schedule is created and running correctly.</p><p>To create the command, create a <strong><em>commands </em></strong>folder in a <strong><em>management </em></strong>folder existing in a project app folder.</p><p>In the above <strong><em>commands </em></strong>folder, you can create python files that will house the command execution codes.</p><p>To learn more about Django commands, check the below articles.</p><ol><li><a href="https://alwaysdjango.com/django-built-in-commands/">Built-in Django commands.</a></li><li><a href="https://alwaysdjango.com/how-to-create-custom-commands-in-django/">How to create custom commands in Django.</a></li></ol><pre>from django.core.management import BaseCommand<br>from django.core.mail import send_mail<br>from django.conf import settings<br></pre><pre><em>class</em> Command(BaseCommand):<br>    <em>help</em>=&quot;Sends a hi email to a friend&quot;<br>    <em>def</em> handle(self, *args, **kwargs) :<br>        send_mail(&#39;Hi message from windows task scheduler&#39;, &#39;Hi, we are testing sending emails from windows scheduler with a Django app&#39;, settings.EMAIL_HOST, <br>        [&#39;friendsEmail@email.com&#39;], fail_silently=False)</pre><p>In the above python file(<strong><em>friendEmail.py</em></strong>), we created a simple Django script to send emails to a specified email address.</p><p>To learn how to send emails using Django, check the below articles.</p><ul><li><a href="https://alwaysdjango.com/how-to-send-email-in-django/">How to send emails in Django.</a></li><li><a href="https://alwaysdjango.com/how-to-send-html-emails-in-django/">How to send HTML emails in Django.</a></li></ul><p>To execute the above command run <strong><em>python manage.py friendEmail</em></strong>, since <strong><em>friendEmail </em></strong>is the name of the python file.</p><h4>Open The Windows Task Scheduler.</h4><p>To open the windows task scheduler, you can simply enter the phrase <strong><em>task scheduler</em></strong> in the windows search bar. On the outputted results click to open the task scheduler.</p><p><a href="https://alwaysdjango.com/how-to-schedule-django-commands-with-windows-task-scheduler/#opening_scheduler_for_windows_10">Scroll here to learn how to open windows task scheduler specifically on windows 10.</a></p><h4>Set Up The Task Name, Action, And Triggers.</h4><p>Click on the <strong><em>Create Basic task</em></strong> option on the right options bar. This will launch a Create Basic task wizard.</p><p>Setting Task Name</p><p>Now you can specify a name you wish your command to be called. Here am setting it as <strong><em>friendEmailer</em></strong>( since this is the name of our command in the Django application).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/0*am1yr9BKahNmLY9U.png" /></figure><p>You could also add a description for your command, but I will ignore that since it is optional.</p><p>From the bottom actbuttonstons click on next.</p><p>Setting Task Trigger</p><p>On the next tab(<strong><em>Trigger</em></strong>), you specify when the task should be executed.</p><p>On the trigger options we have;</p><ul><li><strong><em>Daily:</em></strong> Runs your tasks daily at a specified period.</li><li><strong><em>Weekly:</em></strong> Executes your tasks daily at a specified period and day of the week.</li><li><strong><em>Monthly:</em></strong> Specifies the day and time in the month to run your task.</li><li><strong><em>One Time:</em></strong> For just a task that runs once and only that. You can choose the date and time to run your task.</li><li><strong><em>When the computer starts</em></strong>: Carry out the task immediately after the computer boots completely.</li><li><strong><em>When I Log on:</em></strong> Runs every time the current window user logs into the system.</li><li><strong><em>When a specific event is logged:</em></strong> Use when you want to use <a href="https://www.solarwinds.com/resources/it-glossary/windows-event-log">window logs</a> to trigger the task execution.</li></ul><p>For this example, I will set my task to execute daily at 8:00 am.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/0*vxNLM4Dstnok8oBC.jpg" /></figure><p>Setting Task Actions</p><p>Next, will be specifying the <strong><em>Action</em></strong>. Here there are 3 options — <strong><em>Start a program(default)</em></strong>, <strong><em>Send an e-mail</em></strong>, and <strong><em>Display a message</em></strong>.</p><p>We are going to leave this option as the default(<strong><em>Start a program</em></strong>). As of when this article was published all other options were depreciated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/0*URcYO3Lw_M_0_Whw.jpg" /></figure><p>The next tab(<strong><em>Action</em></strong>), is where we declare what happens when we run our task.</p><p>On this tab, there are two important fields</p><ol><li><strong><em>Program/script: </em></strong>This is the script executable file that is used as an environment for the task. In this case, it will be our python environment(virtual or system).</li></ol><p>To find out which python environment our project is running on, we open our command line to the project folder and enter the below code.</p><pre>where python</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/0*Yrxyo_AHZqTyWxwp.jpg" /></figure><p>This will list a set of python environments this current directory has access to, copy this environment path to the <strong><em>Program/script</em></strong> field.</p><p>2. <strong><em>Add arguments</em></strong>: This is where you say which command is used to start this task. For every Django project it is <strong><em>python manage.py commandName –options–</em></strong>. In this input, we enter a sample argument like</p><p><strong><em>“C:\python projects\django\task_scheduler\manage.py” friendEmailer</em></strong></p><p><strong>Note:</strong> Inside the quotation marks(“), we have the absolute path to our <strong>manage.py</strong> file. Outside it is the name of the command to rrun ran</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/0*iLD_KTOpUq_mZN2h.png" /></figure><p>You can now click on finish to finalize.</p><p>After this, your Django command will be scheduled to run as specified.</p><p>You could also wish to run your task immediately for testing.</p><h3>How To Run A Task From Windows Task Scheduler</h3><p>To run a ttask theon windows task scheduler we follow the steps below</p><ul><li><a href="https://alwaysdjango.com/how-to-schedule-django-commands-with-windows-task-scheduler/#open_the_task_scheduler">Open the task scheduler</a>.</li><li><a href="https://alwaysdjango.com/how-to-schedule-django-commands-with-windows-task-scheduler/#available_tasks">Click on the Task scheduler library to see all available tt</a>asks rightight-click</li><li><a href="https://alwaysdjango.com/how-to-schedule-django-commands-with-windows-task-scheduler/#click_on_run">Right click on the desired task and click on run.</a></li></ul><h4>Open The Task Scheduler.</h4><p>Because we have discussed tins in this article, <a href="https://alwaysdjango.com/how-to-schedule-django-commands-with-windows-task-scheduler/#open_windows_task_scheduler">click here to read</a>.</p><h4>Click On The Task Scheduler Library To See All Available Tasks.</h4><p>When the task scheduler wizard is opened, on the left options tab click on Task scheduler library. This will display all available tasks on your window machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-zStntkMQUe2DsTg.jpg" /></figure><h4>Right Click On The Desired Task And Click On Run.</h4><p>From the displayed list, you can scroll to review all tasks. Right-click on the desired task and click on run.</p><p>This will start the tasks for execution.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db83f9c9fde4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HOW TO USE MULTIPLE SETTINGS.PY FILES IN DJANGO]]></title>
            <link>https://medium.com/@morganhezekiah111/how-to-use-multiple-settings-py-files-in-django-97f6ead55aa7?source=rss-fc429b86080d------2</link>
            <guid isPermaLink="false">https://medium.com/p/97f6ead55aa7</guid>
            <dc:creator><![CDATA[Hezekiah Morgan Chukwudi]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 01:07:17 GMT</pubDate>
            <atom:updated>2022-10-20T01:07:17.610Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*mRKDqf02Kd0cyupG.png" /></figure><p><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/">September 30, 2022</a></p><h3>HOW TO USE MULTIPLE SETTINGS.PY FILES IN DJANGO</h3><p><a href="https://www.facebook.com/sharer/sharer.php?u=https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/&amp;display=popup&amp;ref=plugin&amp;src=share_button"><strong>Facebook</strong></a><a href="https://twitter.com/share?url=https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/&amp;text=How%20to%20use%20multiple%20settings.py%20files%20in%20Django"><strong>Tweet</strong></a><a href="https://pinterest.com/pin/create/button/?url=https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/&amp;media=https://alwaysdjango.com/wp-content/uploads/2022/09/How-to-use-multiple-settings.png&amp;description=How%20to%20use%20multiple%20settings.py%20files%20in%20Django"><strong>Pin</strong></a><a href="https://www.linkedin.com/shareArticle?mini=true&amp;url=https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/"><strong>LinkedIn</strong></a></p><p>Most times our applications go through different stages and environments like development, testing and production. Since the Django <strong><em>settings.py</em></strong> file contains the configurations and setting variables for our application, its mostly common that we need to twirk or change theses setting at different stages and environments.</p><p>To solve this problem, most times we use conditional statements to determine if the <strong><em>DEBUG </em></strong>variable is true or not and make necessary changes. This is will work perfectly but most times when lots of conditions and variables are changed, the <strong><em>settings.py</em></strong> file becomes clouded.</p><p><a href="https://giphy.com/gifs/onechicago-nbc-one-chicago-med-41OVKRJeDwt8aeaN1H">via GIPHY</a></p><p>What should we do then?</p><p>Multiple <strong><em>settings.py</em></strong> files, that is the solution.</p><h3>Benefits Of Using Multiple Settings Files In Django</h3><ol><li>It increases the security of your application.</li><li>Performance improvement. It was mentioned as one performance-enhancing tip <a href="https://alwaysdjango.com/10-tips-to-improve-the-performance-of-a-django-application/">here(Tips To Improve The Performance Of A Django Application)</a>.</li><li>It makes for better collaborative development processes for our Django projects.</li></ol><h3>How To Use Multiple Settings.Py Files In Django? Follow The Steps Below</h3><ul><li><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/#create_folder">Create a settings folder.</a></li><li><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/#rename_to_base">Copy the current settings.py file to the newly created folder and rename it to <strong><em>base.py</em></strong>.</a></li><li><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/#different">Create your different settings files with preferred names( <strong><em>development.py</em></strong>, <strong><em>production.py</em></strong>).</a></li><li><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/#star">Star(*) import from <strong><em>base.py</em></strong> and make applicable changes to the new settings files.</a></li><li><a href="https://alwaysdjango.com/how-to-use-multiple-settings-py-files-in-django/#manage">Make changes to the <strong><em>manage.py</em></strong> file to use any settings.py file you want.</a></li></ul><h4>1. Create A Settings Folder On Your Project Root Directory.</h4><p>On your project root directory, create a settings folder to house your settings files.</p><p>After creating this folder, create an <strong><em>__init__.py</em></strong> file inside it. This is very important, especially for python versions less than 2.5. It helps your applications recognize the folder as a python module.</p><pre>\your_project_folder<br>   \settings.py<br>   \settings<br>       __init__.py<br>    \manage.py<br>    \your_apps</pre><h4>2. Copy The Current Settings.Py File To The Newly Created Folder And Rename It To Base.Py.</h4><p>Because we still need our normal Django settings and configurations, we need to move the current <strong><em>settings.py</em></strong> file to the newly created folder(settings folder).</p><p>We now rename this copied file to <strong><em>base.py</em></strong>. This renaming is not necessary but conventional.</p><pre>\your_project_folder<br>   \settings.py<br>   \settings<br>       __init__.py<br>       base.py<br>    \manage.py<br>    \your_apps</pre><p>Sample <strong><em>settings.py</em></strong> file</p><pre>SECRET_KEY = &#39;django-insecure-fbpmp8hifr2dpkl6b^gyxq^7@gi*nxz&amp;0t46y5*ot2l5kn0qqx&#39;</pre><pre>INSTALLED_APPS = [<br>    &#39;django.contrib.auth&#39;,<br>    &#39;django.contrib.contenttypes&#39;,<br>    &#39;django.contrib.sessions&#39;,<br>    &#39;django.contrib.messages&#39;,<br>    &#39;django.contrib.staticfiles&#39;,</pre><pre>    &#39;myapp&#39;,<br>]</pre><pre>MIDDLEWARE = [<br>    &#39;django.middleware.security.SecurityMiddleware&#39;,<br>    &#39;django.contrib.sessions.middleware.SessionMiddleware&#39;,<br>    &#39;django.middleware.common.CommonMiddleware&#39;,<br>    &#39;django.middleware.csrf.CsrfViewMiddleware&#39;,<br>    &#39;django.contrib.auth.middleware.AuthenticationMiddleware&#39;,<br>    &#39;django.contrib.messages.middleware.MessageMiddleware&#39;,<br>    &#39;django.middleware.clickjacking.XFrameOptionsMiddleware&#39;,<br>]</pre><pre>ROOT_URLCONF = &#39;myapp.urls&#39;</pre><pre>WSGI_APPLICATION = &#39;myapp.wsgi.application&#39;</pre><h4>3. Create Your Different Settings Files With Prefered Names( Development.Py, Production.Py).</h4><p>Now for your different environments and stages, create a settings file to accommodate its configurations.</p><p>In this case, we are using two;</p><ul><li><strong><em>developments.py</em></strong>: To be used in the development environment.</li><li><strong><em>production.py</em></strong>: To be used in the production environment.</li></ul><p><strong>Note:</strong> For most applications, we could also have a <strong><em>tests.py</em></strong>. Which will be used for testing environment.</p><h4>4. Star(*) Import From Base.Py And Make Applicable Changes To The New Settings Files.</h4><p>From your different settings files(<strong><em>development.py</em></strong>, <strong><em>productions.py</em></strong>), we now import everything from <strong><em>base.py</em></strong>. This makes sure we share all the reusable settings in <strong><em>base.py</em></strong>.</p><p>In setting files,</p><pre>from base import *</pre><p><strong>Note:</strong> In most cases, star(*) import is discouraged. This is because by so doing, you could import unnecessary values. But in this case, we do because we just have to.</p><p>Now, you could make changes to your different setting files to achieve what you want.</p><p>In <strong><em>development.py</em></strong>,</p><pre>from .base import *</pre><pre>DEBUG = True</pre><pre>INSTALLED_APPS += [<br>    &#39;debug_toolbar&#39;,<br>]</pre><pre>MIDDLEWARE += [&#39;debug_toolbar.middleware.DebugToolbarMiddleware&#39;, ]</pre><pre>EMAIL_BACKEND = &#39;django.core.mail.backends.console.EmailBackend&#39;</pre><p>In <strong><em>production.py</em></strong>,</p><pre>from .base import *</pre><pre>DEBUG = False</pre><pre>ALLOWED_HOSTS = [&#39;127.0.0.1:8000&#39;, ]</pre><pre>CACHES = {<br>    &#39;default&#39;: {<br>        &#39;BACKEND&#39;: &#39;django.core.cache.backends.memcached.MemcachedCache&#39;,<br>        &#39;LOCATION&#39;: &#39;127.0.0.1:11211&#39;,<br>    }<br>}</pre><pre>EMAIL_BACKEND = &#39;django.core.mail.backends.smtp.EmailBackend&#39;<br>EMAIL_HOST = &#39;smtp.mailgun.org&#39;<br>EMAIL_PORT = 587<br>EMAIL_HOST_USER = config(&#39;EMAIL_HOST_USER&#39;)<br>EMAIL_HOST_PASSWORD = config(&#39;EMAIL_HOST_PASSWORD&#39;)<br>EMAIL_USE_TLS = True</pre><h4>5. Make Changes To The Manage.Py File To Use Any Settings.Py File You Want.</h4><p>By default, when we run our Django commands, we are expected to have our setting in the <strong><em>/app_name_folder/settings.py</em></strong> file. Following our above changes, this file does not exist, so we have to tell Django where to locate our settings file.</p><p>In your <strong><em>manage.py(project_folder/manage.py)</em></strong>, we can now make the below changes.</p><p>From</p><pre>#!/usr/bin/env python<br>&quot;&quot;&quot;Django&#39;s command-line utility for administrative tasks.&quot;&quot;&quot;<br>import os<br>import sys<br></pre><pre>def main():<br>    &quot;&quot;&quot;Run administrative tasks.&quot;&quot;&quot;<br>    os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;project_name.settings&#39;)<br>    try:<br>        from django.core.management import execute_from_command_line<br>    except ImportError as exc:<br>        raise ImportError(<br>            &quot;Couldn&#39;t import Django. Are you sure it&#39;s installed and &quot;<br>            &quot;available on your PYTHONPATH environment variable? Did you &quot;<br>            &quot;forget to activate a virtual environment?&quot;<br>        ) from exc<br>    execute_from_command_line(sys.argv)<br></pre><pre>if __name__ == &#39;__main__&#39;:<br>    main()</pre><p>To</p><pre>#!/usr/bin/env python<br>&quot;&quot;&quot;Django&#39;s command-line utility for administrative tasks.&quot;&quot;&quot;<br>import os<br>import sys<br></pre><pre><em>def</em> main():<br>    &quot;&quot;&quot;Run administrative tasks.&quot;&quot;&quot;<br>    os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;project_name.settings.development&#39;)<br>    try:<br>        from django.core.management import execute_from_command_line<br>    except ImportError as exc:<br>        raise ImportError(<br>            &quot;Couldn&#39;t import Django. Are you sure it&#39;s installed and &quot;<br>            &quot;available on your PYTHONPATH environment variable? Did you &quot;<br>            &quot;forget to activate a virtual environment?&quot;<br>        ) from exc<br>    execute_from_command_line(sys.argv)<br></pre><pre>if __name__ == &#39;__main__&#39;:<br>    main()</pre><p>Basically, we changed this line,</p><pre>os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;project_name.settings&#39;)</pre><p>To</p><pre>os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;project_name.settings.development&#39;)</pre><p>From</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/0*GI-0MfD4UkBFbELT.jpg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/0*QdxeTN9_HFkKJTjY.jpg" /></figure><p>Now when you run <strong><em>python manage.py runserver</em></strong>, the specified settings file will be used. It is now advisable to make these changes to suit the environment we are in.</p><h3>How To Specify Setting File To Use For Your Django Commands</h3><p>Sure, we have answered the question, <strong><em>How to use multiple settings.py files in Django?</em></strong></p><p>Now, we are going to look at how we can specify the settings file to be used when running Django commands.</p><pre>python manage.py runserver --settings=myapp.settings.development</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Lo8QrJ-7FY0woLTv.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97f6ead55aa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HOW TO CREATE A JOB SCHEDULER IN DJANGO]]></title>
            <link>https://medium.com/@morganhezekiah111/how-to-create-a-job-scheduler-in-django-3dc449b1fa9a?source=rss-fc429b86080d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dc449b1fa9a</guid>
            <dc:creator><![CDATA[Hezekiah Morgan Chukwudi]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 01:05:33 GMT</pubDate>
            <atom:updated>2022-10-20T01:05:33.429Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*gdUCimev1iXmM9BX.png" /></figure><p><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/">October 5, 2022</a></p><h3>HOW TO CREATE A JOB SCHEDULER IN DJANGO</h3><p><a href="https://www.facebook.com/sharer/sharer.php?u=https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/&amp;display=popup&amp;ref=plugin&amp;src=share_button"><strong>Facebook</strong></a><a href="https://twitter.com/share?url=https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/&amp;text=How%20to%20create%20a%20job%20scheduler%20in%20Django"><strong>Tweet</strong></a><a href="https://pinterest.com/pin/create/button/?url=https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/&amp;media=https://alwaysdjango.com/wp-content/uploads/2022/10/How-to-create-a-job-scheduler-in-Django.png&amp;description=How%20to%20create%20a%20job%20scheduler%20in%20Django"><strong>Pin</strong></a><a href="https://www.linkedin.com/shareArticle?mini=true&amp;url=https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/"><strong>LinkedIn</strong></a></p><p>On many occasions have I wanted to run a piece of code repeatively in my Django application.</p><p>Maybe I have a <a href="https://alwaysdjango.com/django-commands/">Django custom command</a> and want to call this command periodically as my application server is running.</p><p>Job scheduling is the process of repetitively running a command or job over a specific period.</p><p>For this tutorial, we are going to set up a job that prints the timezone time every 5 seconds.</p><p>To schedule jobs in our Django application, we are going to use a Python external library called <a href="https://pypi.org/project/APScheduler/">APScheduler</a>.</p><h3>What Is APSCHEDULER Python Library</h3><p>APSCHEDULER is a Python library used to run python code occasionally. These python codes are mostly refered to as jobs.</p><p>APSCHEDULER allows you add and remove jobs to a scheduler.</p><p><strong>Note:</strong> For this tutorial, am assuming you know how to create a Django project. If you do not, you can check this tutorial on <a href="https://alwaysdjango.com/how-to-create-a-project-in-django/">creating a Django project</a>.</p><h3>Steps On How To Create A Job Scheduler In Django</h3><ol><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#install_apscheduler">Install python APSCHEDULER library.</a></li><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#create_app">Create a Django app to house your job functions.</a></li><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#create_job">Create your job function(s).</a></li><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#create_job_scheduler_function">Create a job scheduler function(s).</a></li><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#register_scheduler">Register the scheduler function to your app.</a></li><li><a href="https://alwaysdjango.com/how-to-create-a-job-scheduler-in-django/#restart_server">Restart your server.</a></li></ol><h3>1. Install Python APSCHEDULER Library.</h3><p>To install the APSCHEDULER library, open your terminal to the project folder and enter the below pip installation code,</p><pre>pip install APScheduler</pre><p>With the above command, you can install the APSCHEDULER library to your project.</p><h3>2. Create A Django App To House Your Job Functions.</h3><p>For the modularization of our project codes, we are to create a new Django app where our job functions and schedulers will live.</p><pre>python manage.py startappjobs</pre><p>Then, register this app to the project’s <strong><em>settings.py</em></strong> file.</p><pre>INSTALLED_APPS = [<br>    &#39;django.contrib.admin&#39;,<br>    &#39;django.contrib.auth&#39;,<br>    &#39;django.contrib.contenttypes&#39;,<br>    &#39;django.contrib.sessions&#39;,<br>    &#39;django.contrib.messages&#39;,<br>    &#39;django.contrib.staticfiles&#39;,</pre><pre>    &quot;jobs&quot;,<br>]</pre><p>In the app folder, we could create two folders;</p><ul><li><strong><em>functions</em></strong>: To house the job functions.</li><li><strong><em>schedulers</em></strong>: To house the job schedulers.</li></ul><pre>\jobs<br>  \migration<br>  __init__.py<br>  admin.py<br>  app.py<br>  functions.py<br>  models.py<br>  schedulers.py<br>  tests.py<br>  views.py</pre><h3>3. Create Your Job Function(S).</h3><p>The job functions are the actual functions that are executed doing the job process.</p><p>They are the tasks you want to perform.</p><p>We are going to store these in the <strong><em>functions </em></strong>folder in the <strong><em>jobs </em></strong>app folder.</p><p>Below is the <strong><em>printTime </em></strong>function.</p><pre>from django.utils import timezone</pre><pre><em>def</em> printTime():<br>    print(timezone.now())</pre><p>This function simply prints the timezone time every 5 seconds.</p><h3>4. Create A Job Scheduler Function(S).</h3><p>The schedulers are functions that periodically schedule the job functions.</p><p>Note, here we are going to use APSCHEDULER <strong>BackgroundScheduler</strong> but there are many other scheduler like <a href="https://apscheduler.readthedocs.io/en/3.x/modules/schedulers/blocking.html#apscheduler.schedulers.blocking.BlockingScheduler">BlockingScheduler</a>, <a href="https://apscheduler.readthedocs.io/en/3.x/modules/schedulers/asyncio.html#apscheduler.schedulers.asyncio.AsyncIOScheduler">AsyncIOScheduler</a>, <a href="https://apscheduler.readthedocs.io/en/3.x/modules/schedulers/tornado.html#apscheduler.schedulers.tornado.TornadoScheduler">TornadoScheduler</a>. But since our scheduler will run in the background, <strong>BackgroundScheduler</strong> is perfect.</p><pre>from .functions import printTime<br></pre><pre><em>def</em> start_printTime():<br>    scheduler = BackgroundScheduler()<br>    scheduler.add_job(printTime, &#39;interval&#39;, seconds=5)<br>    scheduler.start()</pre><p><strong>Note:</strong> Conventionally, the schedulers are named as <strong><em>start_&lt;job_function_name&gt;</em></strong>.</p><p><strong><em>scheduler = BackgroundScheduler():</em></strong> Initializing <strong><em>BackgroundScheduler</em></strong>.</p><p><strong><em>scheduler.add_job(printTime, ‘interval’, seconds=5)</em></strong>: Adding a job function to the scheduler(<strong><em>printTime</em></strong>).</p><p><strong><em>scheduler.start():</em></strong> Starting the scheduler.</p><h3>5. Register The Scheduler Function To Your App.</h3><p>Obviously, we need a way to start our scheduler immediately our server starts running.</p><p>The perfect place for this will be in the <strong><em>&lt;App&gt;Config</em></strong> class <strong><em>ready </em></strong>method in the <strong><em>app.py</em></strong> file of our <strong><em>jobs</em></strong> project app.</p><p>This is because the <strong><em>&lt;App&gt;Config</em></strong> class <strong><em>ready </em></strong>method of every Django app runs when the server starts.</p><p>In <strong><em>jobs/app.py</em></strong>,</p><pre>from django.apps import AppConfig</pre><pre><em>class</em> JobsConfig(AppConfig):<br>    default_auto_field = &#39;django.db.models.BigAutoField&#39;<br>    name = &#39;jobs&#39;</pre><pre>    <em>def</em> ready(self):<br>        from .schedulers import start_printTime<br>        start_printTime()</pre><h3>6. Restart Your Server.</h3><p>After making these changes, simply restart your development server so your app could synchronize with these changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/0*AZLjKptosHek4X13.jpg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dc449b1fa9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>