Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ venv.bak/

# Exclude collected Django static files
static_collected

# Exclude machine-specific vs code settings
.vscode/settings.json
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

6 changes: 4 additions & 2 deletions hello/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django import forms
from .models import LogMessage

from hello.models import LogMessage


class LogMessageForm(forms.ModelForm):
class Meta:
model = LogMessage
fields = ('message',) # NOTE: the trailing comma is required
fields = ("message",) # NOTE: the trailing comma is required
7 changes: 4 additions & 3 deletions hello/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.db import models


class LogMessage(models.Model):
message = models.CharField(max_length=300)
log_date = models.DateTimeField('date logged')
log_date = models.DateTimeField("date logged")

def __unicode__(self):
def __str__(self):
"""Returns a string representation of a message."""
return "'" + self.text + "' logged on " + log_date.strftime('%A, %d %B, %Y at %X')
return f"'{self.message}' logged on {self.log_date.strftime('%A, %d %B, %Y at %X')}"
4 changes: 2 additions & 2 deletions hello/templates/hello/about.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "hello/layout.html" %}
{% block title %}
About
About
{% endblock %}
{% block content %}
<p>About page for the Visual Studio Code Django tutorial.</p>
<p>About page for the Visual Studio Code Django tutorial.</p>
{% endblock %}
4 changes: 2 additions & 2 deletions hello/templates/hello/contact.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "hello/layout.html" %}
{% block title %}
Contact Us
Contact Us
{% endblock %}
{% block content %}
<p>Contact page for the Visual Studio Code Django tutorial.</p>
<p>Contact page for the Visual Studio Code Django tutorial.</p>
{% endblock %}
18 changes: 9 additions & 9 deletions hello/templates/hello/hello_there.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello, Django</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'hello/site.css' %}" />
</head>
<body>
<span class="message">Hello, there {{ name }}!</span> It's {{ date | date:'l, d F, Y' }} at {{ date | time:'H:i:s' }}.
</body>
<head>
<meta charset="utf-8"/>
<title>Hello, Django</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'hello/site.css' %}"/>
</head>
<body>
<span class="message">Hello, there {{ name }}!</span> It's {{ date | date:'l, d F, Y' }} at {{ date | time:'H:i:s' }}.
</body>
</html>
52 changes: 26 additions & 26 deletions hello/templates/hello/home.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{% extends "hello/layout.html" %}
{% block title %}
Home
Home
{% endblock %}
{% block content %}
<h2>Logged messages</h2>
<h2>Logged messages</h2>

{% if message_list %}
<table class="message_list">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Message</th>
</tr>
</thead>
<tbody>
{% for message in message_list %}
<tr>
<td>{{ message.log_date | date:'d M Y' }}</td>
<td>{{ message.log_date | date:'H:i:s' }}</td>
<td>
{{ message.message }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No messages have been logged. Use the <a href="{% url 'log' %}">Log Message form</a>.</p>
{% endif %}
{% if message_list %}
<table class="message_list">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Message</th>
</tr>
</thead>
<tbody>
{% for message in message_list %}
<tr>
<td>{{ message.log_date | date:'d M Y' }}</td>
<td>{{ message.log_date | date:'H:i:s' }}</td>
<td>
{{ message.message }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No messages have been logged. Use the <a href="{% url 'log' %}">Log Message form</a>.</p>
{% endif %}
{% endblock %}
44 changes: 22 additions & 22 deletions hello/templates/hello/layout.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{% block title %}{% endblock %}</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'hello/site.css' %}" />
</head>
<head>
<meta charset="utf-8"/>
<title>{% block title %}{% endblock %}</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'hello/site.css' %}"/>
</head>

