tangowithcode / tango_with_django_2_code Goto Github PK
View Code? Open in Web Editor NEWDjango 2 and Python 3: Tango With Django 2 Code Repository (for book 2020-01a)
Home Page: https://www.tangowithdjango.com
License: MIT License
Django 2 and Python 3: Tango With Django 2 Code Repository (for book 2020-01a)
Home Page: https://www.tangowithdjango.com
License: MIT License
Completed exercises until end of Chapter 3.
Upon executing the tests for chapter 3 get following error:
File "/home/aspma/python/django/tango_with_django_project/rango/tests_chapter3.py", line 26
FAILURE_HEADER = f"{os.linesep}{os.linesep}{os.linesep}================{os.linesep}TwD TEST FAILURE =({os.linesep}================{os.linesep}"
----------------------------------------------- Console Output --------------------------------------------
SyntaxError: invalid syntax
(rangoenv) aspma@aspma-VirtualBox:~/python/django/tango_with_django_project$ python manage.py test rango.tests_chapter3
Traceback (most recent call last):
File "manage.py", line 15, in
execute_from_command_line(sys.argv)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/init.py", line 381, in execute_from_command_line
utility.execute()
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/init.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/test/runner.py", line 603, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/home/aspma/Envs/rangoenv/lib/python3.5/site-packages/django/test/runner.py", line 485, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName
module = import(module_name)
File "/home/aspma/python/django/tango_with_django_project/rango/tests_chapter3.py", line 26
FAILURE_HEADER = f"{os.linesep}{os.linesep}{os.linesep}================{os.linesep}TwD TEST FAILURE =({os.linesep}================{os.linesep}"
^
SyntaxError: invalid syntax
----------------------------------------------- Console Output --------------------------------------------
One of the unit tests from chapter 3 requires the directory name to be exactly "tango_with_django_project". This is a bit inconsistent with the instructions from the book (page 27).
This command will invoke the django-admin.py script, which will set up a new Django project called tango_with_django_project for you. Naming conventions dictate that we would typically append _project to the end of our Django project directories so we know exactly what they contain โ but naming this is really entirely up to you.
Unit test case:
def test_project_created(self):
"""
Tests whether the tango_with_django_project configuration directory is present and correct.
"""
directory_exists = os.path.isdir(os.path.join(self.project_base_dir, 'tango_with_django_project'))
urls_module_exists = os.path.isfile(os.path.join(self.project_base_dir, 'tango_with_django_project', 'urls.py'))
self.assertTrue(directory_exists, f"{FAILURE_HEADER}Your tango_with_django_project configuration directory doesn't seem to exist. Did you use the correct name?{FAILURE_FOOTER}")
self.assertTrue(urls_module_exists, f"{FAILURE_HEADER}Your project's urls.py module does not exist. Did you use the startproject command?{FAILURE_FOOTER}")
My suggestion is to remove the directory assertion and leave only the "urls_module_exists" -- adapting it to ignore the project name.
The required str in test_about_contains_rango:
required_str = f"<img src=\"{settings.MEDIA_URL}cat.jpg\" alt=\"Picture of a Cat\" />"
fails if about.html contains context variable like:
<img src="{{ MEDIA_URL }}cat.jpg" alt="Picture of a Cat.">
only was to pass the test seems to be to hard code the path to media:
<img src="/media/cat.jpg" alt="Picture of a Cat" />
Not sure if this is intended or if the context variable for media should also pass the test.
Hello,
I understand that by overriding the clean() method in PageForm class the URL box on the ../add_page/ webpage should also accept URLs that don't begin with 'http://'.
But it appears that the method is called only after the URL box is validated for incorrect format of URL, therefore it's never called until you type the URL correctly formatted in the first place.
I tried cloning your chapter7 branch and it produced the same result as my project.
Is there a mistake or do I understand the purpose of the method in a wrong way?
Sorry for doing the actions so late. I figured out where is the problem for can't pass the test even the page shows the correct content. The HTML code for showing the picture has to be exactly same as
<img src="{% static 'images/rango.jpg' %}" alt="Picture of Rango" />
No SPACE or LINE BREAK is allowed ๐ต
I personally think that maybe a little bit too harsh for people who need to pass the test to get the grade like university students. I wish it can be changed to a more friendly test : ) ๐
Hello,
I have finished Chapter 8 and when doing the tests, I get 1 failure - I've been looking over the code for a couple days and can't seem to work out what I have to change. If you could help that would be great - think i'm just not understanding the error. I have included my github link if you need to look at that.
Hi,
Really enjoying the book so far, I am on the bootstrap chapter, I found the link to the modified HTML source of the dashboard link does not work. Could you please provide an alternate link ?
This is on page 163 and the link provided is: http://bit.ly/twd-bootstrap-base
Thanks
Hi -
A definite newbie to Django (and even Python) and I'm getting the below error when attempting Chapter 11:
"File "/Users/kent/.virtualenvs/rangoprojenv/lib/python3.8/site-packages/registration/models.py", line 21, in
from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (/Users/kent/.virtualenvs/rangoprojenv/lib/python3.8/site-packages/django/utils/encoding.py)"
I've googled the above and the solutions I find discuss altering external libraries which I'm definitely not comfortable with doing at the moment (especially as one of the lines could not even be found - otherwise might have attempted it).
Is it something I missed in the book, a bug on my part, or any chance a compatibility error as most posts I've seen have hinted at? Not much coding in Chapter 11 and can't see where I might have gone wrong (though I've coded enough to know it could definitely be me!).
New to Github too but will post code if the need arises.
Thanks,
Kent
In Creating a Population Script in chapter 5 of the book, the sample code gives the URL for Django Rocks as http://www.djangorocks.com/, with a trailing forward slash.
In the test_index_pages
method on line 108 of tests_chapter6.py
, the index view responses are checked using regex, including a check for Django Rocks as http://www.djangorocks.com, without the trailing forward slash.
This of course results in the test failing.
The tests for Chapter 5 are producing the error message on line 178 of the tests_chapter5.py file. The error message reads:
"We couldn't find the correct output in the Page view within the admin interface for page listings. Did you complete the exercises, adding extra columns to the admin view for this model? Are the columns in the correct order?"
I can see that the pertinent test checks whether expected_str is in response_body. When I print response_body, the string is identical to expected_str except for the start of the string, which begins instead of , but everything else is the same.
I.e. response_body reads:
instead of:
The code in my admin.py file for the project reads as follows:
"from django.contrib import admin
from rango.models import Category, Page
class PageAdmin (admin.ModelAdmin):
list_display = ('title', 'category', 'url')
admin.site.register(Category)
admin.site.register(Page,PageAdmin)"
which matches the code suggested in the Excercise Hints part of the book on Page 89. The code also produces the correct Pages view on the /admin/ site:
The above is a screenshot of the /admin/ page from my project which exactly matches the screenshot on page 88 of the book.
I am running a later version of Django than suggested in the book (4.1), which perhaps explains this error.
Can you please confirm whether there is something wrong with my implementation or whether the error is produced due to external reasons (e.g. using a later version of Django)? Thanks in advance for your help!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.