Forms.py funsies syntax error!


#1

Help! I’m on page 60 of the fantastic book.

I’ve got an error.
I can’t figure out where to fix the error.
It tells me the error is a syntax error in my views.py code at:
return redirect(‘thing_detail’, slug=thing.slug)

  1. I don’t know where to go to fix the error…
  2. can I just delete it and try again?
  3. Is there a good place for me to understand the “commit” function? - if I can figure out how to fix my error, I want to use that function.

Thanks!

SyntaxError at /admin/collection/thing/

‘return’ outside function (views.py, line 25)

Request Method: GET
Request URL: http://localhost:8000/admin/collection/thing/
Django Version: 1.8.4
Exception Type: SyntaxError
Exception Value:

‘return’ outside function (views.py, line 25)

Exception Location: /home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/importlib/init.py in import_module, line 109
Python Executable: /home/juicebox/my_project_folder/hellowebapp/venv/bin/python
Python Version: 3.4.3
Python Path:

[’/home/juicebox/my_project_folder/hellowebapp’,
’/home/juicebox/my_project_folder/hellowebapp/venv/lib/python34.zip’,
’/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4’,
’/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/plat-linux’,
’/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/lib-dynload’,
’/home/juicebox/.pyenv/versions/3.4.3/lib/python3.4’,
’/home/juicebox/.pyenv/versions/3.4.3/lib/python3.4/plat-linux’,
’/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages’]

Server time: Mon, 28 Sep 2015 04:04:53 +0000
Error during template rendering

In template /home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 11
’return’ outside function
1 {% extends “admin/base_site.html” %}
2 {% load i18n admin_urls admin_static admin_list %}
3
4 {% block extrastyle %}
5 {{ block.super }}
6 <link rel=“stylesheet” type=“text/css” href="{% static “admin/css/changelists.css” %}" />
7 {% if cl.formset %}
8 <link rel=“stylesheet” type=“text/css” href="{% static “admin/css/forms.css” %}" />
9 {% endif %}
10 {% if cl.formset or action_form %}
11

12 {% endif %}
13 {{ media.css }}
14 {% if not actions_on_top and not actions_on_bottom %}
15
16 #changelist table thead th:first-child {width: inherit}
17
18 {% endif %}
19 {% endblock %}
20
21 {% block extrahead %}
Traceback Switch to copy-and-paste view

