-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from PdxCodeGuild/james-django-lab02
Excellent!
- Loading branch information
Showing
38 changed files
with
1,148 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
Code/James/Django-lab02/blog_app/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
], | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
Code/James/Django-lab02/blog_app/migrations/0002_alter_blogpost_public.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.