{"id":21377467,"url":"https://github.com/zeyu-li/django-tutorial","last_synced_at":"2025-03-16T09:45:05.962Z","repository":{"id":53442018,"uuid":"275722772","full_name":"Zeyu-Li/django-tutorial","owner":"Zeyu-Li","description":"A tutorial for Django projects 🏫","archived":false,"fork":false,"pushed_at":"2021-03-30T13:56:22.000Z","size":664,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T22:09:33.499Z","etag":null,"topics":["django","django-tutorial","tutorial"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Zeyu-Li.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-29T03:38:55.000Z","updated_at":"2022-03-27T15:13:28.000Z","dependencies_parsed_at":"2022-08-17T20:05:22.651Z","dependency_job_id":null,"html_url":"https://github.com/Zeyu-Li/django-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zeyu-Li%2Fdjango-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zeyu-Li%2Fdjango-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zeyu-Li%2Fdjango-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zeyu-Li%2Fdjango-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zeyu-Li","download_url":"https://codeload.github.com/Zeyu-Li/django-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243852431,"owners_count":20358270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-tutorial","tutorial"],"created_at":"2024-11-22T09:21:05.961Z","updated_at":"2025-03-16T09:45:05.938Z","avatar_url":"https://github.com/Zeyu-Li.png","language":"Python","readme":"# Django Tutorial\n\nThis is a Django tutorial that goes through the in and outs of Django, a **Python** based **back-end web framework**. Django is very flexible and modular system that offers users a complete and faster way to develop back-end code \n\n\n\n\u003ca name='top'\u003e\u003c/a\u003e\n\n## Index\n\n1. [Getting Started](#start)\n2. [Setting Up](#setup)\n3. [Running](#running)\n4. [Project Overview](#overview)\n5. [Starting an App](#app)\n6. [Notes Before we Start](#note)\n7. [Viewing a Homepage](#homepage)\n8. [Frontend](#front)\n9. [Login](#login)\n10. [End Note \u0026 Resources](#resources)\n11. [Miscellaneous](#misc)\n\n\n\n\u003ca name='start'\u003e\u003c/a\u003e\n\n## 1. Getting Started\n\nTo start, you must have the following requirements\n\n* [Python](https://www.python.org/) (latest version is recommended, I am using 3.8.3)\n* IDE ([VS Code](https://code.visualstudio.com/) is recommended)\n* [Django](https://www.djangoproject.com/) module\n\nTo see how to setup Django, see the [setting up section](#setup) (below)\n\n\n\n\\* Note, before starting this course, make sure you are comfortable with Python and the command line. \n\n\u003ca name='setup'\u003e\u003c/a\u003e\n\n## 2. Setting Up\n\nAssuming you have Python (+ added to the **PATH**) and your IDE installed, we will go through installing Django properly. \n\nFirst we need a shell/terminal open (PowerShell in Windows)\n\n\n\nAn optional but **strongly recommended** step is to setup a virtual environment to isolate your Django project code from other Python projects. In addition, if you need to setup this project on a production server, this will help immensely. To install a virtual environment do a pip install exactly like the following:\n\n```python\npip install virtualenv\n```\n\nAfterwards, you need to create a virtual environment with\n\n```shell\npy -m venv env\n```\n\nNext you want to activate/go into the virtual env so in Windows enter:\n\n```powershell\n.\\env\\Scripts\\activate\n```\n\nOtherwise, if you are on Mac or Linux, \n\n```shell\nsource env/Scripts/activate\n```\n\nNow you have isolated Django from your other Python modules\n\n\\* Note you can exit the virtual env by using\n\n```shell\ndeactivate\n```\n\n\n\nNext install the requirements modules\n\n```powershell\npip install -r requirements.txt\n```\n\n Right now, this is just Django, so you could have done\n\n```shell\npip install Django\n```\n\n\n\nAfter virtual env and Django where installed, we want to start a new Django project so use the startproject command\n\n```shell\ndjango-admin startproject #youProjectNameHere\n```\n\nFor this tutorial, I will be naming it **Django_tutorial**\n\n\n\n\u003ca name='running'\u003e\u003c/a\u003e\n\n## 3. Running\n\nOnce you are in a virtual environment, you can go into template folder with the following command:\n\n```\ncd .\\template\\\n```\n\nNext to start the Django project\n\n```shell\npy .\\manage.py runserver\n```\n\nNow you can go to http://127.0.0.1:8000/ and see the Django default homepage\n\n\n\n\u003ca name='overview'\u003e\u003c/a\u003e\n\n## 4. Project Overview\n\nNow let us take a look at the Django project once generated.\n\nYou will see in the Django project there are 3 things generated\n\n1. db.sqlite3 - the database for all the important info stored by Django (ie passwords, username, etc.). You can change from a SQLite database to another database style of your choice (ie. mySQL). \n2. manage.py - the main initializer for your Django project\n3. Django project name folder - a folder containing critical info about your Django project\n\nNow let's dive into the Django project files\n\n* asgi.py and wsgi.py - are files that communicate with the web server (probably going to be [NGINX](https://www.nginx.com/))\n\n\n\nAll of the files above should not be changed unless you absolutely 100% sure you know what you are doing\n\n* urls.py - this is where you specify URL paths so Django knows how to link a URL request to a webpage. You will be working with this script a lot to direct the user to the right web page \n* settings.py - defines the settings of the entire project. There are many things inside the settings that are important, so here are some of the most important things\n  * This is the place where you can add modules to the project\n  * You can specify the path of your static files (ie CSS, JavaScript, Images)\n  * Change database engine\n  * Changing project into a production build\n  * Secret Key - a salt for your hashes; keep this secret by following this tutorial -\u003e https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/#secret-key\n\n\n\n\u003ca name='app'\u003e\u003c/a\u003e\n\n## 5. Starting an App\n\nNow we have Django set up, we need to start an **app**. In Django, apps are what is used to modularize the code. Each app can have its own functionality. To start the first app, enter\n\n```shell\ndjango-admin startapp #appNameHere\n```\n\nFor this tutorial, I will name it tutorial (I recommend apps being lowercase while the project is in uppercase for the sake of clarity)\n\nNow we see a new folder called login\n\n\n\nNow let us explore app folder. There are 5 files\n\n* admin.py - dealing with the admin site\n* apps.py - specifies some info about the app, won't need to touch it much\n* tests.py - testing things\n* models.py - changing the models relating to the app\n* views.py - handles the rendering of the webpage, you will work with this and models.py the most\n\nKnow we understand the basics about the app, we can work on linking it up to the project\n\nTo so this, we go to the INSTALLED_APPS in the settings and add the name of the app at the last element of the list\n\n\n\n\u003ca name='note'\u003e\u003c/a\u003e\n\n## 6. Notes Before we Start\n\nBefore we start the main course, there are some things that you will need to know\n\nOnce you make a new model (will explore these later), you will have to use\n\n```\ndjango-admin makemigrations\n```\n\nIf you decide to change the model, you will need to \n\n```shell\npython manage.py migrate\n```\n\nto migrate changes\n\n\n\n\u003ca name='homepage'\u003e\u003c/a\u003e\n\n## 7. Viewing a Homepage\n\nNow to make a homepage, go to the urls.py, import the views in the new app with\n\n```python\nfrom tutorial import views\n```\n\nthen add a path to views.home (which we will make later). \n\n```python\nurlpatterns = [\n    ...\n    path('', views.home, name='home'), #the name is so we can identify it later\n]\n```\n\nNow we can go to the views in the app and direct to the homepage\n\n```python\n# views.py\nfrom django.shortcuts import render\n\ndef home(request):\n    return render(request, 'tutorial/home.html')\n```\n\nNow you need to make a templates folder in the app directory\n\nWithin that template folder, you should make another folder of the same name of the app and then the home.html file. If you want to place the template folder in the main project directory, you must add \n\n```python\nTEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [os.path.join(BASE_DIR, 'templates')], # directory is in base project dir\n        ...\n```\n\nto tell Django where your templates are located.\n\n\n\n\u003ca name='front'\u003e\u003c/a\u003e\n\n## 8. Frontend\n\nWorking with Django front end is very similar to working with a React or Angular template. You start off with a regular html page with other static files in its own separate folder in the main project\n\nLike the template above, if you wish to store the static files in the main project folder, you must specify by adding\n\n```python\nSTATICFILES_DIRS = (\n    os.path.join(BASE_DIR, 'static'),\n)\n```\n\nin the settings at the end of the file\n\nNow if you need static images, css, etc., you must specify in the template. \n\nTo do that, have \n\n```django\n{% load static %} # at the start of the file\n```\n\nif you want to specify something stored in static, you must use the \n\n```django\n{% static 'css/index.css' %}\n```\n\nAs you may have notices, most Django expressions have the **{%** #someExpressionHere **%}** format. In these expressions, you can have **if statements, for loops, links to other URLs, and much, much more**\n\n\n\n\\* **Important Note**, if you are updating your front end, but your browser is not showing any changes, it is because the browser is caching the css and js, therefore, you have to hard reset the page (usually the keyboard shortcut is Ctrl+f5)\n\n\u003ca name='login'\u003e\u003c/a\u003e\n\n## 9. Login\n\nDjango comes with an authentication system for all to use, but I recommend using **django-allauth** because it supports using other services (ie. Google, Facebook + more) as optional logins. This is usually the way most people will sign in and optionally it can be the only way people sign in. This can be useful because you won't have to deal with passwords offload it to that other service. \n\ndjango-allauth can be installed by following the allauth [docs here](https://django-allauth.readthedocs.io/en/latest/installation.html). \n\nSee below for more resources on how to login\n\n\n\n\u003ca name='resources'\u003e\u003c/a\u003e\n\n## 10. End Note \u0026 Resources\n\nI hope you enjoyed this Django tutorial. I know this may have been short, and I have barely scratched the surface of Django's capabilities, but there are many resources online that can help you on your Django journey. In terms of next steps, I recommend making a Django ecommerce website as that is a fairly complex project that Django was designed for. Now I will list some resources for making a Django ecommerce website and some helpful Django documentation;\n\n* Official Django documentation: https://www.djangoproject.com/start/\n* YouTube ecommerce playlist by JustDjango: https://www.youtube.com/watch?v=z4USlooVXG0\u0026list=PLLRM7ROnmA9F2vBXypzzplFjcHUaKWWP5\n\n\\* BTW, JustDjango has some more amazing tutorials for all things Django, check it out: https://www.youtube.com/channel/UCRM1gWNTDx0SHIqUJygD-kQ\n\n* If you have a library card, most likely, they have access to [Lynda.com](https://www.lynda.com/), where has some Django courses\n\n\n\n\u003ca name='misc'\u003e\u003c/a\u003e\n\n## 11. Miscellaneous\n\n**Convenience Scripts**\n\nIf you look in the main repo directory, you will see 4 script files, 2 open and 2 start scripts. \n\nThe open scripts are to open the project in the virtual environment and to cd into the Django directory\n\nThe start is the same as open but includes running the server as well. \n\n\n\n[**⬆ Back to top**](#top)\n\n## License\n\nThis code is under MIT licence.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeyu-li%2Fdjango-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeyu-li%2Fdjango-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeyu-li%2Fdjango-tutorial/lists"}