diff --git a/gymapp/settings.py b/gymapp/settings.py index c56a586fbd9ae8016dd82f851ddf55a385dc8019..13363a6b8a921a1f3c02ccdbc05293d8bc23ff84 100644 --- a/gymapp/settings.py +++ b/gymapp/settings.py @@ -32,6 +32,7 @@ ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'homeapp.apps.HomeappConfig', + 'sass_processor', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -118,7 +119,19 @@ USE_TZ = True STATIC_URL = 'static/' +STATIC_ROOT = BASE_DIR / 'static' +SASS_PROCESSOR_ROOT = STATIC_ROOT + +STATICFILES_FINDERS = [ + 'django.contrib.staticfiles.finders.FileSystemFinder', + 'django.contrib.staticfiles.finders.AppDirectoriesFinder', + 'sass_processor.finders.CssFinder', +] + # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +DEFAULT_FROM_EMAIL = "so00647@surrey.ac.uk" +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" \ No newline at end of file diff --git a/gymapp/urls.py b/gymapp/urls.py index 9085c2843590f70bacb2a13eb53383049e8d4a06..82694f22aa8d3a3c2a8d4543ed452ecc5315e668 100644 --- a/gymapp/urls.py +++ b/gymapp/urls.py @@ -14,9 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import include, path +from django.urls import path, include urlpatterns = [ - path('', include('homeapp.urls')), path('admin/', admin.site.urls), + path('', include('homeapp.urls')), ] diff --git a/homeapp/templates/homeapp/home.html b/homeapp/templates/homeapp/home.html index aea532f6b58d86e0bf98fcb24242dc867f3613c8..e018923517c56e84fa017927f1680fdad34bddcb 100644 --- a/homeapp/templates/homeapp/home.html +++ b/homeapp/templates/homeapp/home.html @@ -5,7 +5,21 @@ {% endblock title%} {% block content %} + <h1>Welcome!</h1> -<p>Rendering template -/homeapp/home.html</p> -{% endblock content %} \ No newline at end of file +{% endblock content %} + +<!-- + <script type="text/javascript"> + console.log('In the js') + + window.onload = function(){ + if(window.jQuery){ + alert('jQuery is loaded'); + } + else{ + alert('jQuery is not loaded'); + } + } + </script> +--> \ No newline at end of file diff --git a/homeapp/tests.py b/homeapp/tests.py index 7ce503c2dd97ba78597f6ff6e4393132753573f6..88d33a4ef8cb90635137180479784672bf740be2 100644 --- a/homeapp/tests.py +++ b/homeapp/tests.py @@ -1,3 +1,18 @@ from django.test import TestCase +from django.urls import reverse -# Create your tests here. +class HomePageTests(TestCase): + + def setUp(self): + return + + def test_homepage(self): + response = self.client.get('') + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'Welcome!') + + def test_contact(self): + response = self.client.get(reverse('contact')) + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'Contact Us') + \ No newline at end of file diff --git a/homeapp/urls.py b/homeapp/urls.py index 9ec83613b7213a034410fae268cf8c0ec7bbdc46..b9409b3360a46aa52a67e051ce68a6f088702b1a 100644 --- a/homeapp/urls.py +++ b/homeapp/urls.py @@ -3,4 +3,5 @@ from . import views urlpatterns = [ path('', views.home, name='home'), + path('contact', views.contact, name='contact') ] diff --git a/homeapp/views.py b/homeapp/views.py index 0d89966ebbe2a90a34e82adf87fcfe311f1dd412..755b80b39db60f48801a66ae954e7b8d7b6633f9 100644 --- a/homeapp/views.py +++ b/homeapp/views.py @@ -1,5 +1,33 @@ -from django.shortcuts import render +from django.core.mail import send_mail, BadHeaderError +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render, redirect +from django.contrib import messages +from django.urls import reverse +from .forms import ContactForm def home(request): context = {} - return render(request, 'homeapp/home.html', context) \ No newline at end of file + return render(request, 'homeapp/home.html', context) + +def contact(request): + if request.method == "GET": + form = ContactForm() + else: + form = ContactForm(request.POST) + if form.is_valid(): + name = form.cleaned_data['name'] + subject = form.cleaned_data['subject'] + email = form.cleaned_data['email'] + message = name + ':\n' + form.cleaned_data['message'] + try: + send_mail(subject, message, email, ['My']) + except BadHeaderError(): + messages.add_message(request, messages.ERROR, 'Message Not Sent') + return HttpResponse("Invalid header found.") + + messages.add_message(request, messages.SUCCESS, 'Message Sent') + return redirect(reverse('home')) + else: + messages.add_message(request, messages.ERROR, 'Invalid Form Data; Message Not Sent') + + return render(request, 'homeapp/contact.html', {"form": form}) diff --git a/templates/base.html b/templates/base.html index bb3e6c48c20a82d26299fc04d2fd4b0a598518e0..7a26515db0707333ddc10b92bb173273496a7abb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,8 +1,15 @@ {% load static %} +{% load sass_tags %} <!DOCTYPE html> <html lang="en"> <head> + <link rel="stylesheet" type="text/css" href={% sass_src 'css/base.scss' %}> + <script type="text/javascript" src="{% static 'js/jquery-3.6.1.js' %}"></script> + + {% block app_includes %} + {% endblock app_includes %} + {% block title %} {% endblock title %} </head> @@ -11,6 +18,8 @@ {% block header %} {% endblock header %} + {% include 'messages.html' %} + {% block content%} {% endblock content %}