diff --git a/Lecture1/myproject/boards/__pycache__/tests.cpython-36.pyc b/Lecture1/myproject/boards/__pycache__/tests.cpython-36.pyc index c209d87396ba39710200361e59b90e6089d7be17..ddddac05ec04d811223d7a5a8c616add86509909 100644 Binary files a/Lecture1/myproject/boards/__pycache__/tests.cpython-36.pyc and b/Lecture1/myproject/boards/__pycache__/tests.cpython-36.pyc differ diff --git a/Lecture1/myproject/boards/__pycache__/views.cpython-36.pyc b/Lecture1/myproject/boards/__pycache__/views.cpython-36.pyc index 187548c89cf5f4edfe5daec7b8289832a718e1e4..eacc6c1ead6f631aac70f17eeb53822a4512e473 100644 Binary files a/Lecture1/myproject/boards/__pycache__/views.cpython-36.pyc and b/Lecture1/myproject/boards/__pycache__/views.cpython-36.pyc differ diff --git a/Lecture1/myproject/boards/tests.py b/Lecture1/myproject/boards/tests.py index 31c6f40a98304e1deff0bda581919fba46ae0520..ea8e8ef268d3a7b9858150cbf030af706e2bf566 100644 --- a/Lecture1/myproject/boards/tests.py +++ b/Lecture1/myproject/boards/tests.py @@ -1,6 +1,6 @@ from django.test import TestCase from django.urls import reverse, resolve -from .views import home, board_topics +from .views import home, board_topics, new_topic from .models import Board @@ -33,4 +33,29 @@ class BoardTopicsTests(TestCase): # resolve("/boards/1/") is different as resolve("/boards/1") def test_board_topics_url_resolves_board_topics_view(self): view = resolve("/boards/1/") - self.assertEquals(view.func, board_topics) \ No newline at end of file + self.assertEquals(view.func, board_topics) + + +class NewTopicTests(TestCase): + def setUp(self): + Board.objects.create(name="Django", description="Django board.") + + def test_new_topic_view_success_status_code(self): + url = reverse("new_topic", kwargs={"board_id": 1}) + response = self.client.get(url) + self.assertEquals(response.status_code, 200) + + def test_new_topic_view_not_found_status_code(self): + url = reverse("new_topic", kwargs={"board_id": 99}) + response = self.client.get(url) + self.assertEquals(response.status_code, 404) + + def test_new_topic_url_resolves_new_topic_view(self): + view = resolve("/boards/1/new/") + self.assertEquals(view.func, new_topic) + + def test_new_topic_view_contains_link_back_to_board_topics_view(self): + new_topic_url = reverse('new_topic', kwargs={"board_id": 1}) + board_topics_url = reverse('board_topics', kwargs={'board_id': 1}) + response = self.client.get(new_topic_url) + self.assertContains(response, 'href="{0}"'.format(board_topics_url)) \ No newline at end of file diff --git a/Lecture1/myproject/boards/views.py b/Lecture1/myproject/boards/views.py index 5d45a96e562381d55d62d4e5d6467db41c8e55eb..62f94a91b563e0b7e10fce112b8f5aca3e45750c 100644 --- a/Lecture1/myproject/boards/views.py +++ b/Lecture1/myproject/boards/views.py @@ -1,9 +1,8 @@ -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse from django.http import Http404 from .models import Board - def home(request): boards = Board.objects.all() """ @@ -25,3 +24,8 @@ def board_topics(request, board_id): raise Http404 return render(request, 'topics.html', {'board': board}) + +def new_topic(request, board_id): + board = get_object_or_404(Board, pk=board_id) + return render(request, 'new_topic.html', {'board': board}) + diff --git a/Lecture1/myproject/db.sqlite3 b/Lecture1/myproject/db.sqlite3 index f496c5b875e9faedd723e6b1785317ae3c937f28..9ab07beb707818b1ce1d9fd68f87485b1a0d4bca 100644 Binary files a/Lecture1/myproject/db.sqlite3 and b/Lecture1/myproject/db.sqlite3 differ diff --git a/Lecture1/myproject/myproject/__pycache__/urls.cpython-36.pyc b/Lecture1/myproject/myproject/__pycache__/urls.cpython-36.pyc index e11f359e0cee58bb35e8f3e04531a836ba09fe4d..d0c8dcafca0d39cb3d22d0c6119a932e8996bf32 100644 Binary files a/Lecture1/myproject/myproject/__pycache__/urls.cpython-36.pyc and b/Lecture1/myproject/myproject/__pycache__/urls.cpython-36.pyc differ diff --git a/Lecture1/myproject/myproject/urls.py b/Lecture1/myproject/myproject/urls.py index 47bea4c1be0abb3dfced4e53a059736db9038a49..e4c9c174944484d1ee2790fc4f44f2409bc1ae1f 100644 --- a/Lecture1/myproject/myproject/urls.py +++ b/Lecture1/myproject/myproject/urls.py @@ -21,7 +21,8 @@ from boards import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.home, name='home'), - path('boards/<int:board_id>/', views.board_topics, name='board_topics') + path('boards/<int:board_id>/', views.board_topics, name='board_topics'), + path('boards/<int:board_id>/new/', views.new_topic, name='new_topic'), #path(r'^(?P<username>[\w.@+-]+)/$', views.user_profile, name="user_profile") ] diff --git a/Lecture1/myproject/static/css/app.css b/Lecture1/myproject/static/css/app.css new file mode 100644 index 0000000000000000000000000000000000000000..b5266d53ea79348d66a0895fb1d876ce401819d2 --- /dev/null +++ b/Lecture1/myproject/static/css/app.css @@ -0,0 +1,3 @@ +.navbar-brand { + font-family: 'MuseoModerno', cursive; +} \ No newline at end of file diff --git a/Lecture1/myproject/templates/base.html b/Lecture1/myproject/templates/base.html new file mode 100644 index 0000000000000000000000000000000000000000..5e67bcb0eb217c48818b02aec17153a1a33731fb --- /dev/null +++ b/Lecture1/myproject/templates/base.html @@ -0,0 +1,27 @@ +{% load static %}<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>{% block title %}Django Boards{% endblock %}</title> + <link href="https://fonts.googleapis.com/css2?family=MuseoModerno:wght@600&display=swap" rel="stylesheet"> + <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> + <link rel="stylesheet" href="{% static 'css/app.css' %}"> + </head> + <body> + + <nav class="navbar navbar-expand-ld navbar-dark bg-dark"> + <div class="container"> + <a class="navbar-brand" href="{% url 'home' %}">Django Boards</a> + </div> + + </nav> + <div class="container"> + <ol class="breadcrumb my-4"> + {% block breadcrumb %} + {% endblock %} + </ol> + {% block content %} + {% endblock %} + </div> + </body> +</html> \ No newline at end of file diff --git a/Lecture1/myproject/templates/home.html b/Lecture1/myproject/templates/home.html index 167b3efadf8a55f31cefe36641df701e74b83592..052fdfa624022d42f4ca414930c3e13c9403ec44 100644 --- a/Lecture1/myproject/templates/home.html +++ b/Lecture1/myproject/templates/home.html @@ -1,38 +1,31 @@ -{% load static %}<!DOCTYPE html> -<html> - <head> - <meta charset="utf-8"> - <title>Boards</title> - <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> - </head> - <body> - <div class="container"> - <ol class="breadcrumb my-4"> - <li class="breadcrumb-item active">Boards</li> - </ol> - <table class="table"> - <thead class="thead-inverse"> - <tr> - <th>Board</th> - <th>Posts</th> - <th>Topics</th> - <th>Last Post</th> - </tr> - </thead> - <tbody> - {% for board in boards %} - <tr> - <td> - {{ board.name }} - <small class="text-muted d-block">{{ board.description }}</small> - </td> - <td class="align-middle">0</td> - <td class="align-middle">0</td> - <td></td> - </tr> - {% endfor %} - </tbody> - </table> - </div> - </body> -</html> \ No newline at end of file +{% extends 'base.html' %} + +{% block breadcrumb %} + <li class="breadcrumb-item active">Boards</li> +{% endblock %} + +{% block content %} + <table class="table"> + <thead class="thead-inverse"> + <tr> + <th>Board</th> + <th>Posts</th> + <th>Topics</th> + <th>Last Post</th> + </tr> + </thead> + <tbody> + {% for board in boards %} + <tr> + <td> + <a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a> + <small class="text-muted d-block">{{ board.description }}</small> + </td> + <td class="align-middle">0</td> + <td class="align-middle">0</td> + <td></td> + </tr> + {% endfor %} + </tbody> + </table> +{% endblock %} \ No newline at end of file diff --git a/Lecture1/myproject/templates/new_topic.html b/Lecture1/myproject/templates/new_topic.html new file mode 100644 index 0000000000000000000000000000000000000000..c154e45bda675436fe517722531115cf15027e85 --- /dev/null +++ b/Lecture1/myproject/templates/new_topic.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block title %}Start a new topic{% endblock %} + +{% block breadcrumb %} + <li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li> + <li class="breadcrumb-item"><a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a></li> + <li class="breadcrumb-item active">New topic</li> +{% endblock %} + +{% block content %} +<form method="post"> + {% csrf_token %} + <div class="form-group"> + <label for="id_subject">Subject</label> + <input type="text" class="form-control" id="id_subject" name="subject"> + </div> + <div class="form-group"> + <label for="id_message">Message</label> + <textarea class="form-control" id="id_message" name="message" rows="5"></textarea> + </div> + <button type="submit" class="btn btn-success">Post</button> +</form> + +{% endblock %} \ No newline at end of file diff --git a/Lecture1/myproject/templates/topics.html b/Lecture1/myproject/templates/topics.html index 80c7eae1a5c0f32fd9727a831f95cf2f90cb6199..08640795c5e622120e157f6a80d7cc0cb5d1b597 100644 --- a/Lecture1/myproject/templates/topics.html +++ b/Lecture1/myproject/templates/topics.html @@ -1,16 +1,14 @@ -{% load static %}<!DOCTYPE html> -<html> - <head> - <meta charset="utf-8"> - <title>{{ board.name }}</title> - <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> - </head> - <body> - <div class="container"> - <ol class="breadcrumb my-4"> - <li class="breadcrumb-item">Boards</li> - <li class="breadcrumb-item active">{{ board.name }}</li> - </ol> - </div> - </body> -</html> \ No newline at end of file +{% extends 'base.html' %} + +{% block title %} + {{ board.name }} - {{ block.super }} +{% endblock %} + +{% block breadcrumb %} + <li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li> + <li class="breadcrumb-item active">{{ board.name }}</li> +{% endblock %} + +{% block content %} + <!-- just leaving it empty for now. we will add core here soon. --> +{% endblock %} \ No newline at end of file