From 5990e7650a0ab04f5ebfcf68de1f65d7bb328539 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Tue, 4 Jan 2022 12:08:52 -0800 Subject: [PATCH 01/18] initial commit --- Code/James/Django-lab02/blog_proj/__init__.py | 0 Code/James/Django-lab02/blog_proj/asgi.py | 16 +++ Code/James/Django-lab02/blog_proj/settings.py | 123 ++++++++++++++++++ Code/James/Django-lab02/blog_proj/urls.py | 21 +++ Code/James/Django-lab02/blog_proj/wsgi.py | 16 +++ Code/James/Django-lab02/manage.py | 22 ++++ Code/James/Django-lab02/users_app/__init__.py | 0 Code/James/Django-lab02/users_app/admin.py | 3 + Code/James/Django-lab02/users_app/apps.py | 6 + .../users_app/migrations/__init__.py | 0 Code/James/Django-lab02/users_app/models.py | 3 + Code/James/Django-lab02/users_app/tests.py | 3 + Code/James/Django-lab02/users_app/views.py | 3 + 13 files changed, 216 insertions(+) create mode 100644 Code/James/Django-lab02/blog_proj/__init__.py create mode 100644 Code/James/Django-lab02/blog_proj/asgi.py create mode 100644 Code/James/Django-lab02/blog_proj/settings.py create mode 100644 Code/James/Django-lab02/blog_proj/urls.py create mode 100644 Code/James/Django-lab02/blog_proj/wsgi.py create mode 100644 Code/James/Django-lab02/manage.py create mode 100644 Code/James/Django-lab02/users_app/__init__.py create mode 100644 Code/James/Django-lab02/users_app/admin.py create mode 100644 Code/James/Django-lab02/users_app/apps.py create mode 100644 Code/James/Django-lab02/users_app/migrations/__init__.py create mode 100644 Code/James/Django-lab02/users_app/models.py create mode 100644 Code/James/Django-lab02/users_app/tests.py create mode 100644 Code/James/Django-lab02/users_app/views.py diff --git a/Code/James/Django-lab02/blog_proj/__init__.py b/Code/James/Django-lab02/blog_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/blog_proj/asgi.py b/Code/James/Django-lab02/blog_proj/asgi.py new file mode 100644 index 00000000..740a6d46 --- /dev/null +++ b/Code/James/Django-lab02/blog_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for blog_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.settings') + +application = get_asgi_application() diff --git a/Code/James/Django-lab02/blog_proj/settings.py b/Code/James/Django-lab02/blog_proj/settings.py new file mode 100644 index 00000000..fa91f9d3 --- /dev/null +++ b/Code/James/Django-lab02/blog_proj/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for blog_proj project. + +Generated by 'django-admin startproject' using Django 4.0.1. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-me4xdjc!pc$54xn(!&7m2i2w=5du@j%0!jnf(tm*a6ik1x*u!(' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'blog_proj.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'blog_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/James/Django-lab02/blog_proj/urls.py b/Code/James/Django-lab02/blog_proj/urls.py new file mode 100644 index 00000000..b2ae25e8 --- /dev/null +++ b/Code/James/Django-lab02/blog_proj/urls.py @@ -0,0 +1,21 @@ +"""blog_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/Code/James/Django-lab02/blog_proj/wsgi.py b/Code/James/Django-lab02/blog_proj/wsgi.py new file mode 100644 index 00000000..86ed6ae8 --- /dev/null +++ b/Code/James/Django-lab02/blog_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for blog_proj project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.settings') + +application = get_wsgi_application() diff --git a/Code/James/Django-lab02/manage.py b/Code/James/Django-lab02/manage.py new file mode 100644 index 00000000..aac5dde4 --- /dev/null +++ b/Code/James/Django-lab02/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/Code/James/Django-lab02/users_app/__init__.py b/Code/James/Django-lab02/users_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/users_app/admin.py b/Code/James/Django-lab02/users_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/James/Django-lab02/users_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/James/Django-lab02/users_app/apps.py b/Code/James/Django-lab02/users_app/apps.py new file mode 100644 index 00000000..7200dd57 --- /dev/null +++ b/Code/James/Django-lab02/users_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users_app' diff --git a/Code/James/Django-lab02/users_app/migrations/__init__.py b/Code/James/Django-lab02/users_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/James/Django-lab02/users_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/James/Django-lab02/users_app/tests.py b/Code/James/Django-lab02/users_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/James/Django-lab02/users_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/Code/James/Django-lab02/users_app/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From 87dbd7fc74458bbed8e29c2d26c75ed4b95f44ed Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Tue, 4 Jan 2022 12:16:02 -0800 Subject: [PATCH 02/18] adding base_dir for templates and static files --- Code/James/Django-lab02/blog_proj/settings.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Code/James/Django-lab02/blog_proj/settings.py b/Code/James/Django-lab02/blog_proj/settings.py index fa91f9d3..56441c24 100644 --- a/Code/James/Django-lab02/blog_proj/settings.py +++ b/Code/James/Django-lab02/blog_proj/settings.py @@ -37,6 +37,8 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + 'users_app' ] MIDDLEWARE = [ @@ -54,7 +56,7 @@ TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [str(BASE_DIR.joinpath('templates'))], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -105,7 +107,7 @@ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'America/Los_Angeles' USE_I18N = True @@ -116,6 +118,7 @@ # https://docs.djangoproject.com/en/4.0/howto/static-files/ STATIC_URL = 'static/' +STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))] # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field From fa10ffe0d8bb9ec0411eae4413b763c4016aec67 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Wed, 5 Jan 2022 19:30:26 -0800 Subject: [PATCH 03/18] added login and register views --- Code/James/Django-lab02/blog_proj/settings.py | 6 + Code/James/Django-lab02/blog_proj/urls.py | 3 +- Code/James/Django-lab02/static/css/index.css | 0 .../Django-lab02/static/css/normalize.css | 349 ++++++++++++++++++ Code/James/Django-lab02/templates/base.html | 37 ++ Code/James/Django-lab02/templates/login.html | 23 ++ .../Django-lab02/templates/register.html | 26 ++ Code/James/Django-lab02/users_app/admin.py | 4 + Code/James/Django-lab02/users_app/forms.py | 27 ++ .../users_app/migrations/0001_initial.py | 44 +++ Code/James/Django-lab02/users_app/models.py | 7 +- Code/James/Django-lab02/users_app/urls.py | 14 + Code/James/Django-lab02/users_app/views.py | 33 +- 13 files changed, 570 insertions(+), 3 deletions(-) create mode 100644 Code/James/Django-lab02/static/css/index.css create mode 100644 Code/James/Django-lab02/static/css/normalize.css create mode 100644 Code/James/Django-lab02/templates/base.html create mode 100644 Code/James/Django-lab02/templates/login.html create mode 100644 Code/James/Django-lab02/templates/register.html create mode 100644 Code/James/Django-lab02/users_app/forms.py create mode 100644 Code/James/Django-lab02/users_app/migrations/0001_initial.py create mode 100644 Code/James/Django-lab02/users_app/urls.py diff --git a/Code/James/Django-lab02/blog_proj/settings.py b/Code/James/Django-lab02/blog_proj/settings.py index 56441c24..dd737927 100644 --- a/Code/James/Django-lab02/blog_proj/settings.py +++ b/Code/James/Django-lab02/blog_proj/settings.py @@ -120,7 +120,13 @@ STATIC_URL = 'static/' STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))] +MEDIA_ROOT ='static/' + # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +AUTH_USER_MODEL = 'users_app.User' + +LOGIN_URL = '/users/login' diff --git a/Code/James/Django-lab02/blog_proj/urls.py b/Code/James/Django-lab02/blog_proj/urls.py index b2ae25e8..6bc32069 100644 --- a/Code/James/Django-lab02/blog_proj/urls.py +++ b/Code/James/Django-lab02/blog_proj/urls.py @@ -14,8 +14,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('users/', include('users_app.urls')) ] diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/static/css/normalize.css b/Code/James/Django-lab02/static/css/normalize.css new file mode 100644 index 00000000..c45a85f8 --- /dev/null +++ b/Code/James/Django-lab02/static/css/normalize.css @@ -0,0 +1,349 @@ +/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ + +/* Document + ========================================================================== */ + +/** + * 1. Correct the line height in all browsers. + * 2. Prevent adjustments of font size after orientation changes in iOS. + */ + + html { + line-height: 1.15; /* 1 */ + -webkit-text-size-adjust: 100%; /* 2 */ + } + + /* Sections + ========================================================================== */ + + /** + * Remove the margin in all browsers. + */ + + body { + margin: 0; + } + + /** + * Render the `main` element consistently in IE. + */ + + main { + display: block; + } + + /** + * Correct the font size and margin on `h1` elements within `section` and + * `article` contexts in Chrome, Firefox, and Safari. + */ + + h1 { + font-size: 2em; + margin: 0.67em 0; + } + + /* Grouping content + ========================================================================== */ + + /** + * 1. Add the correct box sizing in Firefox. + * 2. Show the overflow in Edge and IE. + */ + + hr { + box-sizing: content-box; /* 1 */ + height: 0; /* 1 */ + overflow: visible; /* 2 */ + } + + /** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + + pre { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ + } + + /* Text-level semantics + ========================================================================== */ + + /** + * Remove the gray background on active links in IE 10. + */ + + a { + background-color: transparent; + } + + /** + * 1. Remove the bottom border in Chrome 57- + * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. + */ + + abbr[title] { + border-bottom: none; /* 1 */ + text-decoration: underline; /* 2 */ + text-decoration: underline dotted; /* 2 */ + } + + /** + * Add the correct font weight in Chrome, Edge, and Safari. + */ + + b, + strong { + font-weight: bolder; + } + + /** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + + code, + kbd, + samp { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ + } + + /** + * Add the correct font size in all browsers. + */ + + small { + font-size: 80%; + } + + /** + * Prevent `sub` and `sup` elements from affecting the line height in + * all browsers. + */ + + sub, + sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; + } + + sub { + bottom: -0.25em; + } + + sup { + top: -0.5em; + } + + /* Embedded content + ========================================================================== */ + + /** + * Remove the border on images inside links in IE 10. + */ + + img { + border-style: none; + } + + /* Forms + ========================================================================== */ + + /** + * 1. Change the font styles in all browsers. + * 2. Remove the margin in Firefox and Safari. + */ + + button, + input, + optgroup, + select, + textarea { + font-family: inherit; /* 1 */ + font-size: 100%; /* 1 */ + line-height: 1.15; /* 1 */ + margin: 0; /* 2 */ + } + + /** + * Show the overflow in IE. + * 1. Show the overflow in Edge. + */ + + button, + input { /* 1 */ + overflow: visible; + } + + /** + * Remove the inheritance of text transform in Edge, Firefox, and IE. + * 1. Remove the inheritance of text transform in Firefox. + */ + + button, + select { /* 1 */ + text-transform: none; + } + + /** + * Correct the inability to style clickable types in iOS and Safari. + */ + + button, + [type="button"], + [type="reset"], + [type="submit"] { + -webkit-appearance: button; + } + + /** + * Remove the inner border and padding in Firefox. + */ + + button::-moz-focus-inner, + [type="button"]::-moz-focus-inner, + [type="reset"]::-moz-focus-inner, + [type="submit"]::-moz-focus-inner { + border-style: none; + padding: 0; + } + + /** + * Restore the focus styles unset by the previous rule. + */ + + button:-moz-focusring, + [type="button"]:-moz-focusring, + [type="reset"]:-moz-focusring, + [type="submit"]:-moz-focusring { + outline: 1px dotted ButtonText; + } + + /** + * Correct the padding in Firefox. + */ + + fieldset { + padding: 0.35em 0.75em 0.625em; + } + + /** + * 1. Correct the text wrapping in Edge and IE. + * 2. Correct the color inheritance from `fieldset` elements in IE. + * 3. Remove the padding so developers are not caught out when they zero out + * `fieldset` elements in all browsers. + */ + + legend { + box-sizing: border-box; /* 1 */ + color: inherit; /* 2 */ + display: table; /* 1 */ + max-width: 100%; /* 1 */ + padding: 0; /* 3 */ + white-space: normal; /* 1 */ + } + + /** + * Add the correct vertical alignment in Chrome, Firefox, and Opera. + */ + + progress { + vertical-align: baseline; + } + + /** + * Remove the default vertical scrollbar in IE 10+. + */ + + textarea { + overflow: auto; + } + + /** + * 1. Add the correct box sizing in IE 10. + * 2. Remove the padding in IE 10. + */ + + [type="checkbox"], + [type="radio"] { + box-sizing: border-box; /* 1 */ + padding: 0; /* 2 */ + } + + /** + * Correct the cursor style of increment and decrement buttons in Chrome. + */ + + [type="number"]::-webkit-inner-spin-button, + [type="number"]::-webkit-outer-spin-button { + height: auto; + } + + /** + * 1. Correct the odd appearance in Chrome and Safari. + * 2. Correct the outline style in Safari. + */ + + [type="search"] { + -webkit-appearance: textfield; /* 1 */ + outline-offset: -2px; /* 2 */ + } + + /** + * Remove the inner padding in Chrome and Safari on macOS. + */ + + [type="search"]::-webkit-search-decoration { + -webkit-appearance: none; + } + + /** + * 1. Correct the inability to style clickable types in iOS and Safari. + * 2. Change font properties to `inherit` in Safari. + */ + + ::-webkit-file-upload-button { + -webkit-appearance: button; /* 1 */ + font: inherit; /* 2 */ + } + + /* Interactive + ========================================================================== */ + + /* + * Add the correct display in Edge, IE 10+, and Firefox. + */ + + details { + display: block; + } + + /* + * Add the correct display in all browsers. + */ + + summary { + display: list-item; + } + + /* Misc + ========================================================================== */ + + /** + * Add the correct display in IE 10+. + */ + + template { + display: none; + } + + /** + * Add the correct display in IE 10. + */ + + [hidden] { + display: none; + } \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html new file mode 100644 index 00000000..09ab4bdd --- /dev/null +++ b/Code/James/Django-lab02/templates/base.html @@ -0,0 +1,37 @@ +{% load static %} + +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Blog</title> + + <link rel="stylesheet" href="{% static 'css/normalize.css' %}"> + <link rel="stylesheet" href="{% static 'css/index.css' %}"> + + <link + href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" + rel="stylesheet" + integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" + crossorigin="anonymous" + /> + <script + src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" + integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" + crossorigin="anonymous" + ></script> +</head> +<body> + <main class="container"> + <!-- <div class="row w-100"> + + </div> --> + + {% block content %} + {% endblock content %} + </main> + +</body> +</html> \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/login.html b/Code/James/Django-lab02/templates/login.html new file mode 100644 index 00000000..7eab03af --- /dev/null +++ b/Code/James/Django-lab02/templates/login.html @@ -0,0 +1,23 @@ +{% extends 'base.html' %} + +{% block content %} + <h1 class="h1 text-center">Log in</h1> + + <form action="{% url 'users_app:login %}" method="POST" class="d-flex flex-column align-items-center gap-3"> + + <fieldset> + <label for="id_username">{{form.username.label}}</label> + {{form.username}} + </fieldset> + + <fieldset> + <label for="id_password">{{form.passwrod.label}}</label> + {{form.password}} + </fieldset> + {% csrf_token %} + + <button type="submit" class="btn btn-success">Log in</button> + + + </form> +{% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/register.html b/Code/James/Django-lab02/templates/register.html new file mode 100644 index 00000000..af6e23a7 --- /dev/null +++ b/Code/James/Django-lab02/templates/register.html @@ -0,0 +1,26 @@ +{% extends 'base.html' %} + +{% block content %} + <h1 class="h1 text-center">Register</h1> + + <form action="{% url 'users_app:register %}" method="POST" class="d-flex flex-column align-items-center gap-3"> + <fieldset> + <label for="id_username">{{form.username.label}}</label> + {{form.username}} + </fieldset> + + <fieldset> + <label for="id_password">{{form.password.label}}</label> + {{form.password}} + </fieldset> + {% csrf_token %} + + <button type="submit" class="btn btn-success">Register</button> + + + </form> + + + {% endblock content %} + + diff --git a/Code/James/Django-lab02/users_app/admin.py b/Code/James/Django-lab02/users_app/admin.py index 8c38f3f3..3101297e 100644 --- a/Code/James/Django-lab02/users_app/admin.py +++ b/Code/James/Django-lab02/users_app/admin.py @@ -1,3 +1,7 @@ from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import User # Register your models here. + +admin.site.register(User, UserAdmin) diff --git a/Code/James/Django-lab02/users_app/forms.py b/Code/James/Django-lab02/users_app/forms.py new file mode 100644 index 00000000..cfbe5be0 --- /dev/null +++ b/Code/James/Django-lab02/users_app/forms.py @@ -0,0 +1,27 @@ +from django import forms +from .models import User + + +class UserForm(forms.ModelForm): + + class Meta: + + model = User + + fields = [ + 'first_name' + 'last_name' + ] + + + widgets = { + 'first_name': forms.TextInput(attrs={'class':'form-control'}), + 'last_name': forms.TextInput(attrs={'class':'form-control'}), + 'username': forms.TextInput(attrs={'class':'form-control', 'id':'abcdefg'}), + 'password': forms.PasswordInput(attrs={'class':'form-control'}) + } + + +class UserAuthForm(UserForm): + class Meta(UserForm.Meta): + fields = ['username', 'password'] \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/migrations/0001_initial.py b/Code/James/Django-lab02/users_app/migrations/0001_initial.py new file mode 100644 index 00000000..9b04a7a5 --- /dev/null +++ b/Code/James/Django-lab02/users_app/migrations/0001_initial.py @@ -0,0 +1,44 @@ +# Generated by Django 4.0.1 on 2022-01-06 02:37 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), + ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), + ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), + ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py index 71a83623..754e2131 100644 --- a/Code/James/Django-lab02/users_app/models.py +++ b/Code/James/Django-lab02/users_app/models.py @@ -1,3 +1,8 @@ from django.db import models - +from django.contrib.auth.models import AbstractUser # Create your models here. + + +class User(AbstractUser): + def __str__(self): + return self.username diff --git a/Code/James/Django-lab02/users_app/urls.py b/Code/James/Django-lab02/users_app/urls.py new file mode 100644 index 00000000..5d1e6c9c --- /dev/null +++ b/Code/James/Django-lab02/users_app/urls.py @@ -0,0 +1,14 @@ +from django.urls import path +from django.urls.resolvers import URLPattern + + +from . import views + + +app_name = 'users_app' + +urlpatterns = [ + path('register/', views.register, name='register'), + path('login/', views.login, name='login'), + path('<str:username>/', views.profile, name='profile') +] \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index 91ea44a2..f3af18d7 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -1,3 +1,34 @@ -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render, redirect, reverse +from django.contrib.auth import ( + get_user_model, + authenticate, + login as django_login, + logout as django_logout +) + +from django.contrib.auth.decorators import login_required + +from .forms import UserForm, UserAuthForm # Create your views here. +def register(request): + form = UserAuthForm() + + if request.method == 'GET': + + context = { + 'form':form + } + + return render(request, 'users/register.html', context) + + +def login(request): + if request.method == 'GET': + form = UserAuthForm() + + return render(request, 'users/login.html', {'form': form}) + + +def profile(request, username): + pass \ No newline at end of file From 06f5e3f92fae4d61fb6c4945498c9d57147c2e1b Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Wed, 5 Jan 2022 20:40:01 -0800 Subject: [PATCH 04/18] fixed login view and register view --- .../templates/{ => users}/login.html | 4 +- .../templates/{ => users}/register.html | 2 +- Code/James/Django-lab02/users_app/forms.py | 4 +- .../users_app/migrations/0001_initial.py | 44 ------------------- .../users_app/migrations/__init__.py | 0 Code/James/Django-lab02/users_app/models.py | 5 ++- Code/James/Django-lab02/users_app/views.py | 5 +++ 7 files changed, 13 insertions(+), 51 deletions(-) rename Code/James/Django-lab02/templates/{ => users}/login.html (71%) rename Code/James/Django-lab02/templates/{ => users}/register.html (81%) delete mode 100644 Code/James/Django-lab02/users_app/migrations/0001_initial.py delete mode 100644 Code/James/Django-lab02/users_app/migrations/__init__.py diff --git a/Code/James/Django-lab02/templates/login.html b/Code/James/Django-lab02/templates/users/login.html similarity index 71% rename from Code/James/Django-lab02/templates/login.html rename to Code/James/Django-lab02/templates/users/login.html index 7eab03af..3dd0daee 100644 --- a/Code/James/Django-lab02/templates/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -3,7 +3,7 @@ {% block content %} <h1 class="h1 text-center">Log in</h1> - <form action="{% url 'users_app:login %}" method="POST" class="d-flex flex-column align-items-center gap-3"> + <form action="{% url 'users_app:login' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> <fieldset> <label for="id_username">{{form.username.label}}</label> @@ -11,7 +11,7 @@ <h1 class="h1 text-center">Log in</h1> </fieldset> <fieldset> - <label for="id_password">{{form.passwrod.label}}</label> + <label for="id_password">{{form.password.label}}</label> {{form.password}} </fieldset> {% csrf_token %} diff --git a/Code/James/Django-lab02/templates/register.html b/Code/James/Django-lab02/templates/users/register.html similarity index 81% rename from Code/James/Django-lab02/templates/register.html rename to Code/James/Django-lab02/templates/users/register.html index af6e23a7..b3c68640 100644 --- a/Code/James/Django-lab02/templates/register.html +++ b/Code/James/Django-lab02/templates/users/register.html @@ -3,7 +3,7 @@ {% block content %} <h1 class="h1 text-center">Register</h1> - <form action="{% url 'users_app:register %}" method="POST" class="d-flex flex-column align-items-center gap-3"> + <form action="{% url 'users_app:register' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> <fieldset> <label for="id_username">{{form.username.label}}</label> {{form.username}} diff --git a/Code/James/Django-lab02/users_app/forms.py b/Code/James/Django-lab02/users_app/forms.py index cfbe5be0..fcbe278d 100644 --- a/Code/James/Django-lab02/users_app/forms.py +++ b/Code/James/Django-lab02/users_app/forms.py @@ -9,7 +9,7 @@ class Meta: model = User fields = [ - 'first_name' + 'first_name', 'last_name' ] @@ -17,7 +17,7 @@ class Meta: widgets = { 'first_name': forms.TextInput(attrs={'class':'form-control'}), 'last_name': forms.TextInput(attrs={'class':'form-control'}), - 'username': forms.TextInput(attrs={'class':'form-control', 'id':'abcdefg'}), + 'username': forms.TextInput(attrs={'class':'form-control'}), 'password': forms.PasswordInput(attrs={'class':'form-control'}) } diff --git a/Code/James/Django-lab02/users_app/migrations/0001_initial.py b/Code/James/Django-lab02/users_app/migrations/0001_initial.py deleted file mode 100644 index 9b04a7a5..00000000 --- a/Code/James/Django-lab02/users_app/migrations/0001_initial.py +++ /dev/null @@ -1,44 +0,0 @@ -# Generated by Django 4.0.1 on 2022-01-06 02:37 - -import django.contrib.auth.models -import django.contrib.auth.validators -from django.db import migrations, models -import django.utils.timezone - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ] - - operations = [ - migrations.CreateModel( - name='User', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('password', models.CharField(max_length=128, verbose_name='password')), - ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), - ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), - ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), - ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), - ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), - ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), - ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), - ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), - ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), - ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), - ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), - ], - options={ - 'verbose_name': 'user', - 'verbose_name_plural': 'users', - 'abstract': False, - }, - managers=[ - ('objects', django.contrib.auth.models.UserManager()), - ], - ), - ] diff --git a/Code/James/Django-lab02/users_app/migrations/__init__.py b/Code/James/Django-lab02/users_app/migrations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py index 754e2131..aed88d55 100644 --- a/Code/James/Django-lab02/users_app/models.py +++ b/Code/James/Django-lab02/users_app/models.py @@ -4,5 +4,6 @@ class User(AbstractUser): - def __str__(self): - return self.username + pass + # def __str__(self): + # return self.username diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index f3af18d7..6b70fabc 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -21,6 +21,8 @@ def register(request): } return render(request, 'users/register.html', context) + elif request.method == 'POST': + pass def login(request): @@ -29,6 +31,9 @@ def login(request): return render(request, 'users/login.html', {'form': form}) + elif request.method == 'POST': + pass + def profile(request, username): pass \ No newline at end of file From c39a5b61f2d2cd2804e03f4b4df05546163fec28 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Sat, 8 Jan 2022 05:46:50 -0800 Subject: [PATCH 05/18] adding blog post functionality --- Code/James/Django-lab02/blog_app/__init__.py | 0 Code/James/Django-lab02/blog_app/admin.py | 8 +++ Code/James/Django-lab02/blog_app/apps.py | 6 +++ Code/James/Django-lab02/blog_app/forms.py | 0 .../blog_app/migrations/__init__.py | 0 Code/James/Django-lab02/blog_app/models.py | 17 ++++++ Code/James/Django-lab02/blog_app/tests.py | 3 ++ Code/James/Django-lab02/blog_app/urls.py | 9 ++++ Code/James/Django-lab02/blog_app/views.py | 18 +++++++ Code/James/Django-lab02/blog_proj/settings.py | 3 +- Code/James/Django-lab02/blog_proj/urls.py | 3 +- Code/James/Django-lab02/templates/base.html | 54 +++++++++---------- .../Django-lab02/templates/blogs/home.html | 15 ++++++ .../Django-lab02/templates/users/profile.html | 4 ++ Code/James/Django-lab02/users_app/admin.py | 4 +- Code/James/Django-lab02/users_app/models.py | 8 +-- Code/James/Django-lab02/users_app/views.py | 43 ++++++++++++--- 17 files changed, 155 insertions(+), 40 deletions(-) create mode 100644 Code/James/Django-lab02/blog_app/__init__.py create mode 100644 Code/James/Django-lab02/blog_app/admin.py create mode 100644 Code/James/Django-lab02/blog_app/apps.py create mode 100644 Code/James/Django-lab02/blog_app/forms.py create mode 100644 Code/James/Django-lab02/blog_app/migrations/__init__.py create mode 100644 Code/James/Django-lab02/blog_app/models.py create mode 100644 Code/James/Django-lab02/blog_app/tests.py create mode 100644 Code/James/Django-lab02/blog_app/urls.py create mode 100644 Code/James/Django-lab02/blog_app/views.py create mode 100644 Code/James/Django-lab02/templates/blogs/home.html create mode 100644 Code/James/Django-lab02/templates/users/profile.html diff --git a/Code/James/Django-lab02/blog_app/__init__.py b/Code/James/Django-lab02/blog_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/blog_app/admin.py b/Code/James/Django-lab02/blog_app/admin.py new file mode 100644 index 00000000..09e54715 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin + +from .models import BlogPost + +# Register your models here. + +admin.site.register(BlogPost) + diff --git a/Code/James/Django-lab02/blog_app/apps.py b/Code/James/Django-lab02/blog_app/apps.py new file mode 100644 index 00000000..90d811d3 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'blog_app' diff --git a/Code/James/Django-lab02/blog_app/forms.py b/Code/James/Django-lab02/blog_app/forms.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/blog_app/migrations/__init__.py b/Code/James/Django-lab02/blog_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/James/Django-lab02/blog_app/models.py b/Code/James/Django-lab02/blog_app/models.py new file mode 100644 index 00000000..31a5c9b1 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/models.py @@ -0,0 +1,17 @@ +from django.db import models +from django.contrib.auth import get_user_model + +from users_app.models import User + +# Create your models here. + +class BlogPost(models.Model): + title = models.CharField(max_length=200) + body = models.TextField() + user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='user') + public = models.BooleanField() + date_created = models.DateTimeField(auto_now_add=True) + date_edited = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return f'{self.title} {self.body} {self.user} {self.body}' \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/tests.py b/Code/James/Django-lab02/blog_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/James/Django-lab02/blog_app/urls.py b/Code/James/Django-lab02/blog_app/urls.py new file mode 100644 index 00000000..62812d73 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from . import views + +app_name = 'blog_app' +urlpatterns = [ + path('', views.home, name='home'), + path('create/', views.create, name='home') +] \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/views.py b/Code/James/Django-lab02/blog_app/views.py new file mode 100644 index 00000000..c7e87c9e --- /dev/null +++ b/Code/James/Django-lab02/blog_app/views.py @@ -0,0 +1,18 @@ +from django.http.response import JsonResponse +from django.shortcuts import get_object_or_404, redirect, render +from django.urls import reverse +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse + +# from users_app.views import login + +# from .models import BlogPost +# from .forms import + +# Create your views here. + +def home(request): + pass + +def create(request): + pass \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_proj/settings.py b/Code/James/Django-lab02/blog_proj/settings.py index dd737927..5a5ae5b9 100644 --- a/Code/James/Django-lab02/blog_proj/settings.py +++ b/Code/James/Django-lab02/blog_proj/settings.py @@ -38,7 +38,8 @@ 'django.contrib.messages', 'django.contrib.staticfiles', - 'users_app' + 'users_app', + 'blog_app', ] MIDDLEWARE = [ diff --git a/Code/James/Django-lab02/blog_proj/urls.py b/Code/James/Django-lab02/blog_proj/urls.py index 6bc32069..2ce1e858 100644 --- a/Code/James/Django-lab02/blog_proj/urls.py +++ b/Code/James/Django-lab02/blog_proj/urls.py @@ -18,5 +18,6 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('users/', include('users_app.urls')) + path('users/', include('users_app.urls')), + path('', include('blog_app.urls')) ] diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html index 09ab4bdd..80b9636b 100644 --- a/Code/James/Django-lab02/templates/base.html +++ b/Code/James/Django-lab02/templates/base.html @@ -2,36 +2,36 @@ <!DOCTYPE html> <html lang="en"> -<head> - <meta charset="UTF-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <head> + <meta charset="UTF-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Blog</title> - <link rel="stylesheet" href="{% static 'css/normalize.css' %}"> - <link rel="stylesheet" href="{% static 'css/index.css' %}"> + <link rel="stylesheet" href="{% static 'css/normalize.css' %}" /> + <link rel="stylesheet" href="{% static 'css/index.css' %}" /> <link - href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" - rel="stylesheet" - integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" - crossorigin="anonymous" - /> - <script - src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" - integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" - crossorigin="anonymous" - ></script> -</head> -<body> + href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" + rel="stylesheet" + integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" + crossorigin="anonymous" + /> + <script + src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" + integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" + crossorigin="anonymous" + ></script> + </head> + <body> <main class="container"> - <!-- <div class="row w-100"> - - </div> --> - - {% block content %} - {% endblock content %} + {% if errors %} + <div class="row w-100"> + {% for error in errors %} + <div class="col-12 col-lg-6 offset-lg-3">{{error}}</div> + {% endfor %} + </div> + {% endif %} {% block content %} {% endblock content %} </main> - -</body> -</html> \ No newline at end of file + </body> +</html> diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html new file mode 100644 index 00000000..aadddf90 --- /dev/null +++ b/Code/James/Django-lab02/templates/blogs/home.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} + +{% load static %} + +{% block content %} + +<div class="row d-flex jusify-content-center pb-5"> +<!-- {% for blog in blogs %} + +{% endfor %} --> + +<h1>HELLO</h1> + + </div> +{% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html new file mode 100644 index 00000000..47172cc9 --- /dev/null +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -0,0 +1,4 @@ +{% extends 'base.html' %} +{% block content %} + +{% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/admin.py b/Code/James/Django-lab02/users_app/admin.py index 3101297e..16bde54f 100644 --- a/Code/James/Django-lab02/users_app/admin.py +++ b/Code/James/Django-lab02/users_app/admin.py @@ -1,7 +1,9 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin -from .models import User +from .models import User +# BlogPost # Register your models here. admin.site.register(User, UserAdmin) +# admin.site.register(BlogPost) diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py index aed88d55..2072a9bd 100644 --- a/Code/James/Django-lab02/users_app/models.py +++ b/Code/James/Django-lab02/users_app/models.py @@ -4,6 +4,8 @@ class User(AbstractUser): - pass - # def __str__(self): - # return self.username + + def __str__(self): + return self.username + + diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index 6b70fabc..dc532605 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -12,26 +12,55 @@ # Create your views here. def register(request): + form = UserAuthForm() if request.method == 'GET': - context = { 'form':form } - return render(request, 'users/register.html', context) + elif request.method == 'POST': - pass + + form = UserAuthForm(request.POST) + + # print(form.is_valid()) + # print(form.errors) + + if form.is_valid(): + + new_user = form.save(commit=False) + + new_user.set_password(form.cleaned_data['password']) + + new_user.save() + + return redirect(reverse('users_app:register')) + + + else: + context = { + 'form': UserAuthForm(), + 'errors': [value for value in form.errors.values()] + } + return render(request, 'users/register.html', context) def login(request): - if request.method == 'GET': - form = UserAuthForm() + # if request.method == 'GET': + # form = UserAuthForm() - return render(request, 'users/login.html', {'form': form}) + # return render(request, 'users/login.html', {'form': form}) - elif request.method == 'POST': + # elif request.method == 'POST': + # form = request.POST + + # username = form['username'] + # password = form['password'] + + # user = authenticate(request, username=username, password=password) + # print(user) pass From 6ece4008cc68ea14a53304f60a89624859f0e32f Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 08:42:39 -0800 Subject: [PATCH 06/18] django lab 2 part 2 complete --- .../Django-lab-01/todo/templates/index.html | 99 ++++++++----------- Code/James/Django-lab-01/todo/views.py | 23 ++--- Code/James/Django-lab02/blog_app/forms.py | 14 +++ .../blog_app/migrations/0001_initial.py | 29 ++++++ Code/James/Django-lab02/blog_app/models.py | 2 +- Code/James/Django-lab02/blog_app/urls.py | 4 +- Code/James/Django-lab02/blog_app/views.py | 33 +++++-- Code/James/Django-lab02/blog_proj/urls.py | 4 +- .../Django-lab02/templates/blogs/create.html | 12 +++ .../Django-lab02/templates/blogs/home.html | 15 --- .../Django-lab02/templates/blogs/profile.html | 11 +++ .../Django-lab02/templates/users/login.html | 4 +- .../Django-lab02/templates/users/profile.html | 44 ++++++++- Code/James/Django-lab02/users_app/urls.py | 2 +- Code/James/Django-lab02/users_app/views.py | 67 ++++++------- 15 files changed, 227 insertions(+), 136 deletions(-) create mode 100644 Code/James/Django-lab02/blog_app/migrations/0001_initial.py create mode 100644 Code/James/Django-lab02/templates/blogs/create.html delete mode 100644 Code/James/Django-lab02/templates/blogs/home.html create mode 100644 Code/James/Django-lab02/templates/blogs/profile.html diff --git a/Code/James/Django-lab-01/todo/templates/index.html b/Code/James/Django-lab-01/todo/templates/index.html index d0c35405..562ce638 100644 --- a/Code/James/Django-lab-01/todo/templates/index.html +++ b/Code/James/Django-lab-01/todo/templates/index.html @@ -2,71 +2,56 @@ <form action="{%url 'todo:add_todos'%}" method="POST"> {% csrf_token %} -<h1>Todo List</h1> -<div class="card "> - <div class="input-group mb-3"> - <input - type="text" - class="form-control" - placeholder="add-todos" - aria-label="add-todos" - aria-describedby="button-addon2" - name="add_todo" - /> - <div class="dropdown"> - <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"> - Priority + <h1>Todo List</h1> + <div class="card "> + <div class="input-group mb-3"> + <input type="text" class="form-control" placeholder="add-todos" aria-label="add-todos" + aria-describedby="button-addon2" name="add_todo" /> + <div class="dropdown"> + <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" + data-bs-toggle="dropdown" aria-expanded="false"> + Priority + </button> + <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1"> + {%for choice in choices%} + <div class="form-check"> + <input class="form-check-input" value="{{choice}}" type="radio" name="key" id="{{choice}}"> + <label class="form-check-label" for="flexRadioDefault1"> + {{choice}} + </label> + </div> + {%endfor%} + </ul> + + </div> + + + <button class="btn btn-outline-secondary" type="submit" id="button-addon2"> + Add todo </button> - <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1"> - {%for choice in choices%} - <div class="form-check"> - <input class="form-check-input" value="{{choice}}" type="radio" name="key" id="{{choice}}"> - <label class="form-check-label" for="flexRadioDefault1"> - {{choice}} - </label> - </div> - {%endfor%} - </ul> - - </div> - <button class="btn btn-outline-secondary" type="submit" id="button-addon2"> - Add todo - </button> +</form> +</div> +<div class="card-body p-0"> + <ul class="list-group"> - - </form> - </div> - <div class="card-body p-0"> - <ul class="list-group"> - {% for todo in todos.all%} - + <li class="list-group-item">{{todo.text}}, created: {{todo.created_date}} priority: {{todo.priority.name}} <div class="btn-group"> - <button - class="btn btn-outline-secondary" - type="button" - id="button-addon2" - name="delete" - > - delete - </button> - <button - class="btn btn-outline-secondary" - type="button" - id="button-addon2" - name="complete" - > - complete - </button> - </div> + <button class="btn btn-outline-secondary" type="button" id="button-addon2" name="delete"> + delete + </button> + <button class="btn btn-outline-secondary" type="button" id="button-addon2" name="complete"> + complete + </button> + </div> </li> - + {%endfor%} - </ul> - </div> + </ul> +</div> </div> -{%endblock%} +{%endblock%} \ No newline at end of file diff --git a/Code/James/Django-lab-01/todo/views.py b/Code/James/Django-lab-01/todo/views.py index 0b1c4a43..244be900 100644 --- a/Code/James/Django-lab-01/todo/views.py +++ b/Code/James/Django-lab-01/todo/views.py @@ -4,8 +4,8 @@ render, get_object_or_404, reverse - - ) + +) from django.http import HttpResponse, Http404 @@ -17,23 +17,23 @@ def index(request): todos = Todoitem.objects.all().order_by('created_date') - + priority = Priority.objects.all() print(priority) - + context = { 'todos': todos, 'priority': priority, - 'choices' : ['high', 'medium', 'low' ] + 'choices': ['high', 'medium', 'low'] } - return render(request, 'index.html', context) + def save_todo_item(request): - + form = request.POST - + text = form['add_todo'] name = form['key'] # print(name) @@ -41,11 +41,8 @@ def save_todo_item(request): priority, created = Priority.objects.get_or_create(name=form['key']) new_todo = Todoitem() new_todo.text = text - + new_todo.priority = priority - - - print(priority.name) + new_todo.save() return HttpResponseRedirect(reverse('todo:home')) - \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/forms.py b/Code/James/Django-lab02/blog_app/forms.py index e69de29b..1ee230d6 100644 --- a/Code/James/Django-lab02/blog_app/forms.py +++ b/Code/James/Django-lab02/blog_app/forms.py @@ -0,0 +1,14 @@ +from django import forms +from django.forms import fields, widgets + + +from .models import BlogPost + +class BlogForm(forms.ModelForm): + class Meta: + model = BlogPost + fields = ['title','body'] + widgets = { + 'title': forms.TextInput(attrs={'class': 'form-control'}), + 'body': forms.Textarea(attrs={'class': 'form-control', 'style': 'resize: none'}) + } \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/migrations/0001_initial.py b/Code/James/Django-lab02/blog_app/migrations/0001_initial.py new file mode 100644 index 00000000..f6928fa8 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 4.0.1 on 2022-01-08 19:03 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='BlogPost', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('body', models.TextField()), + ('public', models.BooleanField()), + ('date_created', models.DateTimeField(auto_now_add=True)), + ('date_edited', models.DateTimeField(auto_now_add=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/Code/James/Django-lab02/blog_app/models.py b/Code/James/Django-lab02/blog_app/models.py index 31a5c9b1..b62ff0a2 100644 --- a/Code/James/Django-lab02/blog_app/models.py +++ b/Code/James/Django-lab02/blog_app/models.py @@ -14,4 +14,4 @@ class BlogPost(models.Model): date_edited = models.DateTimeField(auto_now_add=True) def __str__(self): - return f'{self.title} {self.body} {self.user} {self.body}' \ No newline at end of file + return f'{self.title} {self.body} {self.user} {self.body} {self.date_created} {self.date_edited}' \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/urls.py b/Code/James/Django-lab02/blog_app/urls.py index 62812d73..a5bc746f 100644 --- a/Code/James/Django-lab02/blog_app/urls.py +++ b/Code/James/Django-lab02/blog_app/urls.py @@ -4,6 +4,6 @@ app_name = 'blog_app' urlpatterns = [ - path('', views.home, name='home'), - path('create/', views.create, name='home') + path('profile/', views.profile, name='profile'), + path('create/', views.create, name='create') ] \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/views.py b/Code/James/Django-lab02/blog_app/views.py index c7e87c9e..8505a871 100644 --- a/Code/James/Django-lab02/blog_app/views.py +++ b/Code/James/Django-lab02/blog_app/views.py @@ -4,15 +4,36 @@ from django.contrib.auth.decorators import login_required from django.http import JsonResponse -# from users_app.views import login +from users_app.views import login -# from .models import BlogPost -# from .forms import +from .models import BlogPost +from .forms import BlogForm # Create your views here. -def home(request): - pass +def profile(request): + blogs = BlogPost.objects.all().order_by("-date_created") + print(blogs) + context = {"blogs": blogs} + + return render(request, "blogs/profile.html", context) + + +@login_required def create(request): - pass \ No newline at end of file + if request.method == "GET": + form = BlogForm() + + return render(request, "blogs/create.html", {"form": form}) + + elif request.method == "POST": + form = BlogForm(request.POST) + + if form.is_valid(): + + new_blog = form.save(commit=False) + new_blog.user = request.user + new_blog.save() + + return redirect(reverse('blog_app:profile')) diff --git a/Code/James/Django-lab02/blog_proj/urls.py b/Code/James/Django-lab02/blog_proj/urls.py index 2ce1e858..12c4c25e 100644 --- a/Code/James/Django-lab02/blog_proj/urls.py +++ b/Code/James/Django-lab02/blog_proj/urls.py @@ -18,6 +18,6 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('users/', include('users_app.urls')), - path('', include('blog_app.urls')) + path('', include('users_app.urls')), + path('blog/', include('blog_app.urls')) ] diff --git a/Code/James/Django-lab02/templates/blogs/create.html b/Code/James/Django-lab02/templates/blogs/create.html new file mode 100644 index 00000000..8e84b76e --- /dev/null +++ b/Code/James/Django-lab02/templates/blogs/create.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} {% load static %}{% block content %} + <h1 class="text-center py3">New Blog</h1> + <div class="row"> + <div class="col-10 offset-1 col-md-6 offset-md-3"> + <form action="{% url 'blog_app:create' %}" method="POST" class="d-flex flex-column gap-3 p-3"> + {% csrf_token %} + {{form}} + <button type="submit" class="btn btn-success form-control mt-3">Submit</button> + </form> + </div> + </div> +{% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html deleted file mode 100644 index aadddf90..00000000 --- a/Code/James/Django-lab02/templates/blogs/home.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends 'base.html' %} - -{% load static %} - -{% block content %} - -<div class="row d-flex jusify-content-center pb-5"> -<!-- {% for blog in blogs %} - -{% endfor %} --> - -<h1>HELLO</h1> - - </div> -{% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/blogs/profile.html b/Code/James/Django-lab02/templates/blogs/profile.html new file mode 100644 index 00000000..489d1833 --- /dev/null +++ b/Code/James/Django-lab02/templates/blogs/profile.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} {% load static %} {% block content %} + +<div class="d-flex flex-column align-items-center"> + <ul class="list-group"> + {% for blog in blogs %} + <li class="list-group-item">Title: {{blog.title}} | {{blog.date_created}}</li> + {% endfor %} + </ul> +</div> + +{% endblock content %} diff --git a/Code/James/Django-lab02/templates/users/login.html b/Code/James/Django-lab02/templates/users/login.html index 3dd0daee..7c9488fc 100644 --- a/Code/James/Django-lab02/templates/users/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -4,7 +4,7 @@ <h1 class="h1 text-center">Log in</h1> <form action="{% url 'users_app:login' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> - + {% csrf_token %} <fieldset> <label for="id_username">{{form.username.label}}</label> {{form.username}} @@ -14,7 +14,7 @@ <h1 class="h1 text-center">Log in</h1> <label for="id_password">{{form.password.label}}</label> {{form.password}} </fieldset> - {% csrf_token %} + <button type="submit" class="btn btn-success">Log in</button> diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html index 47172cc9..114f1f1c 100644 --- a/Code/James/Django-lab02/templates/users/profile.html +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -1,4 +1,40 @@ -{% extends 'base.html' %} -{% block content %} - -{% endblock content %} \ No newline at end of file +{% extends 'base.html' %} {% load static %} {% block content %} + +<div class="row"> + <div + class="col-12 col-lg-8 offset-lg-2 d-flex gap-3 flex-column align-items-center pt-5" + > + <h1 class="py-2">{{user.username}}</h1> + </div> + + <div class="col-8 offset-2 col-lg-4 offset-lg-4 pt-5"> + <table class="table"> + <thead> + <tr> + </tr> + </thead> + <tbody> + <tr> + <th scope="row">First</th> + <td> + {% if user.first_name %} {{user.first_name}} {% else %} Not Provided + {% endif %} + </td> + </tr> + <tr> + <th scope="row">Last</th> + <td> + {% if user.last_name %} {{user.last_name}} {% else %} Not Provided + {% endif %} + </td> + </tr> + <tr> + <th scope="row">Joined</th> + <td colspan="2">{{user.date_joined|date}}</td> + </tr> + </tbody> + </table> + </div> +</div> + +{% endblock content %} diff --git a/Code/James/Django-lab02/users_app/urls.py b/Code/James/Django-lab02/users_app/urls.py index 5d1e6c9c..2a293d6a 100644 --- a/Code/James/Django-lab02/users_app/urls.py +++ b/Code/James/Django-lab02/users_app/urls.py @@ -8,7 +8,7 @@ app_name = 'users_app' urlpatterns = [ + path('', views.login, name='login'), path('register/', views.register, name='register'), - path('login/', views.login, name='login'), path('<str:username>/', views.profile, name='profile') ] \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index dc532605..faa09b17 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -3,7 +3,7 @@ get_user_model, authenticate, login as django_login, - logout as django_logout + logout as django_logout, ) from django.contrib.auth.decorators import login_required @@ -12,57 +12,58 @@ # Create your views here. def register(request): - form = UserAuthForm() - - if request.method == 'GET': + if request.method == "GET": context = { - 'form':form - } - return render(request, 'users/register.html', context) - - elif request.method == 'POST': - + "form": form + } + return render(request, "users/register.html", context) + + elif request.method == "POST": form = UserAuthForm(request.POST) - # print(form.is_valid()) # print(form.errors) - if form.is_valid(): - new_user = form.save(commit=False) - - new_user.set_password(form.cleaned_data['password']) - + new_user.set_password(form.cleaned_data["password"]) new_user.save() + return redirect(reverse("users_app:register")) - return redirect(reverse('users_app:register')) - - else: context = { - 'form': UserAuthForm(), - 'errors': [value for value in form.errors.values()] + "form": UserAuthForm(), + "errors": [value for value in form.errors.values()], } - return render(request, 'users/register.html', context) + return render(request, "users/register.html", context) def login(request): - # if request.method == 'GET': - # form = UserAuthForm() + if request.method == 'GET': + form = UserAuthForm() - # return render(request, 'users/login.html', {'form': form}) + return render(request, 'users/login.html', {'form': form}) - # elif request.method == 'POST': - # form = request.POST + elif request.method == 'POST': + form = request.POST + username = form['username'] + password = form['password'] + user = authenticate(request, username=username, password=password) - # username = form['username'] - # password = form['password'] + if user is None: + context = { + 'form': UserAuthForm(), + 'errors': ['Invalid Username or Password'] + } - # user = authenticate(request, username=username, password=password) - # print(user) - pass + return render(request, 'users/login.html', context) + + else: + django_login(request, user) + return redirect(reverse('users_app:profile', kwargs={'username': user.username})) + + def profile(request, username): - pass \ No newline at end of file + user = get_object_or_404(get_user_model(), username=username) + return render(request, 'users/profile.html', {'user':user}) From ce9930e1f25ad9adc6564b7fd07bd5a91366ee4f Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 09:09:40 -0800 Subject: [PATCH 07/18] fixed the public boolienfield in my blogpost model --- .../migrations/0002_alter_blogpost_public.py | 18 ++++++++++++++++++ Code/James/Django-lab02/blog_app/models.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 Code/James/Django-lab02/blog_app/migrations/0002_alter_blogpost_public.py diff --git a/Code/James/Django-lab02/blog_app/migrations/0002_alter_blogpost_public.py b/Code/James/Django-lab02/blog_app/migrations/0002_alter_blogpost_public.py new file mode 100644 index 00000000..356203a1 --- /dev/null +++ b/Code/James/Django-lab02/blog_app/migrations/0002_alter_blogpost_public.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.1 on 2022-01-10 17:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog_app', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='blogpost', + name='public', + field=models.BooleanField(blank=True, null=True), + ), + ] diff --git a/Code/James/Django-lab02/blog_app/models.py b/Code/James/Django-lab02/blog_app/models.py index b62ff0a2..55400ee2 100644 --- a/Code/James/Django-lab02/blog_app/models.py +++ b/Code/James/Django-lab02/blog_app/models.py @@ -9,7 +9,7 @@ class BlogPost(models.Model): title = models.CharField(max_length=200) body = models.TextField() user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='user') - public = models.BooleanField() + public = models.BooleanField(null=True, blank=True) date_created = models.DateTimeField(auto_now_add=True) date_edited = models.DateTimeField(auto_now_add=True) From ac280a2f011382e3c3a3de53f3e3eee64edcab6e Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 09:17:32 -0800 Subject: [PATCH 08/18] added the blog to the post on the profile --- .../Django-lab02/templates/blogs/profile.html | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Code/James/Django-lab02/templates/blogs/profile.html b/Code/James/Django-lab02/templates/blogs/profile.html index 489d1833..1022cbfd 100644 --- a/Code/James/Django-lab02/templates/blogs/profile.html +++ b/Code/James/Django-lab02/templates/blogs/profile.html @@ -1,9 +1,21 @@ {% extends 'base.html' %} {% load static %} {% block content %} - <div class="d-flex flex-column align-items-center"> + <h1>Blog Posts</h1> <ul class="list-group"> {% for blog in blogs %} - <li class="list-group-item">Title: {{blog.title}} | {{blog.date_created}}</li> + <li class="list-group-item"><div class="accordion" id="accordionExample"> + <div class="accordion-item"> + <h2 class="accordion-header" id="headingOne"> + <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> + {{blog.user}}: {{blog.title}} | {{blog.date_created}} + </button> + </h2> + <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> + <div class="accordion-body"> + <strong>{{blog.body}}</strong> + </div> + </div> + </div></li> {% endfor %} </ul> </div> From 4e54fee675182eabdda80c98f7f66bc10d9e5b8a Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 09:21:14 -0800 Subject: [PATCH 09/18] test to see what the __str__ function is doing --- Code/James/Django-lab02/blog_app/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/James/Django-lab02/blog_app/models.py b/Code/James/Django-lab02/blog_app/models.py index 55400ee2..0bd32fcc 100644 --- a/Code/James/Django-lab02/blog_app/models.py +++ b/Code/James/Django-lab02/blog_app/models.py @@ -14,4 +14,4 @@ class BlogPost(models.Model): date_edited = models.DateTimeField(auto_now_add=True) def __str__(self): - return f'{self.title} {self.body} {self.user} {self.body} {self.date_created} {self.date_edited}' \ No newline at end of file + return f'{self.title}' \ No newline at end of file From 74c4dbe67077c4835d3caaf62c14546593d8c62c Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 16:01:34 -0800 Subject: [PATCH 10/18] added navbar --- Code/James/Django-lab02/blog_app/urls.py | 4 +- Code/James/Django-lab02/blog_app/views.py | 4 +- Code/James/Django-lab02/blog_proj/urls.py | 2 +- Code/James/Django-lab02/templates/base.html | 6 ++ .../blogs/{profile.html => home.html} | 0 .../templates/partials/_navbar.html | 59 +++++++++++++++++++ .../Django-lab02/templates/users/login.html | 5 ++ Code/James/Django-lab02/users_app/urls.py | 1 + Code/James/Django-lab02/users_app/views.py | 6 ++ 9 files changed, 82 insertions(+), 5 deletions(-) rename Code/James/Django-lab02/templates/blogs/{profile.html => home.html} (100%) create mode 100644 Code/James/Django-lab02/templates/partials/_navbar.html diff --git a/Code/James/Django-lab02/blog_app/urls.py b/Code/James/Django-lab02/blog_app/urls.py index a5bc746f..2c8d486e 100644 --- a/Code/James/Django-lab02/blog_app/urls.py +++ b/Code/James/Django-lab02/blog_app/urls.py @@ -4,6 +4,6 @@ app_name = 'blog_app' urlpatterns = [ - path('profile/', views.profile, name='profile'), - path('create/', views.create, name='create') + path('home/', views.home, name='home'), + path('create/', views.create, name='create'), ] \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/views.py b/Code/James/Django-lab02/blog_app/views.py index 8505a871..4d985727 100644 --- a/Code/James/Django-lab02/blog_app/views.py +++ b/Code/James/Django-lab02/blog_app/views.py @@ -12,12 +12,12 @@ # Create your views here. -def profile(request): +def home(request): blogs = BlogPost.objects.all().order_by("-date_created") print(blogs) context = {"blogs": blogs} - return render(request, "blogs/profile.html", context) + return render(request, "blogs/home.html", context) @login_required diff --git a/Code/James/Django-lab02/blog_proj/urls.py b/Code/James/Django-lab02/blog_proj/urls.py index 12c4c25e..2030f324 100644 --- a/Code/James/Django-lab02/blog_proj/urls.py +++ b/Code/James/Django-lab02/blog_proj/urls.py @@ -19,5 +19,5 @@ urlpatterns = [ path('admin/', admin.site.urls), path('', include('users_app.urls')), - path('blog/', include('blog_app.urls')) + path('blog/', include('blog_app.urls')), ] diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html index 80b9636b..1b2106ef 100644 --- a/Code/James/Django-lab02/templates/base.html +++ b/Code/James/Django-lab02/templates/base.html @@ -11,6 +11,11 @@ <link rel="stylesheet" href="{% static 'css/normalize.css' %}" /> <link rel="stylesheet" href="{% static 'css/index.css' %}" /> + <link + rel="stylesheet" + href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" + /> + <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" @@ -24,6 +29,7 @@ ></script> </head> <body> + {% include 'partials/_navbar.html' %} <main class="container"> {% if errors %} <div class="row w-100"> diff --git a/Code/James/Django-lab02/templates/blogs/profile.html b/Code/James/Django-lab02/templates/blogs/home.html similarity index 100% rename from Code/James/Django-lab02/templates/blogs/profile.html rename to Code/James/Django-lab02/templates/blogs/home.html diff --git a/Code/James/Django-lab02/templates/partials/_navbar.html b/Code/James/Django-lab02/templates/partials/_navbar.html new file mode 100644 index 00000000..be159716 --- /dev/null +++ b/Code/James/Django-lab02/templates/partials/_navbar.html @@ -0,0 +1,59 @@ +{% load static %} +<nav class="navbar navbar-expand-lg navbar-light bg-light"> + <div class="container-fluid"> + <a class="navbar-brand" href="{% url 'blog_app:home' %}">Blog</a> + {% if request.user.is_authenticated %} + <a href="{% url 'blog_app:create' %}" class="link-success" + ><i class="bi bi-plus"></i + ></a> + {% endif %} + <button + class="navbar-toggler" + type="button" + data-bs-toggle="collapse" + data-bs-target="#navbarNav" + aria-controls="navbarNav" + aria-expanded="false" + aria-label="Toggle navigation" + > + <span class="navbar-toggler-icon"></span> + </button> + <div class="collapse navbar-collapse justify-content-end" id="navbarNav"> + <ul class="navbar-nav w-100 d-flex justify-content-end"> + <div class="d-flex gap-2"> + {% if request.user.is_authenticated %} + <li class="nav-item my-auto"> + <a href="{% url 'users_app:logout' %}" class="nav-link">Logout</a> + </li> + <li class="nav-item"> + <a + href="{% url 'users_app:profile' user.username %}" + class="nav-link" + > + <i class="bi bi-person-fill"></i> + </a> + </li> + </div> + + {% else %} + <div class="d-flex gap-2"> + <li class="nav-item"> + <a + class="nav-link" + href="{% url 'users_app:login' %}" + >Login</a + > + </li> + <li class="nav-item"> + <a + class="nav-link" + href="{% url 'users_app:register' %}" + >Sign up</a + > + </li> + </div> + </ul> + {% endif %} + </div> + </div> +</nav> diff --git a/Code/James/Django-lab02/templates/users/login.html b/Code/James/Django-lab02/templates/users/login.html index 7c9488fc..0cd0e4a7 100644 --- a/Code/James/Django-lab02/templates/users/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -20,4 +20,9 @@ <h1 class="h1 text-center">Log in</h1> </form> + + <!-- <form action="{% url 'users_app:logout' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> --> + <!-- {% csrf_token %} --> + <!-- <button type="submit" class="btn btn-success">Log out</button> + </form> --> {% endblock content %} \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/urls.py b/Code/James/Django-lab02/users_app/urls.py index 2a293d6a..d719fa46 100644 --- a/Code/James/Django-lab02/users_app/urls.py +++ b/Code/James/Django-lab02/users_app/urls.py @@ -10,5 +10,6 @@ urlpatterns = [ path('', views.login, name='login'), path('register/', views.register, name='register'), + path('logout/', views.logout, name='logout'), path('<str:username>/', views.profile, name='profile') ] \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index faa09b17..c73ebc58 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -67,3 +67,9 @@ def login(request): def profile(request, username): user = get_object_or_404(get_user_model(), username=username) return render(request, 'users/profile.html', {'user':user}) + + +def logout(request): + django_logout(request) + + return redirect(reverse('users_app:login')) From 36a9b511aebb615a6911cd6ddfa0fd78041dba5b Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Mon, 10 Jan 2022 16:52:43 -0800 Subject: [PATCH 11/18] added requirements.txt --- Code/James/Django-lab02/requirements.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Code/James/Django-lab02/requirements.txt diff --git a/Code/James/Django-lab02/requirements.txt b/Code/James/Django-lab02/requirements.txt new file mode 100644 index 00000000..5d9b2ec8 --- /dev/null +++ b/Code/James/Django-lab02/requirements.txt @@ -0,0 +1,12 @@ +asgiref==3.4.1 +black==21.12b0 +click==8.0.3 +colorama==0.4.4 +Django==4.0.1 +mypy-extensions==0.4.3 +pathspec==0.9.0 +platformdirs==2.4.1 +sqlparse==0.4.2 +tomli==1.2.3 +typing_extensions==4.0.1 +tzdata==2021.5 From 3f1d4e73c7f1c9f91a47a00f8232a111ad1ce74a Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Wed, 12 Jan 2022 10:24:54 -0800 Subject: [PATCH 12/18] changing styling and views --- Code/James/Django-lab02/blog_proj/settings.py | 1 - Code/James/Django-lab02/static/css/index.css | 43 +++++++++++++++ Code/James/Django-lab02/templates/base.html | 2 +- .../templates/partials/_navbar.html | 2 +- .../Django-lab02/templates/users/login.html | 52 +++++++++---------- .../Django-lab02/templates/users/profile.html | 3 +- .../templates/users/register.html | 49 ++++++++--------- Code/James/Django-lab02/users_app/forms.py | 10 ++-- 8 files changed, 100 insertions(+), 62 deletions(-) diff --git a/Code/James/Django-lab02/blog_proj/settings.py b/Code/James/Django-lab02/blog_proj/settings.py index 5a5ae5b9..362ae80c 100644 --- a/Code/James/Django-lab02/blog_proj/settings.py +++ b/Code/James/Django-lab02/blog_proj/settings.py @@ -121,7 +121,6 @@ STATIC_URL = 'static/' STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))] -MEDIA_ROOT ='static/' # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css index e69de29b..2420065d 100644 --- a/Code/James/Django-lab02/static/css/index.css +++ b/Code/James/Django-lab02/static/css/index.css @@ -0,0 +1,43 @@ +@import url('https://fonts.googleapis.com/css2?family=Montserrat&family=Playfair+Display&display=swap'); + +* { + font-family: 'Montserrat', sans-serif; +} + +body{ + font-size: 1rem; + font-weight: 400; + line-height: 1.5; + color: #212529; + text-align: left; +} + + + +input{ + border-radius: 25px; + +} + +#form_field{ + display: flex; + justify-content: space-between; + background-color: rgba(0, 0, 255, 0); +} + +/* #log_in { + +} */ + +.col-md-6 { + flex: 0 0 50%; + max-width: 50%; + position: relative; + width: 100%; + padding-right: 15px; + padding-left: 15px; +} + +.intro-section{ + color: black +} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html index 1b2106ef..d001ae79 100644 --- a/Code/James/Django-lab02/templates/base.html +++ b/Code/James/Django-lab02/templates/base.html @@ -6,7 +6,6 @@ <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <title>Blog</title> <link rel="stylesheet" href="{% static 'css/normalize.css' %}" /> <link rel="stylesheet" href="{% static 'css/index.css' %}" /> @@ -27,6 +26,7 @@ integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous" ></script> + <title>Blog</title> </head> <body> {% include 'partials/_navbar.html' %} diff --git a/Code/James/Django-lab02/templates/partials/_navbar.html b/Code/James/Django-lab02/templates/partials/_navbar.html index be159716..d3db9d82 100644 --- a/Code/James/Django-lab02/templates/partials/_navbar.html +++ b/Code/James/Django-lab02/templates/partials/_navbar.html @@ -41,7 +41,7 @@ <a class="nav-link" href="{% url 'users_app:login' %}" - >Login</a + >Sign in</a > </li> <li class="nav-item"> diff --git a/Code/James/Django-lab02/templates/users/login.html b/Code/James/Django-lab02/templates/users/login.html index 0cd0e4a7..39f38cdd 100644 --- a/Code/James/Django-lab02/templates/users/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -1,28 +1,28 @@ -{% extends 'base.html' %} +{% extends 'base.html' %} {% load static %} {% block content %} +<main class="d-flex min-vh-100 align-items-center p-4 p-md-0"> +<div class="container"> +<div class="row"> +<div class="col-md-6 intro-section"> +<form + action="{% url 'users_app:login' %}" + method="POST" + +> + {% csrf_token %} + <fieldset> + <label for="id_username">{{form.username.label}}</label> + {{form.username}} + </fieldset> -{% block content %} - <h1 class="h1 text-center">Log in</h1> + <fieldset> + <label for="id_password">{{form.password.label}}</label> + {{form.password}} + </fieldset> - <form action="{% url 'users_app:login' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> - {% csrf_token %} - <fieldset> - <label for="id_username">{{form.username.label}}</label> - {{form.username}} - </fieldset> - - <fieldset> - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> - - - <button type="submit" class="btn btn-success">Log in</button> - - - </form> - - <!-- <form action="{% url 'users_app:logout' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> --> - <!-- {% csrf_token %} --> - <!-- <button type="submit" class="btn btn-success">Log out</button> - </form> --> -{% endblock content %} \ No newline at end of file + <button type="submit" class="btn btn-success">Sign in</button> +</form> +</div> +</div> +</div> +</main> +{% endblock content %} diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html index 114f1f1c..d11e3261 100644 --- a/Code/James/Django-lab02/templates/users/profile.html +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -10,8 +10,7 @@ <h1 class="py-2">{{user.username}}</h1> <div class="col-8 offset-2 col-lg-4 offset-lg-4 pt-5"> <table class="table"> <thead> - <tr> - </tr> + <tr></tr> </thead> <tbody> <tr> diff --git a/Code/James/Django-lab02/templates/users/register.html b/Code/James/Django-lab02/templates/users/register.html index b3c68640..99aade53 100644 --- a/Code/James/Django-lab02/templates/users/register.html +++ b/Code/James/Django-lab02/templates/users/register.html @@ -1,26 +1,23 @@ -{% extends 'base.html' %} - -{% block content %} - <h1 class="h1 text-center">Register</h1> - - <form action="{% url 'users_app:register' %}" method="POST" class="d-flex flex-column align-items-center gap-3"> - <fieldset> - <label for="id_username">{{form.username.label}}</label> - {{form.username}} - </fieldset> - - <fieldset> - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> - {% csrf_token %} - - <button type="submit" class="btn btn-success">Register</button> - - - </form> - - - {% endblock content %} - - +{% extends 'base.html' %} {% load static %} {% block content %} +<h1 class="h1 text-center">Register</h1> + +<form + action="{% url 'users_app:register' %}" + method="POST" + class="d-flex flex-column align-items-center gap-3" +> + <fieldset> + <label for="id_username">{{form.username.label}}</label> + {{form.username}} + </fieldset> + + <fieldset> + <label for="id_password">{{form.password.label}}</label> + {{form.password}} + </fieldset> + {% csrf_token %} + + <button type="submit" class="btn btn-success">Register</button> +</form> + +{% endblock content %} diff --git a/Code/James/Django-lab02/users_app/forms.py b/Code/James/Django-lab02/users_app/forms.py index fcbe278d..5c3f3fce 100644 --- a/Code/James/Django-lab02/users_app/forms.py +++ b/Code/James/Django-lab02/users_app/forms.py @@ -10,15 +10,15 @@ class Meta: fields = [ 'first_name', - 'last_name' + 'last_name', ] widgets = { - 'first_name': forms.TextInput(attrs={'class':'form-control'}), - 'last_name': forms.TextInput(attrs={'class':'form-control'}), - 'username': forms.TextInput(attrs={'class':'form-control'}), - 'password': forms.PasswordInput(attrs={'class':'form-control'}) + 'first_name': forms.TextInput(attrs={'id':'form_field'}), + 'last_name': forms.TextInput(attrs={'id':'form_field'}), + 'username': forms.TextInput(attrs={'id':'form_field'}), + 'password': forms.PasswordInput(attrs={'id':'form_field'}) } From 7fc020a156dc77acf8a6fc1ab0e82935c3e810dc Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Wed, 12 Jan 2022 20:53:52 -0800 Subject: [PATCH 13/18] fixed templates --- Code/James/Django-lab02/.vscode/settings.json | 3 + Code/James/Django-lab02/blog_app/forms.py | 4 +- Code/James/Django-lab02/blog_app/views.py | 2 +- Code/James/Django-lab02/static/css/index.css | 53 ++++++-------- Code/James/Django-lab02/templates/base.html | 6 +- .../Django-lab02/templates/blogs/create.html | 6 +- .../Django-lab02/templates/blogs/home.html | 34 ++++----- .../templates/partials/_navbar.html | 73 ++++--------------- .../Django-lab02/templates/users/login.html | 60 ++++++++------- .../Django-lab02/templates/users/profile.html | 37 +++++----- .../templates/users/register.html | 44 ++++++----- Code/James/Django-lab02/users_app/forms.py | 8 +- Code/James/Django-lab02/users_app/views.py | 36 ++++----- 13 files changed, 161 insertions(+), 205 deletions(-) create mode 100644 Code/James/Django-lab02/.vscode/settings.json diff --git a/Code/James/Django-lab02/.vscode/settings.json b/Code/James/Django-lab02/.vscode/settings.json new file mode 100644 index 00000000..de288e1e --- /dev/null +++ b/Code/James/Django-lab02/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/forms.py b/Code/James/Django-lab02/blog_app/forms.py index 1ee230d6..01d7939c 100644 --- a/Code/James/Django-lab02/blog_app/forms.py +++ b/Code/James/Django-lab02/blog_app/forms.py @@ -9,6 +9,6 @@ class Meta: model = BlogPost fields = ['title','body'] widgets = { - 'title': forms.TextInput(attrs={'class': 'form-control'}), - 'body': forms.Textarea(attrs={'class': 'form-control', 'style': 'resize: none'}) + 'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter Title'}), + 'body': forms.Textarea(attrs={'class': 'form-control', 'style': 'resize: none', 'placeholder': 'Enter blog here'}) } \ No newline at end of file diff --git a/Code/James/Django-lab02/blog_app/views.py b/Code/James/Django-lab02/blog_app/views.py index 4d985727..7a5b3fd3 100644 --- a/Code/James/Django-lab02/blog_app/views.py +++ b/Code/James/Django-lab02/blog_app/views.py @@ -36,4 +36,4 @@ def create(request): new_blog.user = request.user new_blog.save() - return redirect(reverse('blog_app:profile')) + return redirect(reverse('blog_app:home')) diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css index 2420065d..6dc424f2 100644 --- a/Code/James/Django-lab02/static/css/index.css +++ b/Code/James/Django-lab02/static/css/index.css @@ -1,43 +1,32 @@ -@import url('https://fonts.googleapis.com/css2?family=Montserrat&family=Playfair+Display&display=swap'); +@import url("https://fonts.googleapis.com/css2?family=Montserrat&family=Playfair+Display&display=swap"); * { - font-family: 'Montserrat', sans-serif; + font-family: "Montserrat", sans-serif; } -body{ - font-size: 1rem; - font-weight: 400; - line-height: 1.5; - color: #212529; - text-align: left; +.btn { + display: inline-block; + text-align: center; + transition: ease-in-out; } - - -input{ - border-radius: 25px; - +h1 { + line-height: 1.2; + margin-top: 0; } -#form_field{ - display: flex; - justify-content: space-between; - background-color: rgba(0, 0, 255, 0); -} - -/* #log_in { - -} */ - -.col-md-6 { - flex: 0 0 50%; - max-width: 50%; - position: relative; - width: 100%; - padding-right: 15px; - padding-left: 15px; +h2 { + margin-bottom: 0.5rem; + line-height: 1.2; + margin-top: 0; + color: black; } -.intro-section{ - color: black +#input_field { + display: flex; + justify-content: start; + border-radius: 25px; + border-style: solid; + color: black; + text-indent: 0.8rem; } \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html index d001ae79..477ee318 100644 --- a/Code/James/Django-lab02/templates/base.html +++ b/Code/James/Django-lab02/templates/base.html @@ -30,11 +30,11 @@ </head> <body> {% include 'partials/_navbar.html' %} - <main class="container"> + <main class="container min-vh-100"> {% if errors %} - <div class="row w-100"> + <div class="row d-flex justify-content-center"> {% for error in errors %} - <div class="col-12 col-lg-6 offset-lg-3">{{error}}</div> + <div class="col-12 col-lg-6 text-center p-3">{{error}}</div> {% endfor %} </div> {% endif %} {% block content %} {% endblock content %} diff --git a/Code/James/Django-lab02/templates/blogs/create.html b/Code/James/Django-lab02/templates/blogs/create.html index 8e84b76e..42997375 100644 --- a/Code/James/Django-lab02/templates/blogs/create.html +++ b/Code/James/Django-lab02/templates/blogs/create.html @@ -1,11 +1,13 @@ {% extends 'base.html' %} {% load static %}{% block content %} - <h1 class="text-center py3">New Blog</h1> + <h1 class="text-center pt-5">New Blog</h1> <div class="row"> <div class="col-10 offset-1 col-md-6 offset-md-3"> <form action="{% url 'blog_app:create' %}" method="POST" class="d-flex flex-column gap-3 p-3"> {% csrf_token %} {{form}} - <button type="submit" class="btn btn-success form-control mt-3">Submit</button> + <div class="d-flex justify-content-center"> + <button type="submit" class="btn shadow btn-sm btn-outline-primary mt-3 w-25">Submit</button> + </div> </form> </div> </div> diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html index 1022cbfd..1f9ca931 100644 --- a/Code/James/Django-lab02/templates/blogs/home.html +++ b/Code/James/Django-lab02/templates/blogs/home.html @@ -1,23 +1,17 @@ {% extends 'base.html' %} {% load static %} {% block content %} -<div class="d-flex flex-column align-items-center"> - <h1>Blog Posts</h1> - <ul class="list-group"> +<div class="row d-flex justify-content-center"> + <h1 class="text-center pt-4 pb-4">Blog Posts</h1> +<div class="d-flex col-12 col-md-5 col-lg-6 justify-content-center"> + <ul class="list-group shadow"> {% for blog in blogs %} - <li class="list-group-item"><div class="accordion" id="accordionExample"> - <div class="accordion-item"> - <h2 class="accordion-header" id="headingOne"> - <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> - {{blog.user}}: {{blog.title}} | {{blog.date_created}} - </button> - </h2> - <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> - <div class="accordion-body"> - <strong>{{blog.body}}</strong> - </div> - </div> - </div></li> - {% endfor %} - </ul> -</div> - + <li class="list-group-item p-3"> + {{blog.user}}: {{blog.title}} | {{blog.date_created}} + <br> + <br><strong>{{blog.body}}</strong> + </li> + + {% endfor %} + </ul> + </div> + </div> {% endblock content %} diff --git a/Code/James/Django-lab02/templates/partials/_navbar.html b/Code/James/Django-lab02/templates/partials/_navbar.html index d3db9d82..32822fcb 100644 --- a/Code/James/Django-lab02/templates/partials/_navbar.html +++ b/Code/James/Django-lab02/templates/partials/_navbar.html @@ -1,59 +1,16 @@ {% load static %} -<nav class="navbar navbar-expand-lg navbar-light bg-light"> - <div class="container-fluid"> - <a class="navbar-brand" href="{% url 'blog_app:home' %}">Blog</a> - {% if request.user.is_authenticated %} - <a href="{% url 'blog_app:create' %}" class="link-success" - ><i class="bi bi-plus"></i - ></a> - {% endif %} - <button - class="navbar-toggler" - type="button" - data-bs-toggle="collapse" - data-bs-target="#navbarNav" - aria-controls="navbarNav" - aria-expanded="false" - aria-label="Toggle navigation" - > - <span class="navbar-toggler-icon"></span> - </button> - <div class="collapse navbar-collapse justify-content-end" id="navbarNav"> - <ul class="navbar-nav w-100 d-flex justify-content-end"> - <div class="d-flex gap-2"> - {% if request.user.is_authenticated %} - <li class="nav-item my-auto"> - <a href="{% url 'users_app:logout' %}" class="nav-link">Logout</a> - </li> - <li class="nav-item"> - <a - href="{% url 'users_app:profile' user.username %}" - class="nav-link" - > - <i class="bi bi-person-fill"></i> - </a> - </li> - </div> - - {% else %} - <div class="d-flex gap-2"> - <li class="nav-item"> - <a - class="nav-link" - href="{% url 'users_app:login' %}" - >Sign in</a - > - </li> - <li class="nav-item"> - <a - class="nav-link" - href="{% url 'users_app:register' %}" - >Sign up</a - > - </li> - </div> - </ul> - {% endif %} - </div> - </div> -</nav> +<ul class="nav nav-tabs"> + <li class="nav-item"> + <a class="nav-link active" aria-current="page" href="{% url 'blog_app:home' %}">Home</a> + </li> + {% if request.user.is_authenticated %} + <li class="nav-item"> + <a class="nav-link p-2" href="{% url 'blog_app:create' %}">Create</a> + </li> + <li class="nav-item"> + <a class="nav-link p-2" href="{% url 'users_app:logout' %}">Sign out</a> + </li> + <li class="nav-item"> + <a class="nav-link p-2" href="{% url 'users_app:profile' user.username %}">Profile</a> + </li> + {% endif %} diff --git a/Code/James/Django-lab02/templates/users/login.html b/Code/James/Django-lab02/templates/users/login.html index 39f38cdd..54310716 100644 --- a/Code/James/Django-lab02/templates/users/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -1,28 +1,36 @@ {% extends 'base.html' %} {% load static %} {% block content %} -<main class="d-flex min-vh-100 align-items-center p-4 p-md-0"> -<div class="container"> -<div class="row"> -<div class="col-md-6 intro-section"> -<form - action="{% url 'users_app:login' %}" - method="POST" - -> - {% csrf_token %} - <fieldset> - <label for="id_username">{{form.username.label}}</label> - {{form.username}} - </fieldset> - - <fieldset> - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> - - <button type="submit" class="btn btn-success">Sign in</button> -</form> -</div> -</div> -</div> -</main> + <div class="row d-flex justify-content-center my-auto min-vh-100"> + <div class="col-12 col-md-column col-lg-6 my-auto p-5"> + <h2 class="d-flex justify-content-start"><i class="bi bi-paragraph"></i>Welcome to BlogPost</h1> + <p class="d-flex mt-4"> + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do + eiusmod tempor incididunt ut labore et dolore magna aliqua + </p> + <a href="{% url 'users_app:register' %}" class="btn btn-sm shadow btn-outline-primary" + >Register</a + > + </div> + <div class="d-flex col-12 col-md-column col-lg-6 my-auto p-5 "> + <div class=""> + <h2 class="login-title">Sign in</h2> + <form action="{% url 'users_app:login' %}" method="POST"> + {% csrf_token %} + <div class=""> + <fieldset class="mb-1 mt-2"> + <label for="id_username" + >{{form.username.label}}</label> + {{form.username}} + </fieldset> + <fieldset > + <label for="id_password">{{form.password.label}}</label> + {{form.password}} + </fieldset> + </div> + <div class=""> + <button type="submit" class="btn shadow btn-sm btn-outline-primary mt-3">Sign in</button> + </div> + </form> + </div> + </div> + </div> {% endblock content %} diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html index d11e3261..b6a423e0 100644 --- a/Code/James/Django-lab02/templates/users/profile.html +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -1,39 +1,38 @@ {% extends 'base.html' %} {% load static %} {% block content %} - -<div class="row"> - <div - class="col-12 col-lg-8 offset-lg-2 d-flex gap-3 flex-column align-items-center pt-5" - > - <h1 class="py-2">{{user.username}}</h1> - </div> - - <div class="col-8 offset-2 col-lg-4 offset-lg-4 pt-5"> - <table class="table"> - <thead> - <tr></tr> - </thead> +<div class="row d-flex justify-content-center"> + <h1 class="py-5 text-center">Hello! {{user.username}}</h1> + <div class="col-12 col-md-6 col-lg-4 p-0 m-0 shadow"> + <table class="table table-light m-0 p-0"> <tbody> <tr> - <th scope="row">First</th> - <td> + <th scope="row" class="p-4 table-dark">First</th> + <td class="p-4"> {% if user.first_name %} {{user.first_name}} {% else %} Not Provided {% endif %} </td> </tr> <tr> - <th scope="row">Last</th> - <td> + <th scope="row" class="p-4 table-dark">Last</th> + <td class="p-4"> {% if user.last_name %} {{user.last_name}} {% else %} Not Provided {% endif %} </td> </tr> <tr> - <th scope="row">Joined</th> - <td colspan="2">{{user.date_joined|date}}</td> + <th scope="row" class="p-4 table-dark">Joined</th> + <td colspan="2" class="p-4">{{user.date_joined|date}}</td> </tr> </tbody> </table> </div> + <div class="d-flex justify-content-center justify-space-between p-5"> + <span + ><i class="bi bi-twitter px-2" style="font-size: 2rem;"></i + ><i class="bi bi-discord px-2" style="font-size: 2rem;"></i + ><i class="bi bi-linkedin px-2" style="font-size: 2rem;"></i + ><i class="bi bi-mailbox px-2" style="font-size: 2rem;"></i + ></span> + </div> </div> {% endblock content %} diff --git a/Code/James/Django-lab02/templates/users/register.html b/Code/James/Django-lab02/templates/users/register.html index 99aade53..525e677e 100644 --- a/Code/James/Django-lab02/templates/users/register.html +++ b/Code/James/Django-lab02/templates/users/register.html @@ -1,23 +1,31 @@ {% extends 'base.html' %} {% load static %} {% block content %} -<h1 class="h1 text-center">Register</h1> +<h1 class="h1 text-center p-5">Register</h1> +<div class="row d-flex justify-content-center"> + <div class="card col-12 col-md-6 col-lg-4 shadow"> + <div class="card-body d-flex justify-content-center"> + <form action="{% url 'users_app:register' %}" method="POST"> + <div class="d-flex flex-column justify-content-center"> + <fieldset> + <label for="id_username">{{form.username.label}}</label> + {{form.username}} + </fieldset> -<form - action="{% url 'users_app:register' %}" - method="POST" - class="d-flex flex-column align-items-center gap-3" -> - <fieldset> - <label for="id_username">{{form.username.label}}</label> - {{form.username}} - </fieldset> + <fieldset> + <label for="id_password">{{form.password.label}}</label> + {{form.password}} + </fieldset> + {% csrf_token %} - <fieldset> - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> - {% csrf_token %} - - <button type="submit" class="btn btn-success">Register</button> -</form> + <button + type="submit" + class="btn shadow btn-outline-primary d-flex mx-auto mt-4" + > + Register + </button> + </div> + </form> + </div> + </div> +</div> {% endblock content %} diff --git a/Code/James/Django-lab02/users_app/forms.py b/Code/James/Django-lab02/users_app/forms.py index 5c3f3fce..54d4b61a 100644 --- a/Code/James/Django-lab02/users_app/forms.py +++ b/Code/James/Django-lab02/users_app/forms.py @@ -15,10 +15,10 @@ class Meta: widgets = { - 'first_name': forms.TextInput(attrs={'id':'form_field'}), - 'last_name': forms.TextInput(attrs={'id':'form_field'}), - 'username': forms.TextInput(attrs={'id':'form_field'}), - 'password': forms.PasswordInput(attrs={'id':'form_field'}) + 'first_name': forms.TextInput(attrs={'id':'input_field'}), + 'last_name': forms.TextInput(attrs={'id':'input_field'}), + 'username': forms.TextInput(attrs={'id':'input_field','class': 'input_class', 'placeholder':'Enter username'}), + 'password': forms.PasswordInput(attrs={'id':'input_field','class': 'input_class', 'placeholder':'Enter password'}) } diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index c73ebc58..772fb39a 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -14,15 +14,11 @@ def register(request): form = UserAuthForm() if request.method == "GET": - context = { - "form": form - } + context = {"form": form} return render(request, "users/register.html", context) elif request.method == "POST": form = UserAuthForm(request.POST) - # print(form.is_valid()) - # print(form.errors) if form.is_valid(): new_user = form.save(commit=False) new_user.set_password(form.cleaned_data["password"]) @@ -38,38 +34,38 @@ def register(request): def login(request): - if request.method == 'GET': + if request.method == "GET": form = UserAuthForm() - return render(request, 'users/login.html', {'form': form}) + return render(request, "users/login.html", {"form": form}) - elif request.method == 'POST': + elif request.method == "POST": form = request.POST - username = form['username'] - password = form['password'] + username = form["username"] + password = form["password"] user = authenticate(request, username=username, password=password) if user is None: context = { - 'form': UserAuthForm(), - 'errors': ['Invalid Username or Password'] + "form": UserAuthForm(), + "errors": ["Invalid Username or Password"], } - return render(request, 'users/login.html', context) - + return render(request, "users/login.html", context) + else: django_login(request, user) - return redirect(reverse('users_app:profile', kwargs={'username': user.username})) - - - + return redirect( + reverse("users_app:profile", kwargs={"username": user.username}) + ) +@login_required def profile(request, username): user = get_object_or_404(get_user_model(), username=username) - return render(request, 'users/profile.html', {'user':user}) + return render(request, "users/profile.html", {"user": user}) def logout(request): django_logout(request) - return redirect(reverse('users_app:login')) + return redirect(reverse("users_app:login")) From 0f86e92305285315ec4eb7d5c2f144004ebe63be Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Wed, 12 Jan 2022 20:54:07 -0800 Subject: [PATCH 14/18] styling addition to profile page --- Code/James/Django-lab02/templates/users/profile.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html index b6a423e0..923e2b1a 100644 --- a/Code/James/Django-lab02/templates/users/profile.html +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -25,7 +25,7 @@ <h1 class="py-5 text-center">Hello! {{user.username}}</h1> </tbody> </table> </div> - <div class="d-flex justify-content-center justify-space-between p-5"> + <div class="d-flex justify-content-center justify-space-between p-5 position-fixed bottom-0"> <span ><i class="bi bi-twitter px-2" style="font-size: 2rem;"></i ><i class="bi bi-discord px-2" style="font-size: 2rem;"></i From ea44b6beb3eea1ea08c2af48330985bdc0e9c5b1 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Thu, 13 Jan 2022 18:47:59 -0800 Subject: [PATCH 15/18] getting rid of django form classes --- .../Django-lab02/templates/users/login.html | 13 +++------ .../Django-lab02/templates/users/profile.html | 2 +- Code/James/Django-lab02/users_app/forms.py | 27 ------------------- Code/James/Django-lab02/users_app/views.py | 16 +++++------ 4 files changed, 10 insertions(+), 48 deletions(-) delete mode 100644 Code/James/Django-lab02/users_app/forms.py diff --git a/Code/James/Django-lab02/templates/users/login.html b/Code/James/Django-lab02/templates/users/login.html index 54310716..7304cf70 100644 --- a/Code/James/Django-lab02/templates/users/login.html +++ b/Code/James/Django-lab02/templates/users/login.html @@ -15,16 +15,9 @@ <h2 class="d-flex justify-content-start"><i class="bi bi-paragraph"></i>Welcome <h2 class="login-title">Sign in</h2> <form action="{% url 'users_app:login' %}" method="POST"> {% csrf_token %} - <div class=""> - <fieldset class="mb-1 mt-2"> - <label for="id_username" - >{{form.username.label}}</label> - {{form.username}} - </fieldset> - <fieldset > - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> + <div class="d-flex flex-column"> + <input class="rounded mt-1" type="text" name="username" placeholder="Enter username"> + <input class="rounded mt-3 " type="text" name="password" placeholder="Enter password"> </div> <div class=""> <button type="submit" class="btn shadow btn-sm btn-outline-primary mt-3">Sign in</button> diff --git a/Code/James/Django-lab02/templates/users/profile.html b/Code/James/Django-lab02/templates/users/profile.html index 923e2b1a..78b229e8 100644 --- a/Code/James/Django-lab02/templates/users/profile.html +++ b/Code/James/Django-lab02/templates/users/profile.html @@ -1,5 +1,5 @@ {% extends 'base.html' %} {% load static %} {% block content %} -<div class="row d-flex justify-content-center"> +<div class="row d-flex justify-content-center align-items-center"> <h1 class="py-5 text-center">Hello! {{user.username}}</h1> <div class="col-12 col-md-6 col-lg-4 p-0 m-0 shadow"> <table class="table table-light m-0 p-0"> diff --git a/Code/James/Django-lab02/users_app/forms.py b/Code/James/Django-lab02/users_app/forms.py deleted file mode 100644 index 54d4b61a..00000000 --- a/Code/James/Django-lab02/users_app/forms.py +++ /dev/null @@ -1,27 +0,0 @@ -from django import forms -from .models import User - - -class UserForm(forms.ModelForm): - - class Meta: - - model = User - - fields = [ - 'first_name', - 'last_name', - ] - - - widgets = { - 'first_name': forms.TextInput(attrs={'id':'input_field'}), - 'last_name': forms.TextInput(attrs={'id':'input_field'}), - 'username': forms.TextInput(attrs={'id':'input_field','class': 'input_class', 'placeholder':'Enter username'}), - 'password': forms.PasswordInput(attrs={'id':'input_field','class': 'input_class', 'placeholder':'Enter password'}) - } - - -class UserAuthForm(UserForm): - class Meta(UserForm.Meta): - fields = ['username', 'password'] \ No newline at end of file diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index 772fb39a..c3b7fc18 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -14,15 +14,13 @@ def register(request): form = UserAuthForm() if request.method == "GET": - context = {"form": form} - return render(request, "users/register.html", context) + return render(request, "users/register.html") elif request.method == "POST": - form = UserAuthForm(request.POST) - if form.is_valid(): - new_user = form.save(commit=False) - new_user.set_password(form.cleaned_data["password"]) - new_user.save() + print(request.POST) + username = request.POST['username'] + password = request.POST['password'] + user = .save() return redirect(reverse("users_app:register")) else: @@ -35,9 +33,8 @@ def register(request): def login(request): if request.method == "GET": - form = UserAuthForm() - return render(request, "users/login.html", {"form": form}) + return render(request, "users/login.html") elif request.method == "POST": form = request.POST @@ -47,7 +44,6 @@ def login(request): if user is None: context = { - "form": UserAuthForm(), "errors": ["Invalid Username or Password"], } From 3d4720c8adebd8a37b844843d37b36e5d6c5481e Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Fri, 14 Jan 2022 15:14:32 -0800 Subject: [PATCH 16/18] help with rendering unwanted info on home view --- Code/James/Django-lab02/blog_app/models.py | 2 +- Code/James/Django-lab02/blog_app/views.py | 9 +++-- Code/James/Django-lab02/static/css/index.css | 11 +++++- .../Django-lab02/templates/blogs/home.html | 3 +- .../templates/partials/_navbar.html | 6 ++-- .../templates/users/register.html | 26 ++++++++------ Code/James/Django-lab02/users_app/models.py | 4 +-- Code/James/Django-lab02/users_app/views.py | 34 +++++++++---------- 8 files changed, 53 insertions(+), 42 deletions(-) diff --git a/Code/James/Django-lab02/blog_app/models.py b/Code/James/Django-lab02/blog_app/models.py index 0bd32fcc..eaeb8547 100644 --- a/Code/James/Django-lab02/blog_app/models.py +++ b/Code/James/Django-lab02/blog_app/models.py @@ -8,7 +8,7 @@ class BlogPost(models.Model): title = models.CharField(max_length=200) body = models.TextField() - user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='user') + user = models.ForeignKey(User(), on_delete=models.CASCADE, related_name='user') public = models.BooleanField(null=True, blank=True) date_created = models.DateTimeField(auto_now_add=True) date_edited = models.DateTimeField(auto_now_add=True) diff --git a/Code/James/Django-lab02/blog_app/views.py b/Code/James/Django-lab02/blog_app/views.py index 7a5b3fd3..e0fa1e29 100644 --- a/Code/James/Django-lab02/blog_app/views.py +++ b/Code/James/Django-lab02/blog_app/views.py @@ -2,11 +2,11 @@ from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse from django.contrib.auth.decorators import login_required -from django.http import JsonResponse + from users_app.views import login -from .models import BlogPost +from .models import BlogPost, User from .forms import BlogForm # Create your views here. @@ -29,11 +29,10 @@ def create(request): elif request.method == "POST": form = BlogForm(request.POST) - + print(form) if form.is_valid(): - new_blog = form.save(commit=False) new_blog.user = request.user new_blog.save() - return redirect(reverse('blog_app:home')) + return redirect(reverse("blog_app:home")) diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css index 6dc424f2..aef45bdc 100644 --- a/Code/James/Django-lab02/static/css/index.css +++ b/Code/James/Django-lab02/static/css/index.css @@ -29,4 +29,13 @@ h2 { border-style: solid; color: black; text-indent: 0.8rem; -} \ No newline at end of file +} + +/* .register_page{ + background-image: url(/static/images/pics/sand.jpg); + background-repeat: no-repeat; + background-size: cover; + width: 100vw; + height: 100vh; + z-index: -1; +} */ \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html index 1f9ca931..62d6e082 100644 --- a/Code/James/Django-lab02/templates/blogs/home.html +++ b/Code/James/Django-lab02/templates/blogs/home.html @@ -5,11 +5,10 @@ <h1 class="text-center pt-4 pb-4">Blog Posts</h1> <ul class="list-group shadow"> {% for blog in blogs %} <li class="list-group-item p-3"> - {{blog.user}}: {{blog.title}} | {{blog.date_created}} + User: {{blog.username}}: {{blog.title}} | {{blog.date_created}} <br> <br><strong>{{blog.body}}</strong> </li> - {% endfor %} </ul> </div> diff --git a/Code/James/Django-lab02/templates/partials/_navbar.html b/Code/James/Django-lab02/templates/partials/_navbar.html index 32822fcb..d8b32948 100644 --- a/Code/James/Django-lab02/templates/partials/_navbar.html +++ b/Code/James/Django-lab02/templates/partials/_navbar.html @@ -3,12 +3,12 @@ <li class="nav-item"> <a class="nav-link active" aria-current="page" href="{% url 'blog_app:home' %}">Home</a> </li> - {% if request.user.is_authenticated %} <li class="nav-item"> - <a class="nav-link p-2" href="{% url 'blog_app:create' %}">Create</a> + <a class="nav-link p-2" href="{% url 'users_app:logout' %}">Sign out</a> </li> + {% if request.user.is_authenticated %} <li class="nav-item"> - <a class="nav-link p-2" href="{% url 'users_app:logout' %}">Sign out</a> + <a class="nav-link p-2" href="{% url 'blog_app:create' %}">Create</a> </li> <li class="nav-item"> <a class="nav-link p-2" href="{% url 'users_app:profile' user.username %}">Profile</a> diff --git a/Code/James/Django-lab02/templates/users/register.html b/Code/James/Django-lab02/templates/users/register.html index 525e677e..6dc7cae4 100644 --- a/Code/James/Django-lab02/templates/users/register.html +++ b/Code/James/Django-lab02/templates/users/register.html @@ -1,19 +1,23 @@ {% extends 'base.html' %} {% load static %} {% block content %} +<!-- <div class="register_page"> --> <h1 class="h1 text-center p-5">Register</h1> <div class="row d-flex justify-content-center"> - <div class="card col-12 col-md-6 col-lg-4 shadow"> + <div class="card d-flex justify-content-center col-12 col-md-6 col-lg-4 shadow"> <div class="card-body d-flex justify-content-center"> <form action="{% url 'users_app:register' %}" method="POST"> <div class="d-flex flex-column justify-content-center"> - <fieldset> - <label for="id_username">{{form.username.label}}</label> - {{form.username}} - </fieldset> - - <fieldset> - <label for="id_password">{{form.password.label}}</label> - {{form.password}} - </fieldset> + <input + class="rounded mt-1" + type="text" + name="username" + placeholder="Enter username" + /> + <input + class="rounded mt-3" + type="text" + name="password" + placeholder="Enter password" + /> {% csrf_token %} <button @@ -27,5 +31,5 @@ <h1 class="h1 text-center p-5">Register</h1> </div> </div> </div> - +<!-- </div> --> {% endblock content %} diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py index 2072a9bd..2106f7fa 100644 --- a/Code/James/Django-lab02/users_app/models.py +++ b/Code/James/Django-lab02/users_app/models.py @@ -4,8 +4,8 @@ class User(AbstractUser): - + def __str__(self): - return self.username + return f'{self.username} {self.password}' diff --git a/Code/James/Django-lab02/users_app/views.py b/Code/James/Django-lab02/users_app/views.py index c3b7fc18..0c36f51c 100644 --- a/Code/James/Django-lab02/users_app/views.py +++ b/Code/James/Django-lab02/users_app/views.py @@ -1,39 +1,37 @@ +from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render, redirect, reverse from django.contrib.auth import ( - get_user_model, + # get_user_model, authenticate, login as django_login, logout as django_logout, ) from django.contrib.auth.decorators import login_required - -from .forms import UserForm, UserAuthForm +from users_app.models import User # Create your views here. def register(request): - form = UserAuthForm() if request.method == "GET": return render(request, "users/register.html") elif request.method == "POST": print(request.POST) - username = request.POST['username'] - password = request.POST['password'] - user = .save() - return redirect(reverse("users_app:register")) + username = request.POST["username"] + password = request.POST["password"] - else: - context = { - "form": UserAuthForm(), - "errors": [value for value in form.errors.values()], - } - return render(request, "users/register.html", context) + new_user = User.objects.create_user(username=username, password=password) + + django_login(request, new_user) + return redirect(reverse("users_app:register")) + + else: + + return render(request, "users/register.html") def login(request): if request.method == "GET": - return render(request, "users/login.html") elif request.method == "POST": @@ -51,16 +49,18 @@ def login(request): else: django_login(request, user) - return redirect( + return HttpResponseRedirect( reverse("users_app:profile", kwargs={"username": user.username}) ) + @login_required def profile(request, username): - user = get_object_or_404(get_user_model(), username=username) + user = get_object_or_404(User, username=username) return render(request, "users/profile.html", {"user": user}) + def logout(request): django_logout(request) From bbed81128737f13278e30b2a6b110c6699a84728 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Fri, 14 Jan 2022 16:03:23 -0800 Subject: [PATCH 17/18] completed lab02 --- Code/James/Django-lab02/static/css/index.css | 5 ++- Code/James/Django-lab02/templates/base.html | 2 +- .../Django-lab02/templates/blogs/home.html | 32 +++++++++++-------- .../templates/partials/_navbar.html | 15 +++++++-- Code/James/Django-lab02/users_app/models.py | 2 +- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css index aef45bdc..4bababb7 100644 --- a/Code/James/Django-lab02/static/css/index.css +++ b/Code/James/Django-lab02/static/css/index.css @@ -2,6 +2,8 @@ * { font-family: "Montserrat", sans-serif; + padding: 0; + margin: 0; } .btn { @@ -38,4 +40,5 @@ h2 { width: 100vw; height: 100vh; z-index: -1; -} */ \ No newline at end of file +} */ + diff --git a/Code/James/Django-lab02/templates/base.html b/Code/James/Django-lab02/templates/base.html index 477ee318..689377a8 100644 --- a/Code/James/Django-lab02/templates/base.html +++ b/Code/James/Django-lab02/templates/base.html @@ -30,7 +30,7 @@ </head> <body> {% include 'partials/_navbar.html' %} - <main class="container min-vh-100"> + <main> {% if errors %} <div class="row d-flex justify-content-center"> {% for error in errors %} diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html index 62d6e082..09eac233 100644 --- a/Code/James/Django-lab02/templates/blogs/home.html +++ b/Code/James/Django-lab02/templates/blogs/home.html @@ -1,16 +1,22 @@ {% extends 'base.html' %} {% load static %} {% block content %} -<div class="row d-flex justify-content-center"> - <h1 class="text-center pt-4 pb-4">Blog Posts</h1> -<div class="d-flex col-12 col-md-5 col-lg-6 justify-content-center"> - <ul class="list-group shadow"> - {% for blog in blogs %} +<div class="container p-0 m-0"> +<div class="row d-flex justify-content-center vw-100"> + <div class="blog-heading pt-4 pb-4 d-flex text-light bg-dark justify-content-center"> + <h1> + Blog Posts + </h1> + </div> + <div class="d-flex col-12 col-md-5 col-lg-6 justify-content-center"> + <ul class="list-group shadow"> + {% for blog in blogs %} <li class="list-group-item p-3"> - User: {{blog.username}}: {{blog.title}} | {{blog.date_created}} - <br> - <br><strong>{{blog.body}}</strong> - </li> - {% endfor %} - </ul> - </div> - </div> + User: {{blog.user}} <strong>Title: </strong>{{blog.title}} | {{blog.date_created}} + <br /> + <br /><strong>" {{blog.body}} "</strong> + </li> + {% endfor %} + </ul> + </div> +</div> +</div> {% endblock content %} diff --git a/Code/James/Django-lab02/templates/partials/_navbar.html b/Code/James/Django-lab02/templates/partials/_navbar.html index d8b32948..c619c75c 100644 --- a/Code/James/Django-lab02/templates/partials/_navbar.html +++ b/Code/James/Django-lab02/templates/partials/_navbar.html @@ -1,7 +1,13 @@ {% load static %} + <ul class="nav nav-tabs"> <li class="nav-item"> - <a class="nav-link active" aria-current="page" href="{% url 'blog_app:home' %}">Home</a> + <a + class="nav-link active" + aria-current="page" + href="{% url 'blog_app:home' %}" + >Home</a + > </li> <li class="nav-item"> <a class="nav-link p-2" href="{% url 'users_app:logout' %}">Sign out</a> @@ -11,6 +17,9 @@ <a class="nav-link p-2" href="{% url 'blog_app:create' %}">Create</a> </li> <li class="nav-item"> - <a class="nav-link p-2" href="{% url 'users_app:profile' user.username %}">Profile</a> + <a class="nav-link p-2" href="{% url 'users_app:profile' user.username %}" + >Profile</a + > </li> - {% endif %} +</ul> +{% endif %} diff --git a/Code/James/Django-lab02/users_app/models.py b/Code/James/Django-lab02/users_app/models.py index 2106f7fa..eb76cc1c 100644 --- a/Code/James/Django-lab02/users_app/models.py +++ b/Code/James/Django-lab02/users_app/models.py @@ -6,6 +6,6 @@ class User(AbstractUser): def __str__(self): - return f'{self.username} {self.password}' + return f'{self.username}' From aa9fc1608d2fbadc208fd3bd8549645db85bbb51 Mon Sep 17 00:00:00 2001 From: James Johnson <jamesjhnsn1995@gmail.com> Date: Sat, 15 Jan 2022 12:29:10 -0800 Subject: [PATCH 18/18] styling changes --- Code/James/Django-lab02/static/css/index.css | 7 +++++ .../Django-lab02/templates/blogs/home.html | 4 +-- Code/James/python/lab15.py | 26 +++++++++---------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Code/James/Django-lab02/static/css/index.css b/Code/James/Django-lab02/static/css/index.css index 4bababb7..a3acd14c 100644 --- a/Code/James/Django-lab02/static/css/index.css +++ b/Code/James/Django-lab02/static/css/index.css @@ -42,3 +42,10 @@ h2 { z-index: -1; } */ +.hero-image{ + background-image: url(/static/images/pics/create.jpg) no-repeat center; + background-size: cover; + height: 500px; + position: relative; + width: 100%; +} \ No newline at end of file diff --git a/Code/James/Django-lab02/templates/blogs/home.html b/Code/James/Django-lab02/templates/blogs/home.html index 09eac233..f4e66fc7 100644 --- a/Code/James/Django-lab02/templates/blogs/home.html +++ b/Code/James/Django-lab02/templates/blogs/home.html @@ -1,7 +1,7 @@ {% extends 'base.html' %} {% load static %} {% block content %} <div class="container p-0 m-0"> <div class="row d-flex justify-content-center vw-100"> - <div class="blog-heading pt-4 pb-4 d-flex text-light bg-dark justify-content-center"> + <div class="hero-image jumbotron d-flex text-light bg-dark justify-content-center"> <h1> Blog Posts </h1> @@ -10,7 +10,7 @@ <h1> <ul class="list-group shadow"> {% for blog in blogs %} <li class="list-group-item p-3"> - User: {{blog.user}} <strong>Title: </strong>{{blog.title}} | {{blog.date_created}} + User: {{blog.user.username}} <strong>Title: </strong>{{blog.title}} | {{blog.date_created}} <br /> <br /><strong>" {{blog.body}} "</strong> </li> diff --git a/Code/James/python/lab15.py b/Code/James/python/lab15.py index 6b8298c8..9dc0b8ab 100644 --- a/Code/James/python/lab15.py +++ b/Code/James/python/lab15.py @@ -12,7 +12,7 @@ def linear_search(list, value): return match -#linear_search(nums, 3) +linear_search(nums, 3) """ binary search """ @@ -53,19 +53,19 @@ def binary_search(list, target): -# def bubble_sort(array): -# print('First list is inital list') -# print(nums) -# swapped = True +def bubble_sort(array): + print('First list is inital list') + print(nums) + swapped = True -# while(swapped): # while swap is True run the code. -# swapped = False -# for i in range(len(array) - 1): # will run until second to last item in list -# if array[i] > array[i + 1]: -# # Swap the item in the list if it's greater then it's neighbor. -# array[i], array[i + 1] = array[i + 1], array[i] -# swapped = True -# return array + while(swapped): # while swap is True run the code. + swapped = False + for i in range(len(array) - 1): # will run until second to last item in list + if array[i] > array[i + 1]: + # Swap the item in the list if it's greater then it's neighbor. + array[i], array[i + 1] = array[i + 1], array[i] + swapped = True + return array # print(bubble_sort(nums))