Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a Git Branch for commenting your Project #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/pages/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# views.py is the mini app that handles all of your webpages and it's rendering.

from django.http import HttpResponse
from django.shortcuts import render

"""
This site provide a best explantion and example of *args and **kwargs: https://www.geeksforgeeks.org/args-kwargs-python/ check it out for the actual examples.

*args
The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.
The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.

What *args allows you to do is take in more arguments than the number of formal arguments that you previously defined. With *args, any number of extra arguments can be tacked on to your current formal parameters (including zero extra arguments).

For example : we want to make a multiply function that takes any number of arguments and able to multiply them all together. It can be done using *args.

Using the *, the variable that we associate with the * becomes an iterable meaning you can do things like iterate over it, run some higher order functions such as map and filter, etc.

**kwargs
The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).
A keyword argument is where you provide a name to the variable as you pass it into the function.

One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.
"""

# Create your views here.
def home_view(request, *args, **kwargs): # *args, **kwargs
print(args, kwargs)
Expand Down
4 changes: 2 additions & 2 deletions src/products/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin

from .models import Product
from .models import Product # In'.model' the '.' is known as a relative import. You are importing a 'model.py' from the same directory as this 'admin.py' which in this case is 'products/'. You are import the Product class you created in 'product/models.py'.

admin.site.register(Product)
admin.site.register(Product) # You are registering your DB Model Product to Django's Admin site which is http://127.0.0.1:8000/admin. You can now add/delete/modify you Product records from there in http://127.0.0.1:8000/admin/products. Be sure in 'trydjango/settings.py' you include 'products/' in INSTALLED_APPS section.
69 changes: 66 additions & 3 deletions src/products/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
from django.db import models
from django.urls import reverse
# Create your models here.
class Product(models.Model):
# The 'products/' app job as an app will be to store products infomation in the backend of this site.
# The code below is the DB Model (or blueprint) for the DataBase.
"""
From djangoprojects.com:

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

The basics:
Each model is a Python class that subclasses django.db.models.Model.
Each attribute of the model represents a database field.
With all of this, Django gives you an automatically-generated database-access API

After you typed up your model you will want to go 'admin.py' to register your model. For this project goto 'products/admin.py'
"""
class Product(models.Model): # 'models.Model' Means from the models module import the class Models (you can also import functions from modules but that's not the case here). We are using inheritence here. Product is inheriting the class properties and methods from models.Model
# To know the fields and the required param to use for your project please refer to: https://docs.djangoproject.com/en/2.2/ref/models/fields/
# Make it a habit to read the documentation. Use google or StackOverflow if the documentation is to hard to read.
title = models.CharField(max_length=120) # max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField(blank=False, null=False)
price = models.DecimalField(decimal_places=2, max_digits=10000) # 'decimal_places=2' e.g. .12,.56, etc. not like .2365 or anything passed two decimal places.
summary = models.TextField(blank=False, null=False)
featured = models.BooleanField(default=False) # null=True, default=True
"""
If you ever remake an entire DB model from scratch you may want
to consider deleting the DB (not always, avoid that if you can).
For this project you do that by deleting 'db.sqlite3'
then 'python manage.py makemigrations',
'python manage.py migrate' (doing these two commands will create
a new 'db.sqlite3'), and create a
superuser again using 'python manage.py createsuperuser'.

Remember you must makemigration and migrate anytime you mess with the model or DB.

Check out Try DJANGO Tutorial - 11 - Change a Model.
Refer to: https://www.youtube.com/watch?v=8tVoq291aZ8&list=PLEsfXFp6DpzTD1BD1aWNxS2Ep06vIkaeW&index=11

This is changing DB model without deleting it. Check out the './migrations/' after. No comment was added there.
"""

"""
On 'Try DJANGO Tutorial - 9 - Create Product Objects in the Python Shell'
the model before the current one above was simplistic and not good construction of
DataBase Normization (this was done on purpose for simplisty sake). What will be
written is how to do the commands using above's current fields.

Firstly, make sure you are in the same directory as 'manage.py'.
Then type 'python manage.py shell'. You need to 'from products.models import Product' to
manipuate the DB from the python shell using manage.py.

Using 'Product.objects.all()' will show you all DB QuerySet stored. Remember you can
still check your QuerySet and manipuate it using the Web GUI on
http://127.0.0.1:8000/admin/products/

To create a record type like this: "Product.objects.create(title='Some title',
price=123.12, summary='Some summary')". Noticed 'description = models.TextField
(blank=True, null=True)', 'summary = models.TextField(blank=False, null=False)',
and 'featured = models.BooleanField(default=False)'?

In the description with the field param set to 'blank=True' and 'null=True' you do not
have to set a text value (blank=True) and can be set to nothing or not using ''
(null=True). In summary you must have some string value init or Python will not enter
your code into the DB and the DB will also not accept it.

'Featured' is a field that was added later on after the Try DJANGO Tutorial - 9 video.
To fix the problem python and the DB had since there was no '(blank=True, null=True)'
the default value of 'Featured was set to False. You can add the optional params
should you wish. Like so: "Product.objects.create(title='Some title', price=123.12,
summary='Some summary', Featured=True, description='Some description')".
"""

def get_absolute_url(self):
return reverse("products:product-detail", kwargs={"id": self.id}) #f"/products/{self.id}/"
31 changes: 22 additions & 9 deletions src/trydjango/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/

SuperUser:
Admin
p@ssw0rd
"""

"""
Use 'python manage.py migrate' to sync settings onto the apps of this project.

To create apps you should use 'python manage.py startapp <appname>'.
Each app can do everything by itself but design-wise you create multiple apps that each do one thing very well. The apps in this case are the folders: blog, courses, pages, and products.
"""

import os
Expand All @@ -30,7 +41,7 @@

# Application definition

INSTALLED_APPS = [
INSTALLED_APPS = [ # More like Components that are little pieces of a greater project.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -40,11 +51,11 @@

# third party

# own
# include your own apps here
'blog',
'courses',
'pages',
'products',
'pages', # This is to set an app that will deal with the webpages.
'products', # Refer to 'products/admin.py'. Always 'python manage.py makemigration' since you are now messing with the DB. To implement you DB you have type 'python manage.py migrate' after. Go back to 'products/models.py' to learn about adding records with python shell.
]

MIDDLEWARE = [
Expand All @@ -57,7 +68,7 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'trydjango.urls'
ROOT_URLCONF = 'trydjango.urls' # How Django knows to route any given URLs.

TEMPLATES = [
{
Expand All @@ -75,24 +86,25 @@
},
]

WSGI_APPLICATION = 'trydjango.wsgi.application'
WSGI_APPLICATION = 'trydjango.wsgi.application' # This is how your server works. It goes though this setting. Some cases you change it and in others you leave it alone.


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.sqlite3', # Change where .sqlite3 is to change different Database Management System.
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # Must also place location of new DBMS. Check documentation.
# Fun fact--> 'NAME': os.path.join(BASE_DIR, 'db2.sqlite3'), --> will create another DB called db2.sqlite3 next to db.sqlite3. You have to type 'python manage.py migrate' to implement though. You should do that every time you change the settings of 'settings.py' or DB Models which will be discussed later.
}
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
AUTH_PASSWORD_VALIDATORS = [ # Validates passwords to current standard.
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
Expand Down Expand Up @@ -126,3 +138,4 @@
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

8 changes: 0 additions & 8 deletions trydjango.sublime-project

This file was deleted.

Loading