{"id":20455326,"url":"https://github.com/alexmhack/django_pros","last_synced_at":"2026-05-06T08:36:28.377Z","repository":{"id":114089361,"uuid":"143299507","full_name":"Alexmhack/django_pros","owner":"Alexmhack","description":"working with the django 2 tutorial from joincfe.com","archived":false,"fork":false,"pushed_at":"2018-09-07T15:17:37.000Z","size":1671,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-04-08T02:14:14.055Z","etag":null,"topics":["beginner-project","beginner-tutorial","codingforentreprenuers","django-application","django2","django2-tutorial","python3","tutorial-series"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/Alexmhack.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-08-02T13:36:41.000Z","updated_at":"2023-08-27T01:37:20.000Z","dependencies_parsed_at":"2023-06-12T13:15:21.058Z","dependency_job_id":null,"html_url":"https://github.com/Alexmhack/django_pros","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Alexmhack/django_pros","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango_pros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango_pros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango_pros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango_pros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alexmhack","download_url":"https://codeload.github.com/Alexmhack/django_pros/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango_pros/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32685012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-06T08:33:17.875Z","status":"ssl_error","status_checked_at":"2026-05-06T08:33:17.221Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["beginner-project","beginner-tutorial","codingforentreprenuers","django-application","django2","django2-tutorial","python3","tutorial-series"],"created_at":"2024-11-15T11:18:35.008Z","updated_at":"2026-05-06T08:36:28.372Z","avatar_url":"https://github.com/Alexmhack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django_pros\nworking with the django 2 tutorial from joincfe.com\n\n```\nblank=True | blank=False\nnull=True | null=False\n```\n\nblank=True in a field means we are setting the field to be not required, it will appear as light in\nthe admin site, and blank=False means this field is required and it will appear bold\n\nnull=True means that this field can have a null value in the database, which means that this field \nwill be there in database but with a empty value and null=False means this field has to have a value\nto be stored in the database.\n\n```\nfrom django.http import HttpResponse\n```\n\nWe can render raw html on the page using the HttpResponse method, by just wrapping our html with this \nmethod\n\n```\nfrom django.shortcuts import render\n```\n\nWe can activate our view using the request that our browser sends when we look for the url, we get \naccept that url using the request arg and render takes in the request, renders our html file and also\na context dict that we can use in our html\n\n```\n{{ request.user }} | {{ request.user.is_authenticated }}\n```\n\nrequest is an object that django passes with each url and it has many properties like the user which\ntells which is the current user that is accessing the url or we can check if the user is \nauthenticated in the admin users, it will return a boolean value.\n\n```\n{{ title|title }} | {{ title|capfirst }}\n{{ html_context|safe }} | {{ html_context|striptags }}\n{{ number|add:50 }}\n```\n\nThere are a ton of template filter tags that can ease our work in the html itself, we use filters by \nplacing a | pipe after the context variable and then using our filter\n\n|add filter adds a number or number in a string to another number context\n|title makes the context a title, with capital letter, |capfirst also does the same\n|safe is used when we have html tags in the context variable itself and we want html to render\n|striptags strips the html tags and only renders the words\n\n# Getting data from database\nFor getting the data from our database we can use the model objects, we can play around \nwith the database model in django shell, run shell using\n\n```\n\u003e python manage.py shell\n```\n\nNow you can import the model and use the objects to access data\n\n```\n\u003e\u003e\u003e from products.models import Product\n\u003e\u003e\u003e Product.objects.all()\n\u003e\u003e\u003e Product.objects.get(id=1)\n```\n\nThere are a ton of features that our model objects support which can be found at django2 \ndocs at official website.\n\n# Django Forms\nThe module for creating forms in django is the *forms* and we separate our forms by \ncreating new file just for our forms\n\n**products/forms.py**\n```\nfrom django import forms\n```\n\nTheir are many ways for creating a form, you can use the model itself for creating one or\njust make the form completely fulfilling your requirements\n\n**Model Form in django**\n```\nfrom django import forms\n\nclass ProductForm(forms.ModelForm):\n\tclass Meta:\n\t\tmodel = Product\n\t\tfields = [\n\t\t\t'title',\n\t\t\t'description',\n\t\t\t'price'\n\t\t]\n\n```\n\nModelForm class needs a model upon which it can create the form and you have to define the \nfields that the form will have, for all fields use ```fields = \"__all__\"```\n\nThe other way is defining your own fields.\n\n```\nfrom django import forms\n\nclass RawProductForm(forms.Form):\n\ttitle = forms.CharField()\n\tdescription = forms.CharField()\n\tprice = forms.DecimalField()\n\n```\n\nThe advantage of using raw fields in our form is that we can use the features of the forms\nfields and make our form more nicer looking in html. Some of the features are\n\n```\nrequired=False\t (default is True so we don't mention it in field)\nlabel=''\t(default is the name of field but we can override it or just make it empty and use label tags)\nattrs={} \t(a dict that can have many key value pair)\n```\n\n**products/forms.py**\n```\nclass RawProductForm(forms.Form):\n\ttitle = forms.CharField(\n\t\tlabel='',\n\t\twidget=forms.TextInput(\n\t\t\tattrs={\n\t\t\t\t\"placeholder\": \"Your Title\",\n\t\t\t\t\"class\": \"form-control\",\n\t\t\t}\n\t\t)\n\t)\n\tdescription = forms.CharField(\n\t\trequired=False,\n\t\twidget=forms.Textarea(\n\t\t\tattrs={\n\t\t\t\t\"class\": \"form-control\",\n\t\t\t\t\"rows\": 4,\n\t\t\t\t\"cols\": 50,\n\t\t\t\t'placeholder': \"Your Description\"\n\t\t\t}\n\t\t)\n\t)\n\tprice = forms.DecimalField(initial=299.99)\n\n```\n\nIn our attrs dictionary we can give a key of \"class\" which will then be included in our \ninput tags as classes of CSS, here we used \"form-control\" which a mdbootstrap class for \nnicer looking forms. Placholder will give us a nice text displayed in our form.\n\ndjango.forms does not have a TextField like we have in models so instead of that we use \nwidgets in forms.CharField() to render the field as a text field\n\n**products/forms.py**\n```\n\t...\n\tdescription = forms.CharField(required=False, widget=forms.Textarea)\n\t...\n```\n\nEither you can just use the default text area made by widget or specify your requirements \nin the Textarea()\n\n**products/forms.py**\n```\n\t...\n\tdescription = forms.CharField(\n\t\t\trequired=False,\n\t\t\twidget=forms.Textarea(\n\t\t\t\tattrs={\n\t\t\t\t\t\"class\": \"form-control\",\n\t\t\t\t\t\"rows\": 4,\n\t\t\t\t\t\"cols\": 50,\n\t\t\t\t\t'placeholder': \"Your Description\"\n\t\t\t\t}\n\t\t\t)\n\t\t)\n\t...\n```\n\nThere are a lot of fields and widgets in [Django Forms](https://docs.djangoproject.com/en/2.1/ref/forms/fields/)\n\n# Django Form Validation\nDjango comes with highly advanced and self-sufficient fields validations, we won't ever be \noverriding those validations but we can add our custom validation methods in form class \nusing a simple syntax for validation methods\n\nIn our forms we will add a sample validation method just for learning purposes which will \nsimply check if a word appears in the title field of form\n\n**products/forms.py**\n```\n\tclass ProductForm(forms.ModelForm):\n\t...\n\tdef clean_title(self, *args, **kwargs):\n\t\ttitle = self.cleaned_data.get('title')\n\t\tif \"DJGO\" not in title:\n\t\t\traise forms.ValidationError(\"This is not a valid title\")\n\t\treturn title\n\t...\n```\n**NOTE:** clean_title is not just a simple method which django will call when we call it in \nour views like ```object.clean_title()```, this a special method that will be validating \ntitle field, we do this by getting the title from our cleaned_data and then just use a \nsimple if statement which will raise validation error that comes built-in in django.forms\notherwise if everything is fine we return our title\n\n*Syntax for clean method*\n```\ndef clean_\u003cfield_name\u003e():\n\tfield_name = self.cleaned_data.get('field_name')\n\t# some validation here\t\n```\n\nThis simple clean method syntax can be used for all fields and we can have as many if \nstatements that check our field as we want\n\n**Setting Initial Data**\nOne cool thing you might have seen would be that some websites render form which has some \ndata previously inserted in the form fields that is somewhat related to us or something,\ndjango forms can have that thing too using the ```initial``` attribute in the form itself,\n\n**products/views.py**\n```\ndef product_create_view(request):\n\tinitial_data = {\n\t\t'title': 'ASUS Laptop',\n\t\t'description': 'Cheapest laptop with great features'\n\t}\n\tform = ProductForm(request.POST or None, initial=initial_data)\n\t...\n```\n\nWe simply insert the initial_data in the field by passing initial with that dict\n\n**NOTE:**The keys in the dict initial_data is the name of the form fields from our forms.py\nand the values of that dict represent the initial data that has to be in the form when form\nis requested with a GET request.\n\n**Updating Previous Data**\nForms also provide us with the feature of updating our previous object that is in database\nby passing in the form our object as instance\n\n```\ndef product_create_view(request):\n\tinitial_data = {\n\t\t'title': 'ASUS Laptop',\n\t\t'description': 'Cheapest laptop with great features'\n\t}\n\tobject = Product.objects.get(id=1)\n\tform = ProductForm(request.POST or None, initial=initial_data, instance=object)\n\t...\n```\n\nBut this is not what we might want to do when rendering form to users, this is just for \nour knowledge and learning\n\n# Deleting Object\nDeleting a object is very simple. You just need to get the object and use the \n```.delete()``` method on it\n\n**products/views.py**\n```\nfrom django.shortcuts import render, get_object_or_404, redirect\n\n...\ndef product_delete_view(request, id):\n\tobj = get_object_or_404(Product, id=id)\n\tif request.method == \"POST\":\n\t\t# confirm delete\n\t\tobj.delete()\n\t\treturn redirect('product-list')\n\tcontext = {\n\t\t'object': obj\n\t}\n\treturn render(request, 'products/delete.html', context)\n\n```\n\nIn the above function based view we get the object using ```get_object_or_404``` and check\nthe request method, it he method is ```\"POST\"``` that means someone has pressed the yes\nbutton in template then we delete the object and redirect to the ```product-list``` page\n\n**templates/products/delete.html**\n```\n{% extends \"base.html\" %}\n\n{% block content %}\n\t\n\t\u003cform action='{% url \"product-delete\" object.id %}' method=\"POST\"\u003e\n\t\t{% csrf_token %}\n\t\t\u003ch1\u003eDo you want to delete the product \"{{ object.title }}\"\u003c/h1\u003e\n\t\t\u003cp\u003e\u003cinput type='submit' value='Yes'\u003e\u003ca href=\"{% url 'product-list' %}\"\u003eCancel\u003c/a\u003e\u003c/p\u003e\n\t\u003c/form\u003e\n\n{% endblock %}\n\n```\n\nWe use a html form which has its action set to the url of the same page and method is \n**POST** then we have two buttons one is submit button and other just takes us back to \nproducts list\n\n**trydjango/urls.py**\n```\nfrom products.views import (\n    product_detail_view,\n    product_create_view,\n    product_list_view,\n    product_delete_view,\n)\n\n...\n    path('product/delete/\u003cint:id\u003e', product_delete_view, name='product-delete'),\n...\n```\n\nThat's it we did the MVC Stuff and if you visit the url for delete view and press the yes\nbutton the object will get deleted from the database and you will be redirected to the \nproduct list page and the deleted object no longer appears there.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango_pros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexmhack%2Fdjango_pros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango_pros/lists"}