Code Monkey home page Code Monkey logo

tango_with_django_2's Introduction

Tango with Django 2 Book Repository

This repository contains the source for the book Tango with Django 2. This is the fourth edition of Tango with Django which has now been heavily revised and upgraded to make use of Django 2.x using Python 3.x. The book was first launched in October 2013 by Dr. Leif Azzopardi and Dr. David Maxwell while both were teaching web development at the University of Glasgow. The book has been used as the course text ever since, with literally thousands of undergraduate and postgraduate students using it over the years. Previous editions of the book are freely available at: www.tangowithdjango.com which has been used by over two million visitors!

We store the source Markdown here. If you have an issue with the book (whether it be a typo, disagreement with a statement that we make, or just a suggestion), please let us know here by raising an issue.

We also have implemented a model implementation of what the book walks you through, complete with gotchas to catch out students copying and pasting. This implementation is housed in a different repository. That repository also houses a number of tests that you can use to check your implementation as you go through the book, chapter-by-chapter.

Note: please don't steal this. If you like it, go to Leanpub and support the authors who have literally put thousands of hours of their free time into making this book for you to use.

tango_with_django_2's People

Contributors

leifos avatar maxwelld90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tango_with_django_2's Issues

Maybe it's better this way

at rango/views.py
def show_category(request, category_name_slug):
context = {
'category_name_slug': category_name_slug,
}
...

and then , the template: category.html
<a href="/rango/category/{{ category_name_slug }}/add_page/">Add Page</a>

use category_name_slug is better than use category.slug , Especially when try a wrong category_name_slug in the URL , and press the 'Add Page' link.

Chapter 15 reverse() URL lookup is missing in the book

In the last version of the book, on page 256, in the code instead of
return redirect(reverse('rango:index'))
it is
return redirect('rango:index')
in the repo it is missing too
https://github.com/maxwelld90/tango_with_django_2_code/blob/36cd3e6d2f8efd6e68634a04ea15b24de606857f/tango_with_django_project/rango/views.py#L220

Also, I don't know, whether it is correct or not, but on page 251,in the piece of code there are double time written line if request.method == 'POST', I guess it has to be written once ?
https://github.com/maxwelld90/tango_with_django_2_code/blob/e5401416fe47a08ed57bc138bda9bae9bfe370cc/tango_with_django_project/rango/views.py#L47

Typo on page 275 & 276

Hi,
As I was reading and implementing the book, I noticed a small typo on pages 275 and 276 (images are attached).
We want to display the list of users registered with rango and therefore in list_profiles.html write

...
<div class="container">
<div class="row">
{% if user_profile_list %}
<div class="panel-body">
<div class="list-group">
{% for list_user in user_profile_list %}
...

But on the next page, while creating a view for it in views.py , the code in the book adds the profiles under the key 'userprofile_list' instead of 'user_profile_list.

class ListProfilesView(View):
    @method_decorator(login_required)
    def get(self, request):
        profiles = UserProfile.objects.all()
        return render(request, 'rango/list_profiles.html', {'userprofile_list': profiles})

This leads to an error. I hope you fix it.
P.S: The book has been great. Love from India!

Screenshots of the book

  1. Page 275
    image
  2. Page 276
    image

Chapter 5: Outadated link

The link 'official Django documentation on the admin interface' points to the oudated version of Django.

Typo in Chapter 5

Hello Dr. David Maxwell and Dr. Leif Azzopardi! In Chapter 5, section Telling Django about Your Database, you've given us some examples about other database engines supported by Django. In the source MarkDown files at this repo, you used PostgresSQL, but I've found that the official website of this database use PostgreSQL. It seems that there is a redundant s here.
I think this is an extraordinary book and I've learned a lot, thank you very much for your efforts!

jQuery category like button doesn't work as expected

On Django 2.2 this doesn't work as expected:

“Don’t forget to add in the URL mapping, into rango/urls.py. Update the urlpatterns by adding in:

path('like$', views.like_category, name='like_category'),”

I read the docs and tried this:
re_path(r'^like/$', views.like_category, name="like_category"),

and it kind of works, but after the first like, every time I refresh once liked category, refresh automatically adds +1 to the number of likes.

In the console it's visible that opening/refreshing the category is requesting 'like_category' view at the same time:

[12/Jul/2019 19:39:02] "GET /rango/like/?category_id=5 HTTP/1.1" 200 2
[12/Jul/2019 19:39:02] "GET /rango/category/python/ HTTP/1.1" 200 6311

Typo in Chapter 3.6

In the Creating a new Django App section, there is a typo in the file name, it reads view.py but should read views.py:

-5.  In your app's `view.py`, create the required views ensuring that they return a `HttpResponse` object.
+5.  In your app's `views.py`, create the required views ensuring that they return a `HttpResponse` object.

{% load staticfiles %} removed/depreciated

If you use {% load staticfiles %} in Django 3 you will get the following error: 'staticfiles' is not a registered tag library.
This is because it was completely removed in Django 3 and replaced with {% load static %}
I know the book is for Django 2.x but this may be worth mentioning because it was also deprecated in Django 2.1, aside from being completely removed in 3.

The book is great,
Thank you.

Exercises should have always a solution as a code to view on GitHub

Most of the exercises are explained or provided as a code in GitHub, but this one cracked my mind recently:

“Class Based View Exercises
Go through the Django Documentation and study how to create Class-Based Views.
Update the Rango application to use Class-Based Views.
Tweet how awesome you are and let us know @tangowithdjango.”

I have updated a RestrictedView, AddCategoryView etc. but I am trying to solve this one third day in a row and I'm stuck finding a solution for making function based show_category into Class Based Category View. I don't know if I should use DetailView, ListView, or FormView (due to the search functionality within Category page).

