Intermediate Concepts - User Uploaded Images


#1

I am working through the Intermediate Concepts book and on page 42 ran into an issue, perhaps with the wording of the comment.

our helper, add above the new model

def get_image_path(instance, filename):

return ‘/’.join([‘thing_images’, instance.thing.slug, filename])

				class Upload(models.Model):

thing = models.ForeignKey(Thing, related_name=“uploads”)
image = models.ImageField(upload_to=get_image_path)

I add that into my webapp and then try to run the makemigrations but it gives an error.

class Upload(Timestamp):
    ^

SyntaxError: invalid syntax

Not sure why it is invalid. Thanks for any help.


#2

I finally saw what it was complaining about. I didn’t close the def. I need more sleep.


#3

Hahaha yeah that’s pretty much how programming goes!


#4

Yes, once I finally caught it I just laughed and couldn’t believe I had missed it. Oh well, I am moving ahead.

The new book is great! I’ve gotten everything functional so far along with doing some work on the HTML and CSS. I wish I was able to design my site like you do your sites.


#5

Haha, good thing Hello Web Design will be the next book I write! Glad the new book is working well for you. :) :) :)


#6

So I moved past the earlier error but have run into one that I’m not sire where to go after looking through several Stackoverflow postings and other sites related to it. I am able to upload an image just fine through the Admin page but after going through and putting the code in I go to the user I am logged in as and try to run Edit Images it keeps returning a Value error:

The view collection.views.edit_thing_uploads didn’t return an HttpResponse object. It returned None instead.

I’m pretty sure that I typed in everything correctly. I verified my indention’s and that there were return statements as listed in the book.


#7

Upload your view to pastebin.com so I can take a look at it. :)


#8

I just uploaded it:

Link


#9

You have several indentation issues at the beginning:

def profile_detail(request, slug):
        # grab the object
        profile = Profile.objects.get(slug=slug)
        # grab all the object's social accounts
        social_accounts = profile.social_accounts.all()
        # grab the images associated with the profile
        uploads = profile.uploads.all()
        # and pass it to the templace
        return render(request, 'profiles/profile_detail.html', {
        'profile': profile,
        'social_accounts': social_accounts,
        'uploads': uploads,
                })

Should be

def profile_detail(request, slug):
        # grab the object
        profile = Profile.objects.get(slug=slug)
        # grab all the object's social accounts
        social_accounts = profile.social_accounts.all()
        # grab the images associated with the profile
        uploads = profile.uploads.all()
        # and pass it to the templace
        return render(request, 'profiles/profile_detail.html', {
                'profile': profile,
                'social_accounts': social_accounts,
                'uploads': uploads,
        })

Try making sure that your indentation is right throughout the file and try again!


#10

Actually, I have it the way you listed for what it should be. I just pasted it in on pastebin without checking the format afterwards. I will look at the indention to make sure there isn’t a mixture of spaces and tabs.


#11

Can you take a screenshot of the page that shows that indentation so I can update the books? Or let me know what page/version of the books?


#12

The screen capture is is uploaded. I am not sure if it is a book error or something I did wrong.


#13

I was talking about profile_detail(), which is indented wrong in your pastebin code, not edit_thing_uploads(). :)


#14

Here is the screen capture of it in my views.py


#15

Again, your formatting is incorrect. Look at the closing bracket on the return render() statement. It need to be lined up with the parent, like the code I fixed above.


#16

Thanks for the help. That ended up breaking something else and didn’t resolve it. I will just have to dig deeper and figure it out.


#17

I am happy to help more. Do you understand the formatting and why it was incorrect? That’s very important for you, especially for moving forward — getting formatting and spacing correct is definitely step #1 in debugging. :)


#18

Yes, thanks.


#19

Update: I had to go back to page 79 - 80 of the Hello Web App book to find where I had my indentation wrong. Thanks again for the help!


#20

You’re welcome!