/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/handlers/base.py in get_response

                                response = response.render()

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/response.py in render

                            self.content = self.rendered_content

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/response.py in rendered_content

                        content = template.render(context, self._request)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/backends/django.py in render

                        return self.template.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                    return self._render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in _render

                        return self.nodelist.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                bit = self.render_node(node, context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/debug.py in render_node

                            return node.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/loader_tags.py in render

                        return compiled_parent._render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in _render

                        return self.nodelist.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                bit = self.render_node(node, context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/debug.py in render_node

                            return node.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/loader_tags.py in render

                        return compiled_parent._render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in _render

                        return self.nodelist.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                bit = self.render_node(node, context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/debug.py in render_node

                            return node.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/loader_tags.py in render

                                result = block.nodelist.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                bit = self.render_node(node, context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/debug.py in render_node

                            return node.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/defaulttags.py in render

                                return nodelist.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/base.py in render

                                bit = self.render_node(node, context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/debug.py in render_node

                            return node.render(context)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/template/defaulttags.py in render

                            url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in reverse

                                app_list = resolver.app_dict[ns]

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in app_dict

                            self._populate()

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _populate

                                lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in callback

                        self._callback = get_callable(self._callback_str)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/functools.py in wrapper

                                result = user_function(*args, **kwds)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in get_callable

                        mod = import_module(mod_name)

     ...
▶ Local vars
/home/juicebox/my_project_folder/hellowebapp/venv/lib/python3.4/importlib/__init__.py in import_module

                    return _bootstrap._gcd_import(name[level:], package, level)

     ...
▶ Local vars

Request information
GET

No GET data
POST

No POST data
FILES

No FILES data
COOKIES
Variable Value
csrftoken

‘9USN1IsUOkGk8QkCZMer7INL01mElHTi’

sessionid

‘ekgvrs0avx54lqhgelsv4y0dvala4b0x’

META
Variable Value
GDMSESSION

‘xubuntu’

UPSTART_SESSION

‘unix:abstract=/com/ubuntu/upstart-session/1000/1468’

NVM_DIR

‘/home/juicebox/.nvm’

PYENV_SHELL

‘bash’

wsgi.url_scheme

‘http’

SESSIONTYPE

‘’

UPSTART_EVENTS

‘started xsession’

HTTP_USER_AGENT

‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0’

RUN_MAIN

‘true’

XDG_SESSION_ID

‘c1’

MANPATH

‘/home/juicebox/.nvm/versions/io.js/v2.0.1/share/man:/home/juicebox/.rvm/rubies/ruby-2.1.5/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/home/juicebox/.rvm/man’

NVM_BIN

‘/home/juicebox/.nvm/versions/io.js/v2.0.1/bin’

PWD

‘/home/juicebox/my_project_folder/hellowebapp’

REMOTE_ADDR

‘127.0.0.1’

XDG_GREETER_DATA_DIR

‘/var/lib/lightdm-data/juicebox’

VIRTUAL_ENV

‘/home/juicebox/my_project_folder/hellowebapp/venv’

XDG_SESSION_PATH

‘/org/freedesktop/DisplayManager/Session0’

NVM_IOJS_ORG_VERSION_LISTING

https://iojs.org/dist/index.tab

UPSTART_INSTANCE

‘’

wsgi.input

<_io.BufferedReader name=6>

rvm_prefix

‘/home/juicebox’

QUERY_STRING

‘’

XAUTHORITY

‘/home/juicebox/.Xauthority’

wsgi.multiprocess

False

GLADE_MODULE_PATH

‘:’

HTTP_ACCEPT_LANGUAGE

‘en-US,en;q=0.5’

PATH_INFO

‘/admin/collection/thing/’

COLORTERM

‘xfce4-terminal’

HTTP_COOKIE

('csrftoken=9USN1IsUOkGk8QkCZMer7INL01mElHTi; '
‘sessionid=ekgvrs0avx54lqhgelsv4y0dvala4b0x’)

wsgi.version

(1, 0)

SERVER_PROTOCOL

‘HTTP/1.1’

XDG_CURRENT_DESKTOP

‘XFCE’

_system_name

‘Ubuntu’

XDG_SEAT_PATH

‘/org/freedesktop/DisplayManager/Seat0’


#2

I suspect you have an indentation problem. Can you post your views.py using pastebin.com?


#3

Hi there,
I pasted it into pastebin so you can see my code.
Thank you for your help!

http://pastebin.com/hGaeSj4e


#4

Thanks! Looks like you indeed have some indentation problems — Python is all indentation based so you have to make sure everything is perfect.

I reformatted your code here:

http://pastebin.com/wPWh7gYy

Note that now everything in edit_thing are all indented in. That if request.method part was all the way to the left, which means it’s no longer under your edit_thing view.

Let me know if this fixed your problem!


#5

Hi Tracy, I think I have same issue with indent, as when I click on submit button, nothing happens, even the data is updated which I can see by manually click back to home page and click on each one details. and you link to pastebin desn’t work now, could you please reshare one?
FYI, I bought the kindle version and their layout just prevents us from reading the indent, please take this into consideration for the upcoming intermedia version.
Thanks in advance,
best,


#6

Hi Albert, can you explain more? The submit button where?

“their layout just prevents us from reading the indent” — also this, can you expand more on what you mean by “reading the indent”?

Thanks for the help!


#7

Hi Tracy, above you have a screenshot for code in kindle version, is it supposed to be?
Thanks,
A


#8

I ran code below with two different indents for row with RETURN, both failed, after the input and submit no redirection shew. Can you help take a look and let me know your thought (is there some difference with your published version) ? Thanks!


#9

Oog — I use Leanpub for formatting the kindle book and usually they’re really good about code snippets on Kindle. My apologies — really weird (and frustrating) how they’re wrapping the bits completely incorrectly.

Your second post — the “return redirect…” bit needs to be indented under if form.is(valid):, so:

if form.is_valid():
— form.save()
— return redirect…

When you say they both failed, what was the exact error?


#10

Thanks Tracy, I just bought the epub version on leanpub, the layout looks good, and I copy data from book as below by adapting “thing” with “profile” is this is my case I use in work. but still I got http call 302 instead of 200 ( I think it should be ) and no redirect to individual profile page, but data did saved what I can see by manually click back to home page.


#11

here you have screenshot shows http 302 code. Best,


#12

Thank you for all the screenshots!

I’m really mad — looks like Leanpub formats the EPUB version with really bad code wrapping and ignores me asking for it to make the code bits really small. Looks like I need to use another service to create the epub files. Note that in the EPUB, there are numbers for lines of code:

When Leanpub is wrapping the line, there isn’t a new number (since it’s a continuation).

I need to look into getting a different formatter for the ebooks. @Albert, do you have the PDF? I hand-formatted that so it should be completely correct.


#13

@Tracy, yes, I got the pdf version also from leanpub, and pdf is much better in format. I reviewed my codes in views.py and urls.py but that redirect('profile_detail@,slug=profile.slug) still doesn’t work. below one is from pdf version, those ‘views.’ are ‘collection.views.’ in kindle version, it turns out that kindle version work and pdf version return error page.
P.S. there are difference between pdf/epub and version in kindle for file urls.py
Thanks anyway, I’ll move on regardless this ‘redirect’ issue.
Best,


#14

UPDATE 12/1/2015
It turns out that the redirect issue is caused by online IDE c9.io, I just find that redirect works whe n I test in a specific browser instead of the default one. In case someone else use c9 also, pay attention to the below screenshot.


#15

Thanks for the update!