Books should have exercises and the solutions for them. This one seems like a big challange and I was surprised that in the GitHub code there's no solution to this exercise.

Creating a Suggest Category View - problems with the code

In the recent version of the book the code for this functionality doesn't include the last return line (which is visible in the code on github):

return render(request, 'rango/cats.html', {'cats': cat_list })

https://www.dropbox.com/s/5bkjot0uznns7q1/Screenshot%202019-07-12%2022.10.20.png?dl=0

Furthermore, for some reasons this functionality only works on Chrome (I have not tested the other browsers), but doesn't work on Safari (newest MacOS with all the updates). The request is not visible in the console, when searching for a category on Safari, whereas it is when doing the same on Chrome.

Safari:
https://www.dropbox.com/s/66ewaz15t7n8pjf/Screenshot%202019-07-12%2022.15.11.png?dl=0

Chrome:
https://www.dropbox.com/s/rbj7guln05it92s/Screenshot%202019-07-12%2022.15.43.png?dl=0

Chapter 4: Errors in the test module

Trying to use the test module to test the results which I've got after completing Chapter 4.

The content of my about.html is the following:

<!DOCTYPE html>
<html>
     <head>
     </head>
     <body>
          <h1> About </h1>
          <img src="{{MEDIA_URL}}resin.jpg" alt="Галоша" />
          <p>
               This tutorial has been put together by Marshwiggle
          </p>
     </body>
</html>

So, when I start the module, I get a buch of errors:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...E.EEE.....The module populate_rango does not exist
EThe module populate_rango does not exist
EThe module populate_rango does not exist
E
======================================================================
ERROR: test_about_contains_create_message (rango.tests.Chapter4ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 130, in test_about_contains_create_message
    self.assertIn('This tutorial has been put together by', response.content)
  File "/usr/lib/python3.5/unittest/case.py", line 1076, in assertIn
    if member not in container:
TypeError: a bytes-like object is required, not 'str'

======================================================================
ERROR: test_does_about_contain_img (rango.tests.Chapter4ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 125, in test_does_about_contain_img
    self.assertIn('img',response.content)
  File "/usr/lib/python3.5/unittest/case.py", line 1076, in assertIn
    if member not in container:
TypeError: a bytes-like object is required, not 'str'

======================================================================
ERROR: test_does_index_contain_img (rango.tests.Chapter4ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 114, in test_does_index_contain_img
    self.assertIn('img',response.content)
  File "/usr/lib/python3.5/unittest/case.py", line 1076, in assertIn
    if member not in container:
TypeError: a bytes-like object is required, not 'str'

======================================================================
ERROR: test_index_contains_hello_message (rango.tests.Chapter4ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 109, in test_index_contains_hello_message
    self.assertContains( response.content,'Rango says')
  File "<MY_WORKING_PATH>/tango/rangoenv/lib/python3.5/site-packages/django/test/testcases.py", line 361, in assertContains
    response, text, status_code, msg_prefix, html)
  File "<MY_WORKING_PATH>/tango/rangoenv/lib/python3.5/site-packages/django/test/testcases.py", line 331, in _assert_contains
    response.status_code, status_code,
AttributeError: 'bytes' object has no attribute 'status_code'

======================================================================
ERROR: test_python_cat_added (rango.tests.ModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 91, in test_python_cat_added
    cat = self.get_category('Python')
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 83, in get_category
    from rango.models import Category
ImportError: cannot import name 'Category'

======================================================================
ERROR: test_python_cat_with_likes (rango.tests.ModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 99, in test_python_cat_with_likes
    cat = self.get_category('Python')
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 83, in get_category
    from rango.models import Category
ImportError: cannot import name 'Category'

======================================================================
ERROR: test_python_cat_with_views (rango.tests.ModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 95, in test_python_cat_with_views
    cat = self.get_category('Python')
  File "<MY_WORKING_PATH>/tango/tango_with_django_project/rango/tests.py", line 83, in get_category
    from rango.models import Category
ImportError: cannot import name 'Category'

----------------------------------------------------------------------
Ran 16 tests in 0.089s

FAILED (errors=7)
Destroying test database for alias 'default'...

I've tested my app in the browser and it seems to be working, so perhaps, there are some bugs in the tests.

For example, 'test_about_contains_create_message' method of the 'AboutPageTests' object is almost identical to the method with the same name in the Chapter4ViewTests object, but it raises no error, and that's because it has a type hint (i.e. "b" character) before the string in the last line.

Git directory structure question

I just bought your book and started to go through the first steps. I setup a small Raspberry Pi as a platform and have everything up and running. I got through with Django Basics and was proud to have my first webpage running. At the end of that chapter, you suggest to get started with version control. So I studied the Git Crash Course, but now I'm kinda stuck. I'm not quite clear about how to organize the files.

Git has four separate storage locations.

  • The workspace to have the Django project files. So I assume this is the ~/workspace/tango_with_django_project directory
  • The local repository. I created a ~/git/rango directory and used that to git clone the repository created on https://github.com/
  • The remote repository, created on github.com
  • The local index

My main confusion now is how to "add" files to the local repository. Do I have to just copy the files over using a cp -r ~/workspace/tango_with_django_project ~/git/rango as an initial step and then every time I make a change to any of the files in the workspace directory? And once they are copied over, I have to git add all the files?

What is the best practice to copy back and forth files from the local repository to the workspace? Using only the terminal, it seems a bit cumbersome to track all the files with changes, especially if you pull changes made by other team-members, and getting them into the workspace directory.

Either I have not found it in the book, or am I missing something trivial here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.