<body>
<div class="navbar">
<a href="{% url 'home' %}" class="navbar-brand">Home</a>
<a href="{% url 'log' %}" class="navbar-item">Log Message</a>
<a href="{% url 'about' %}" class="navbar-item">About</a>
<a href="{% url 'contact' %}" class="navbar-item">Contact</a>
</div>
<body>
<div class="navbar">
<a href="{% url 'home' %}" class="navbar-brand">Home</a>
<a href="{% url 'log' %}" class="navbar-item">Log Message</a>
<a href="{% url 'about' %}" class="navbar-item">About</a>
<a href="{% url 'contact' %}" class="navbar-item">Contact</a>
</div>

<div class="body-content">
{% block content %}
{% endblock %}
<hr/>
<footer>
<p>&copy; 2018</p>
</footer>
</div>
</body>
<div class="body-content">
{% block content %}
{% endblock %}
<hr/>
<footer>
<p>&copy; 2018</p>
</footer>
</div>
</body>
</html>
5 changes: 3 additions & 2 deletions hello/templates/hello/log_message.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% extends "hello/layout.html" %}
{% block title %}
Log a message
Log a message
{% endblock %}
{% block content %}
<form method="POST" class="log-form">{% csrf_token %}
<form method="POST" class="log-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Log</button>
</form>
Expand Down
26 changes: 14 additions & 12 deletions hello/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from django.urls import path
from .models import LogMessage
from . import views

from hello import views
from hello.models import LogMessage

home_list_view = views.HomeListView.as_view(
queryset=LogMessage.objects.order_by("-log_date")[:5], # :5 limits the results to the five most recent
context_object_name="message_list",
template_name="hello/home.html",
)

urlpatterns = [
path('',
views.HomeListView.as_view(
queryset=LogMessage.objects.order_by('-log_date')[:5], # :5 limits the results to the five most recent
context_object_name='message_list',
template_name='hello/home.html',),
name="home"),
path('hello/<name>', views.hello_there, name="hello_there"),
path('about/', views.about, name="about"),
path('contact/', views.contact, name="contact"),
path('log/', views.log_message, name="log"),
path("", home_list_view, name="home"),
path("hello/<name>", views.hello_there, name="hello_there"),
path("about/", views.about, name="about"),
path("contact/", views.contact, name="contact"),
path("log/", views.log_message, name="log"),
]
34 changes: 17 additions & 17 deletions hello/views.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
from django.http import HttpResponse
from datetime import datetime
from django.shortcuts import render
from django.shortcuts import redirect

from django.shortcuts import redirect, render
from django.views.generic import ListView
from .forms import LogMessageForm
from .models import LogMessage

print("http://localhost:8000/hello/VSCode")
from hello.forms import LogMessageForm
from hello.models import LogMessage


# def home(request):
# return render(request, 'hello/home.html')


class HomeListView(ListView):
"""Renders the home page, with a list of all polls."""

model = LogMessage

def get_context_data(self, **kwargs):
context = super(HomeListView, self).get_context_data(**kwargs)
return context


def about(request):
return render(request, 'hello/about.html')
return render(request, "hello/about.html")


def contact(request):
return render(request, 'hello/contact.html')

return render(request, "hello/contact.html")


def hello_there(request, name):
return render(
request,
'hello/hello_there.html',
{
'name': name,
'date': datetime.now()
}
request, "hello/hello_there.html", {"name": name, "date": datetime.now()}
)


def log_message(request):
if request.method == "POST":
form = LogMessageForm(request.POST)
Expand All @@ -43,7 +43,7 @@ def log_message(request):
message = form.save(commit=False)
message.log_date = datetime.now()
message.save()
return redirect('home')
return redirect("home")
else:
form = LogMessageForm()
return render(request, 'hello/log_message.html', {'form': form})
return render(request, "hello/log_message.html", {"form": form})
7 changes: 3 additions & 4 deletions web_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
"""

from django.contrib import admin
from django.urls import path
from django.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import include, path

urlpatterns = [
path('', include('hello.urls')),
path('admin/', admin.site.urls), # Activates the admin interface
path("", include("hello.urls")),
path("admin/", admin.site.urls), # Activates the admin interface
]

urlpatterns += staticfiles_urlpatterns()