Commit d523ce09 authored by nheoxoz's avatar nheoxoz

created proj level base.html and implemented FBV

parent b5cc0a1e
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Announcement Board{% endblock %}
{% block heading %}Welcome to Widget's Announcement Board!{% endblock %}
{% block content %}
<p>
Announcements:<br>
{% for a in announcements %}
<a href="{{ user.get_absolute_url }}">
{{a.title}} by {{ a.author }}
</a><br>
{% endfor %}
</p>
{% endblock %}
{% block footing %}
<a href="{% url 'announcements:announcement-create' %}">
<button class="btn add">Add Announcement</button>
</a>
{% endblock %}
from django.urls import path from django.urls import path
from .views import index from .views import (
index,
AnnouncementsDetailView,
AnnouncementsCreateView,
AnnouncementsUpdateView,
)
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', index, name='index'),
path('announcements/<int:pk>/details/',
AnnouncementsDetailView.as_view(),
name='announcement-details'),
path('announcements/add/',
AnnouncementsCreateView.as_view(),
name='announcement-create'),
path('announcements/<int:pk>/edit/',
AnnouncementsUpdateView.as_view(),
name='announcement-update'),
] ]
app_name = "announcements" app_name = "announcements"
from django.http import HttpResponse from django.shortcuts import render
from .models import Announcement, Reaction from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement
def index(request): def index(request):
return_string = "<p>Widget's Announcement Board</p>Announcements:<br>" announcements = Announcement.objects.all()
for a in Announcement.objects.all(): return render(
return_string += '''{} by {} {} published {}{}<br>'''.format( request,
a.title, 'announcements/announcements.html',
a.author.first_name, {'announcements': announcements},
a.author.last_name, )
a.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
a.body,
) class AnnouncementsDetailView(DetailView):
likeTally = 0 model = Announcement
loveTally = 0 template_name = 'announcements/announcement-details.html'
angryTally = 0
for r in Reaction.objects.all():
if r.name == "LIKE" and r.announcement == a: class AnnouncementsCreateView(CreateView):
likeTally += r.tally model = Announcement
if r.name == "LOVE" and r.announcement == a: fields = '__all__'
loveTally += r.tally template_name = 'announcements/announcement-add.html'
if r.name == "ANGRY" and r.announcement == a:
angryTally += r.tally
return_string += '''Like: {}<br>Love: {} class AnnouncementsUpdateView(UpdateView):
<br>Angry: {}<br><br>'''.format( model = Announcement
likeTally, fields = '__all__'
loveTally, template_name = 'announcements/announcement-edit.html'
angryTally,
)
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(html_string)
{% load static %}
<!DOCTYPE html>
<html lang="">
<head>
<link rel="stylesheet" href="{% static 'style.css' %}">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div id="heading">
{% block heading %}{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
<div id="footing">
{% block footing %}{% endblock %}
</div>
</body>
</html>
...@@ -62,7 +62,7 @@ ROOT_URLCONF = 'widget_OhMyBash.urls' ...@@ -62,7 +62,7 @@ ROOT_URLCONF = 'widget_OhMyBash.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
...@@ -124,6 +124,7 @@ USE_TZ = True ...@@ -124,6 +124,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment