diff --git a/.gitignore b/.gitignore index fcdd66c..cab00b6 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,6 @@ venv.bak/ # Exclude collected Django static files static_collected + +# Exclude machine-specific vs code settings +.vscode/settings.json \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 4fe2fd0..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "${workspaceFolder}\\env\\Scripts\\python.exe" -} diff --git a/hello/forms.py b/hello/forms.py index 713aed8..902e9c2 100644 --- a/hello/forms.py +++ b/hello/forms.py @@ -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 diff --git a/hello/models.py b/hello/models.py index baab685..1c4184d 100644 --- a/hello/models.py +++ b/hello/models.py @@ -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')}" diff --git a/hello/templates/hello/about.html b/hello/templates/hello/about.html index 6e923d6..6f5d1a0 100644 --- a/hello/templates/hello/about.html +++ b/hello/templates/hello/about.html @@ -1,7 +1,7 @@ {% extends "hello/layout.html" %} {% block title %} -About + About {% endblock %} {% block content %} -

About page for the Visual Studio Code Django tutorial.

+

About page for the Visual Studio Code Django tutorial.

{% endblock %} diff --git a/hello/templates/hello/contact.html b/hello/templates/hello/contact.html index 4382a54..5cc2e40 100644 --- a/hello/templates/hello/contact.html +++ b/hello/templates/hello/contact.html @@ -1,7 +1,7 @@ {% extends "hello/layout.html" %} {% block title %} -Contact Us + Contact Us {% endblock %} {% block content %} -

Contact page for the Visual Studio Code Django tutorial.

+

Contact page for the Visual Studio Code Django tutorial.

{% endblock %} diff --git a/hello/templates/hello/hello_there.html b/hello/templates/hello/hello_there.html index f9048e1..71f4123 100644 --- a/hello/templates/hello/hello_there.html +++ b/hello/templates/hello/hello_there.html @@ -1,12 +1,12 @@ - - - Hello, Django - {% load static %} - - - - Hello, there {{ name }}! It's {{ date | date:'l, d F, Y' }} at {{ date | time:'H:i:s' }}. - + + + Hello, Django + {% load static %} + + + +Hello, there {{ name }}! It's {{ date | date:'l, d F, Y' }} at {{ date | time:'H:i:s' }}. + diff --git a/hello/templates/hello/home.html b/hello/templates/hello/home.html index 4881ea4..cc34984 100644 --- a/hello/templates/hello/home.html +++ b/hello/templates/hello/home.html @@ -1,32 +1,32 @@ {% extends "hello/layout.html" %} {% block title %} -Home + Home {% endblock %} {% block content %} -

Logged messages

+

Logged messages

-{% if message_list %} - - - - - - - - - - {% for message in message_list %} - - - - - - {% endfor %} - -
DateTimeMessage
{{ message.log_date | date:'d M Y' }}{{ message.log_date | date:'H:i:s' }} - {{ message.message }} -
-{% else %} -

No messages have been logged. Use the Log Message form.

-{% endif %} + {% if message_list %} + + + + + + + + + + {% for message in message_list %} + + + + + + {% endfor %} + +
DateTimeMessage
{{ message.log_date | date:'d M Y' }}{{ message.log_date | date:'H:i:s' }} + {{ message.message }} +
+ {% else %} +

No messages have been logged. Use the Log Message form.

+ {% endif %} {% endblock %} diff --git a/hello/templates/hello/layout.html b/hello/templates/hello/layout.html index 8e6d64f..74cd061 100644 --- a/hello/templates/hello/layout.html +++ b/hello/templates/hello/layout.html @@ -1,27 +1,27 @@ - - - {% block title %}{% endblock %} - {% load static %} - - + + + {% block title %}{% endblock %} + {% load static %} + + - - + + -
- {% block content %} - {% endblock %} -
- -
- +
+ {% block content %} + {% endblock %} +
+ +
+ diff --git a/hello/templates/hello/log_message.html b/hello/templates/hello/log_message.html index 7ffaaed..15f7af9 100644 --- a/hello/templates/hello/log_message.html +++ b/hello/templates/hello/log_message.html @@ -1,9 +1,10 @@ {% extends "hello/layout.html" %} {% block title %} -Log a message + Log a message {% endblock %} {% block content %} -
{% csrf_token %} + + {% csrf_token %} {{ form.as_p }}
diff --git a/hello/urls.py b/hello/urls.py index a2e55f9..bf4024a 100644 --- a/hello/urls.py +++ b/hello/urls.py @@ -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/', 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/", 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"), ] diff --git a/hello/views.py b/hello/views.py index 90414e1..d740d33 100644 --- a/hello/views.py +++ b/hello/views.py @@ -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) @@ -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}) diff --git a/web_project/urls.py b/web_project/urls.py index a5bcba1..71bcdf9 100644 --- a/web_project/urls.py +++ b/web_project/urls.py @@ -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()