Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
.DS_Store
*.py[cod]
*.swp
*.py.save

/.*
!/.gitignore
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

# python manage.py runserver

# python manage.py migrate
# python manage.py migrate

#DOUBT: tests.py -> 'href="{0}"'.format(homepage_url) what does "{0}" mean?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{0} is the placeholder for the first variable (hence 0, for variable 0) that goes in the format function. In that case, you only have one variable: homepage_url.

If homepage_url="https://pipetilla.com" then the result would be:
'href="https://pipetilla.com"'

That sintax is also equivalent to:

'href="{}"'.format(homepage_url)

or

'href="{homepage_url}"'.format({'homepage_url': homepage_url})

From Python 3.6 onwards, you can use also f-strings:

f'href="{homepage_url}"'

There is more interesting info here:
https://realpython.com/python-f-strings/

5 changes: 5 additions & 0 deletions boards/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
from django.contrib import admin

# Register your models here.
from django.contrib import admin
from .models import Board

admin.site.register(Board)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, you haven't reach that yet, but you can also configure your own admin classes. I.e.:

class BoardAdmin(admin.AdminModel):
    list_display = [.....] -> display them in the list view, on the admin
    list_editable = [.....] -> for the ones displayed, the ones on this list will be editable from the list view
    list_filter = [.....] -> creates a filter on the side panel with occurrences. Useful for booleans and choice fields
    search_fields = [....]  -> creates a search bar and you will be able to search for the fields on this list. I.e put `name` and look for a name you know, it will filter results based on that
    ......

admin.site.register(Board, BoardAdmin)

This is actually very easy to use and extremely useful. Try including Board attributes on the list_display, etc.

Like:
list_display = ["name", "description]

etc

You can also build admins for the rest of the models! Don't forget to register them! 😄

43 changes: 39 additions & 4 deletions boards/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
# -*- coding: utf-8 -*-
from django.urls import reverse, resolve
from django.test import TestCase
from .views import home
from .views import home, board_topics
from .models import Board
from django.db import models



class HomeTests(TestCase):
def test_home_view_status_code(self):
def setUp(self):
self.board = Board.objects.create(name='DjangoTest', description='This is another django test')
url = reverse('home')
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.response = self.client.get(url)

def test_home_view_status_code(self):
self.assertEquals(self.response.status_code, 200)

def test_home_url_resolves_home_view(self):
view = resolve('/homepage/')
self.assertEquals(view.func, home)

def test_home_view_contains_link_to_topics_page(self):
board_topics_url = reverse('board_topics', kwargs={'pk': self.board.pk})
self.assertContains(self.response, 'href="{0}"'.format(board_topics_url))

class BoardTopicsTests(TestCase):
def setUp(self):
Board.objects.create(name='Django_test', description='Django board to test')

def test_board_topics_view_success_status_code(self):
url = reverse('board_topics', kwargs={'pk': 1})
response = self.client.get(url)
self.assertEquals(response.status_code, 200)

def test_board_topics_view_not_found_status_code(self):
url = reverse('board_topics', kwargs={'pk': 99})
response = self.client.get(url)
self.assertEquals(response.status_code, 404)

def test_board_topics_url_resolves_board_topics_view(self):
view = resolve('/boards/1/')
self.assertEquals(view.func, board_topics)

def test_board_topics_view_contains_link_back_to_homepage(self):
homepage_url = reverse('home')
board_topics_url = reverse('board_topics', kwargs={'pk': 1})
response = self.client.get(board_topics_url)
self.assertContains(response, 'href="{0}"'.format(homepage_url))
6 changes: 5 additions & 1 deletion boards/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.shortcuts import render, get_object_or_404

# Create your views here.
from django.http import HttpResponse
Expand All @@ -11,3 +11,7 @@
def home(request):
boards = Board.objects.all()
return render(request, 'home.html', {'boards': boards})

def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
return render(request, 'topics.html', {'board': board})
Empty file removed initial
Empty file.
3 changes: 2 additions & 1 deletion myproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from boards import views

urlpatterns = [
url(r'^homepage/$',views.home,name='home'),
url(r'^homepage/$', views.home, name='home'),
url(r'^boards/(?P<pk>\d+)/$', views.board_topics, name='board_topics'),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually comes from an old django version, <3.0 for sure. The syntax changes a bit to path instead of url

url(r'^admin/', admin.site.urls),
]
14 changes: 7 additions & 7 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<ol class="breadcrumb my-4">
<li class="breadcrumb-item active">Boards</li>
</ol>
<table border="1">
<thead>
<table class="table">
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Element table with class table... That's either redundant or not very descriptive, as you want classes for specific things like: boardsTable or pipetasTableBlue and then apply css to them. I know you were following the tutorial, but I thought it was worth it mentioning it.

In CSS, you can apply the style to the element, in this case table:

table {
    border=1;
}

or the class, in this case..... table again.....(you see why it is confusing?):

.table {
    border=1;
}

First one applies to all elements table, second one applies to all elements with class="table". 😄

<thead class="thead-inverse">
<tr>
<th>Board --------------------</th>
<th>Board</th>
<th>Posts</th>
<th>Topics</th>
<th>Last Post</th>
Expand All @@ -23,11 +23,11 @@
{% for board in boards %}
<tr>
<td>
{{ board.name }}<br>
<small style="color: #888">{{ board.description }}</small>
<a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a>
<small class="text-muted d-block">{{ board.description }}</small>
</td>
<td>0</td>
<td>0</td>
<td class="align-middle">0</td>
<td class="align-middle">0</td>
<td></td>
</tr>
{% endfor %}
Expand Down
16 changes: 16 additions & 0 deletions templates/topics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% load static %} <!DOCTYPE html>
<html lang="en">
<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"><a href="{% url 'home' %}">Boards</a></li>
<li class="breadcrumb-item active">{{ board.name }}</li>
</ol>
</div>
</body>
</html>