"User" Not Defined


#1

I am working my way through the HelloWebApp Intermediate Concepts.
On page 19 in the section headed ‘Helper functions on your model’, there is a snippet of code for models.py:-

class Thing(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField()
user = models.OneToOneField(User, blank=True, null=True)

# new helper method
def get_absolute_url(self):
    return "/things/%s/" % self.slug

Where did the user field come from as I can’t see it previously?
I have commented it out as it throws an error “NameError: name ‘User’ is not defined” if left in.

Regards David


#2

Hi David — the user field is just an example, it’s actually put into place in the original Hello Web App book (the step by step tutorial). Basically, the whole class is just an example class, and the bold is the only code you need to pay attention too. :)


#3

Doh!.. What a numpty I am (lol)… However, I am now having another problem:-

In the section “Showing the image in the templates”, I have added the sections to to the urls.py but I am getting an exception:and cannot seem to figure out the problem:-

raise TypeError(‘view must be a callable or a list/tuple in the case of include().’)
TypeError: view must be a callable or a list/tuple in the case of include().


#4

I’ve discovered what the problem is but I am still trying to resolve it. I am running Django 1.10.

Django 1.10 no longer allows you to specify views as a string (e.g. ‘myapp.views.home’) in your URL patterns.

David


#5

Hmmm the book should be updated for that. Where did you get the book?


#6

Hi, I got it from amazon.co.uk


#7

I have now downgraded my Django to version 1.9 and everything works a treat. I checked out the code on github and it is the same as in the book. It also does says in your book that you are using 1.9 and I don’t expect it to work with other versions as Django is a fast moving target. With every new version of Django, there are always some fairly major changes.

Thanks for your help.


#8

Yeah, 1.9 allows for the old system I believe, whereas 1.10 isn’t compatible.

Sounds like you have an old version of the book and unfortunately with Amazon, I can’t send out new versions of the book when something updates. I updated the book they sell to the new urls.py structure, but they don’t proactively update books (as far as I can tell? It’s a very confusing system.)

Email me at [email protected] and I can send you a new mobi file directly.