{"id":21017524,"url":"https://github.com/rootz491/django","last_synced_at":"2025-06-16T03:12:48.207Z","repository":{"id":40477505,"uuid":"311401787","full_name":"rootz491/django","owner":"rootz491","description":"i'll try to learn django for fun! but i'm not very confident about it. I mean it's python if you know what i'm saying ;) ","archived":false,"fork":false,"pushed_at":"2022-05-05T21:12:11.000Z","size":21062,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-08T21:47:26.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rootz491.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-09T16:40:32.000Z","updated_at":"2023-04-11T15:33:00.000Z","dependencies_parsed_at":"2022-08-09T21:40:21.941Z","dependency_job_id":null,"html_url":"https://github.com/rootz491/django","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rootz491/django","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootz491%2Fdjango","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootz491%2Fdjango/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootz491%2Fdjango/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootz491%2Fdjango/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rootz491","download_url":"https://codeload.github.com/rootz491/django/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootz491%2Fdjango/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260089912,"owners_count":22957151,"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":[],"created_at":"2024-11-19T10:19:43.066Z","updated_at":"2025-06-16T03:12:48.171Z","avatar_url":"https://github.com/rootz491.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python (for IoT probably)\n\n\n\n\n## django3\n\n\t\u003e\tto resolve django version conflict, i've installed virtualenvwrapper to work on some specific version of django on specific projects.\n\n\n\n\n\n### virtualenvwrapper commands:\n\n```shell script\n$ mkvirtualenv env_name\t\t\t(create new enviroment)\n$ deactivate\t\t\t\t\t(to deactivate current enviroment)\n$ workon\t\t\t\t\t\t(list of all virtual enviroment)\n$ workon env_name\t\t\t\t(activate the specific python virtual enviroment)\n$ rmvirtualenv env_name\t\t\t(remove specified enviroment)\n```\n\t-\u003e  i've create virtualenv called 'django-iot'\n\t-\u003e  in there, i'm installing django for my upcoming project\n\t-\u003e  to check:\t\n```shell script\n$ python3 -m django --version\n```\n\t\u003e\tit means that django is installed inside python virtual enviroment named 'django-iot'. outside of that, there's no django named package\n\n\n\tso now after manny hours, i've resolved the error most probably\n\tfeeling very happy.\n\n\n\n\n\n\n\n\n### problem:\n\t-\u003e\tas django use SQLite as database but i've haven't installed that.\n\t-\u003e\tso i tried to install it usingn pip command.\n\t-\u003e\tbut it doesn't work!!!\n\t-\u003e\ti first visit:\t[a link](https://pypi.org/project/django-s3-sqlite/)\n\t-\u003e\tthen i went to github repo  'django-s3-sqlite' :  [a link](https://github.com/FlipperPA/django-s3-sqlite/tree/0.0.3)\n\t-\u003e\tthere i read this:\n```\n\tNewer versions of Django (v2.1+) require a newer version of SQLite (3.8.3+) than is available on AWS Lambda instances (3.7.17).\n\n\tBecause of this, you will need to download the file _sqlite3.so (available in the root of this repository) and put it at the root of your Django project.\n```\n\t-\u003e\tso i installed the file:\t[a link](https://github.com/FlipperPA/django-s3-sqlite/blob/0.0.3/_sqlite3.so)\n\t-\u003e\tand pasted it into root of django project\n\t-\u003e\tand now it worked~! \n\n\n\n\n\n\n\n\n\n\n\n\n\n### starting first webapp:\n\n\t\u003e\tim following MDN for this first project.\n\t\u003e\tproject will be 'LocalLibrary'\n\t\u003e\tfull project:\t[a link](https://github.com/mdn/django-locallibrary-tutorial)\n\n\t-\u003e\tsetting up basics:\n```shell script\n$\tdjango-admin startproject \u003cproject-name\u003e\n```\n\t\t\u003e\tproject skeleton will be like:\n\n\t\t\t\u003cproject-name\u003e/\t\t\t\t# root folder\n\t\t\t    manage.py \t\t\t\t# main script to start server\n\t\t\t    \u003cproject-name\u003e/\t\t\t# Website/project\n\t\t\t        __init__.py \t\t# empty file that treat this directory as Python package.\n\t\t\t        settings.py \t\t# website settings\n\t\t\t        urls.py \t\t\t# site URL-to-view mappings\n\t\t\t        wsgi.py \t\t\t# help application communicate with webserver\n\t\t\t        asgi.py \t\t\t# asynchronous successor to WSGI\n\n\t\t\u003e\tto createt specific application inside project.\n```shell script\n$\tpython3 manage.py startapp \u003capp-name\u003e\n```\n\t\t\u003e\tupdated project directory should now look like this:\n\t\t\t\u003cproject-name\u003e/\n\t\t\t    manage.py\n\t\t\t    \u003cproject-name\u003e/\n\t\t\t    \u003capp-name\u003e/\t\t\t# newly created application\n\t\t\t        admin.py\n\t\t\t        apps.py\n\t\t\t        models.py\n\t\t\t        tests.py\n\t\t\t        views.py\n\t\t\t        __init__.py\n\t\t\t        migrations/\t\t# automatically update database as  modify the models\n\n\t\t\u003e\tafter creating application, time to register:\n\t\t\u003e\tgo to 'django_projects/\u003cproject-name\u003e/\u003cproject-name\u003e/settings.py'\n\t\t\u003e\tadd the \u003capp-name\u003e to INSTALLED_APPS list. \n\t\t\u003e\tThen add a new line at the end of the list (comma)\n\n\t\t\u003e\texample:\n```python\nINSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    '\u003capp-name\u003e', \n]\n```\n\t\t\u003e\tnext step will be setting up database. for that, first resolve issue by adding '_sqlite3.so' file in root of folder (read problem section for details)\n\n\t\t\u003e\tthen, everything related to database remains same as we are using 'sqlite'. so let's move on!\n\n\t\t\u003e\tnow setup TIME_ZONE, use this:\n\n```python\n\t\t\tTIME_ZONE = 'Europe/London'\n```\n\t\t\u003e\tsetting up URL mapper (urls.py)\n\n\t\t\u003e\tso add url to our new app.\n\n\t\t\u003e\tin other words, open 'django_projects/\u003cproject-name\u003e/\u003cproject-name\u003e/urls.py'\n\n\t\t\u003e\t Use include() to add paths from the application \n\n```python\nfrom django.urls import include\n\nurlpatterns += [\n    path('\u003capp-name\u003e/', include('\u003capp-name\u003e.urls')),\n]\n```\n\t\t\u003e\tAdd URL maps to redirect the base URL to our application\n\n```python\nfrom django.views.generic import RedirectView\nurlpatterns += [\n    path('', RedirectView.as_view(url='catalog/', permanent=True)),\n]\n```\n\t\t\u003e\tto use static files like css, js.\n\n\t\t\u003e\tUse static() to add url mapping to serve static files during development (only)\n\n```python\nfrom django.conf import settings\nfrom django.conf.urls.static import static\n\nurlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)\n```\n\n\t\t\u003e\tnow save and close this file.\n\n\t\t\u003e\tthen, create urls.py inside \u003capp-name\u003e folder and add this,\n\n```python\nfrom django.urls import path\nfrom . import views\n\nurlpatterns = [\n\n]\n```\n\t\t\u003e\tfinally done with URL mapping!\n\n\t\t\u003e\tAt this point we have a complete skeleton project\n\n\n\n\n\n\n\n\n\n\n#### Running database migrations\n\t\n\t\u003e\tRunning database migrations\n```shell script\t\n$ python3 manage.py makemigrations\n$ python3 manage.py migrate\n```\n\t\u003e\t'makemigrations' command creates (but does not apply) the migrations for all applications installed in project.\n\n\t\u003e\t'migrate' command is what applies the migrations to your database.\n\n\t\u003e\trunning the website\n```shell script\n$ python3 manage.py runserver\n```\n\n\n\n\n\n```\n[DAY 2]\n```\n\n\n\n\n\n### models:\n\n```python\n\nfrom django.db import models\n\nclass MyModelName(models.Model):\n    \"\"\"A typical class defining a model, derived from the Model class.\"\"\"\n\n    # Fields\n    my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')\n    ...\n\n    # Metadata\n    class Meta: \n        ordering = ['-my_field_name']\n\n    # Methods\n    def get_absolute_url(self):\n        \"\"\"Returns the url to access a particular instance of MyModelName.\"\"\"\n        return reverse('model-detail-view', args=[str(self.id)])\n    \n    def __str__(self):\n        \"\"\"String for representing the MyModelName object (in Admin site etc.).\"\"\"\n        return self.my_field_name\n\n```\n\n\n\n\t\u003e\tOnce you've chosen what database you want to use, you don't need to talk to it directly at all — you just write your model structure and other code, and Django handles all the dirty work of communicating with the database for you.\n\n\t\u003e\tmodels contains fields(variales), metadata(classes) and methods(functions.\n\n\n\n\n\n\n#### 1. fields:\n\n```python\nmy_field_name = models.CharField(max_length=20, help_text='Enter field documentation')\n```\n\n\t\u003e\tFIELD represents a column of data that we want to store in one of our database tables.\n\n\t\u003e\tin this example, 'my_field_name' is FIELD and of TYPE 'models.CharField'.\n\n\t\u003e\tFIELD TYPE determine the type of record that is used to store the data in the database, along with validation criteria to be used when values are received from an HTML form.\n\n\t\u003e\tFIELD TYPE 'models.CharField' means that this field will contain strings of alphanumeric characters.\n\n\t\u003e\tFIELD TYPE can also take arguments that further specify how the field is stored or can be used.\n\n\t\t-\u003e\t'max_length=20'\t:\tmax length of value in the field\n\n\t\t-\u003e\t'help_text=\"Enter field documentation\"' : works as a place holder\n\n\t\u003e\tFIELD NAME is use to refer to it in queries and templates.\n\n\t\u003e\tNOTE:\t\n\n\t\t-\u003e\twhen the label is used as a form label through Django frame, the first letter of the label is capitalized. \n\n\t\t\tEXAMPLE:\t'my_field_name' would be 'My field name'\n\n\t\t-\u003e\tthe order that fields are declared will affect their default order if a model is rendered in a form, thouth this may be overwritten.\n\n\n\n\n\n\n\n##### common field types:\n\n\t\u003e\tCharField:\tshort-to-mid sized fixed-length strings.\n\t\t\t\t\t'max-length' argument is must!\n\n\t\u003e\tIntegerField:\tintegers\n\n\t\u003e\tDateField \u0026 DateTimeField:\tdate and date/time information.\n\t\t\t\t\t\t\t\t\t\n\t\t-\u003e\tthese fields can additional declare the parameters: \n\t\t\t\u003e\tauto_now=True:\tset field to the current date every time model is saved.\n\t\t\t\u003e\tauto_now_add:\tonly set the date when the model is first created.\n\t\t\t\u003e\tdefault:\tset a default date that can be overridden by the user.\n\n\t\u003e\t EmailField:\tstore and validate the email.\n\n\t\u003e\tFileField \u0026 ImageField:\tuse to upload files and images respectively.\n\t\t\t\t\tThese have parameter to define how and where the uploaded files are stored.\n\n\t\u003e\tAutoField:\tspecial type of 'IntegerField' that automatically increments.\n\t\t\t\tA primary key of this type is automatically added if not explicitly specify one.\n\n\t\u003e\tForeignKey:\tuse to specify one-to-many relationships to another database model.\n\n\t\u003e\tManyToManyField:\tuse to specify many-to-many relationship.\n\n\t\u003e\tmore:\thttps://docs.djangoproject.com/en/2.1/ref/models/fields/#field-types\n\n\n\n\n\n\n\n\n\n\n##### common field arguments:\n\n\t\u003e\thelp_text:\tplaceholder for HTML forms\n\n\t\u003e\tverbose_name:  name for field used in field labels.\n\t\t\t\tif not specified, Django will be infer the default verbose name from field name.\n\n\t\u003e\tdefault:\tdefault value for the field.\n\n\t\u003e\tnull:\tif True, store blank values as NULL. default False.\n\t\t\tCharField will instread store empty string.\n\n\t\u003e\tblank:\tallow field to be blank in forms. default False.\n\n\t\u003e\tchoices:\ta group of choices for this field.\n\t\t\t\tif provided, default form element will be 'select box' with these choises instead of standard text field.\n\n\t\u003e\tprimary_key:\tif True, set the current field as primary key for model.\n\n\t\u003e\tfor more:\thttps://docs.djangoproject.com/en/2.1/ref/models/fields/#field-options\n\n\n\n\n\n#### 2. Metadata:\n\n\t\u003e\tcan declare model-level metadata for Model by declaring class Meta.\n\n```python\nclass Meta:\n\tordering = ['-my_field_name', 'another_field_name']\n```\n\n\t\u003e\tordering attribute control the default ordering of records returned when query the model type.\n\n\t\u003e\tordering will depend on FIELD TYPES.\n\n\t\t-\u003e\tcharacter fields are sorted alphabetically.\n\n\t\t-\u003e\tdata fields are sorted in chronological order.\n\n\t\u003e\tprefix the FIELD NAME with minus symbol (-) to reverse the sorting order.\n\n```python\nverbose_name = 'BetterName'\n```\n\n\t\u003e\ti don't understand this atleast for now!\n\n\t\u003e\tmore:\thttps://docs.djangoproject.com/en/2.1/ref/models/options/\n\n\n\n#### 3. Methods:\n\n```python\ndef __str__(self):\n\treturn self.field_name\n```\n\t\n\t\u003e\tin every model you should define the standard Python class method __str__() to return \n\ta human-readable string for each object.\n\n```python\ndef get_absolute_url(self):\n\treturn reverse('model-detail-view', args = [str(self.id)])\n```\n\t\n\t\u003e\treturns a URL for displaying individual model records on the website.\n\t\t'self.id' is to grab particular record.\n\n\n\n```\nwith all these done. now we can create, update or delete records, and to run \nqueries to get all records or perticular subset of records.\n```\n\n\n\n\n\n\n\n\n### Model Management:\n\n\n#### Create and modify records:\n\n```python\n#create new model using model's constructor\nrecord = MyModelName(my_field_name = 'Instance #1')\n\n#save the object into database\nrecord.save()\n```\n\n\t\u003e\tto create a record we can define an instance of the model and then call save().\n\n\t\u003e\tif no primary key decared, new record will be given one, with FIELD NAME ID.\n\n```python\n\n# access the model field values\nprint(record.id)\t\t\t# return 1\nprint(record.my_field_name)\t\t# return 'Instance #1'\n\n# modify the fields, then call save()\nrecord.my_field_name = 'New Instance Name'\nrecord.save()\n\n```\n\n\t\u003e\tcan access the fields in this new record using dot syntax and change values.\n\n\n\n\n\n\n#### Searching for records\n\n\t\u003e\tcan search for records that matches certain criteria using the model's 'objects' attribute.\n\n```python\n\nall_books = Book.objects.all()\n\n```\n\n\t\u003e\tget all the records for the model as an iterable object (QuerySet).\n\n```python\n\nwild_books = Book.objects.filter(title__contains='wild')\nnunber_wild_books = wild_books.count()\n\n```\n\n\t\u003e\tfilter() method to filter the returned QuerySet\n\n\t\u003e\ttitle__contains =\u003e 'field_name' + '__' + 'match_type'\n\n\t\t-\u003e\ttitle  =  field_name\n\t\t-\u003e\tcontains  =  match_type\n\n\t\u003e\tfull list of 'match_type':\thttps://docs.djangoproject.com/en/2.1/ref/models/querysets/#field-lookups\n\n\n```python\n\n# Will match on: Fiction, Science fiction, non-fiction etc.\nbooks_containing_genre = Book.objects.filter(genre__name__icontains='fiction')\n\n\n```\n\n```\n\nNote: You can use underscores (__) to navigate as many levels of relationships \n(ForeignKey/ManyToManyField) as you like. For example, a Book that had different\ntypes, defined using a further \"cover\" relationship might have a parameter name:\ntype__cover__name__exact='hard'.\n\n```\n\n\t\u003e\tmore on queries: https://docs.djangoproject.com/en/2.1/topics/db/queries/\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootz491%2Fdjango","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frootz491%2Fdjango","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootz491%2Fdjango/lists"}