lightweightdjango / examples

Code examples for "Lightweight Django", by Julia Elman and Mark Lavin.
http://bit.ly/lightweightdjango
441 stars 221 forks source link

Issue with chapter 3 and BASEDIR #11

Closed ilovetogetspamed closed 9 years ago

ilovetogetspamed commented 9 years ago

Hello, As I was working with the code in chapter three I got as far as the runserver command prior to figure 3-2 trying to test what I had created so far. Anyhow, it was not rendering any of my pages.

I kept getting "Page not found (404)", I also tried using the example code from github for the chapter. It didn't work either.

I narrowed down the problem to the BASE_DIR setting from my testing.

My Test script


    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    import os

    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    print("executing: os.path.dirname(os.path.dirname(__file__))\nreturns: "
        + BASE_DIR)

    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    print('\n')

    print('executing: os.path.dirname(os.path.abspath(__file__))\nreturns: ' + BASE_DIR)
    print('\n')

The results I got were as follows:  (and yes I did use python 34 ;-) this time)

IN WINDOWS CMD
C:\Users\WindozeUser\Desktop\django\codefix>c:\Python34\python.exe test.py
executing: os.path.dirname(os.path.dirname(__file__))
returns:

executing: os.path.dirname(os.path.abspath(__file__))
returns:  C:\Users\WindozeUser\Desktop\django\codefix

IN Ubuntu BASH


    bashuser@localhost:~$ python test.py
    executing: os.path.dirname(os.path.dirname(__file__))
    returns:

    executing: os.path.dirname(os.path.abspath(__file__))
    returns: /home/bashuser

If I change the 'BASE_DIR = os.path.dirname(os.path.dirname(__file__))' to 'BASE_DIR = os.path.dirname(os.path.abspath(__file__))' (as in my test.py file above) within the prototypes.py file the website works as described in the book.

Being a newbie to all of this I'm a bit confused.  I may be miss informed, but I thought __file__ should be the name of module being executed?  But it doesn't seem to be in this context.

I don't know enough about python to have an opinion.  I just know it didn't work out of the box... which irks me a bit, especially when I've paid for something to learn from.

Which I guess I'm doing ;-)

Thanks
mlavin commented 9 years ago

I can reproduce the weirdness of the BASE_DIR setting though it doesn't produce any 404 issues for me following the commands in the book and this example repo (Python 3.4 on Ubuntu 14.04). It looks like this default has since been changed in Django and worth updating in the book. See https://code.djangoproject.com/ticket/23436 and https://github.com/django/django/commit/caf5cd7ba7c9d73194e394a26c81f1a677d54a6c. Thoughts @juliaelman?

ilovetogetspamed commented 9 years ago

Thanks, again, I just started with Django and wasn't aware that they patched it. Though I don't understand why it continues in to manifest itself. From a fresh project's settings.py file:

...
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
...
mlavin commented 9 years ago

This change won't land in Django until the upcoming 1.8 release this/next month. More just pointing out that others had reported a similar issue which prompted the change which wouldn't be difficult to incorporate here (since this chapter doesn't use startproject). However, it is still isn't clear why you are seeing 404 errors from the info given here.

ilovetogetspamed commented 9 years ago

I forgot the trailing / http://127.0.0.1:8000/spam gives 404. http://127.0.0.1:8000/spam/ give page Thanks again.

ilovetogetspamed commented 9 years ago

Computers are so literal... ;-)