Skip to content

Commit

Permalink
Merge pull request #329 from PdxCodeGuild/james-django-lab02
Browse files Browse the repository at this point in the history
Excellent!
  • Loading branch information
perennialAutodidact authored Jan 25, 2022
2 parents 1901a9e + b8bda5d commit e590d41
Show file tree
Hide file tree
Showing 38 changed files with 1,148 additions and 83 deletions.
99 changes: 42 additions & 57 deletions Code/James/Django-lab-01/todo/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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%}
23 changes: 10 additions & 13 deletions Code/James/Django-lab-01/todo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
render,
get_object_or_404,
reverse
)

)

from django.http import HttpResponse, Http404

Expand All @@ -17,35 +17,32 @@

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)
# priority = Priority.objects.get_or_create(name=form['key'])
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'))

3 changes: 3 additions & 0 deletions Code/James/Django-lab02/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
Empty file.
8 changes: 8 additions & 0 deletions Code/James/Django-lab02/blog_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin

from .models import BlogPost

# Register your models here.

admin.site.register(BlogPost)

6 changes: 6 additions & 0 deletions Code/James/Django-lab02/blog_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog_app'
14 changes: 14 additions & 0 deletions Code/James/Django-lab02/blog_app/forms.py
Original file line number Diff line number Diff line change
@@ -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', 'placeholder': 'Enter Title'}),
'body': forms.Textarea(attrs={'class': 'form-control', 'style': 'resize: none', 'placeholder': 'Enter blog here'})
}
29 changes: 29 additions & 0 deletions Code/James/Django-lab02/blog_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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)),
],
),
]
Original file line number Diff line number Diff line change
@@ -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),
),
]
Empty file.
17 changes: 17 additions & 0 deletions Code/James/Django-lab02/blog_app/models.py
Original file line number Diff line number Diff line change
@@ -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(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)

def __str__(self):
return f'{self.title}'
3 changes: 3 additions & 0 deletions Code/James/Django-lab02/blog_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions Code/James/Django-lab02/blog_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from . import views

app_name = 'blog_app'
urlpatterns = [
path('home/', views.home, name='home'),
path('create/', views.create, name='create'),
]
38 changes: 38 additions & 0 deletions Code/James/Django-lab02/blog_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 users_app.views import login

from .models import BlogPost, User
from .forms import BlogForm

# Create your views here.


def home(request):
blogs = BlogPost.objects.all().order_by("-date_created")
print(blogs)
context = {"blogs": blogs}

return render(request, "blogs/home.html", context)


@login_required
def create(request):
if request.method == "GET":
form = BlogForm()

return render(request, "blogs/create.html", {"form": form})

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"))
Empty file.
16 changes: 16 additions & 0 deletions Code/James/Django-lab02/blog_proj/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit e590d41

Please sign in to comment.