Please Assist Learning Programming and Python with Django Framework and Stuck

36 views
Skip to first unread message

cmac0tt

unread,
Jun 16, 2012, 6:10:34 AM6/16/12
to django...@googlegroups.com
So python is what I chose to learn, using the Django framework.

I had this project working, then it just stopped out of nowere when I was about to show one of my two bosses. Anyone able to give any insight? I put everything in a DPASTE.

First it kept telling me that line 45 (which didnt exist was "unindented", I did a %retab and fixed it. Not sure how, my .vimrc settings are correct. But after that starting bugging out then it didnt like my final redirect even though it was working before hand. Note there is no other errors. It basically doesnt like my HttpResponseRedirect at the end of the last view (see dpaste)

Remember I am total n00b so snake talk can go over my head sometimes. So if you feel like helping please try to explain why not just what if that makes sense (if you dont mind). I would appreciate it greatly!

here is the dpaste:

http://dpaste.com/760213/

cmac0tt

unread,
Jun 16, 2012, 6:23:11 AM6/16/12
to django...@googlegroups.com
P.S Python 2.7 with Django 1.4 ON Ubuntu 12.04lts

kenneth gonsalves

unread,
Jun 16, 2012, 6:22:02 AM6/16/12
to django...@googlegroups.com
On Sat, 2012-06-16 at 03:10 -0700, cmac0tt wrote:
> here is the dpaste:
>
> http://dpaste.com/760213/
>
>

what does write() do?

btw, line 56 in models needs to be indented
lines 54 and 55 have a double indent. Why?
lines 21, 22 and 32 will never be executed.
--
regards
Kenneth Gonsalves

cmac0tt

unread,
Jun 16, 2012, 6:26:42 AM6/16/12
to django...@googlegroups.com
I was told by someone I had to use print() then write() then save.page to pass the data permanently as opposed to just printing the data and losing it. It didnt logically make sense to me, which is why I kind was hoping people could explain their answers a little if possible rather than just saying "do this"/

I dont mean that to sound the way it does, I am reading much of the documentation and 3 books while I learn. I just find I've been lead astray a few times and that may be one of them if you're questioning it as even I was.


On Saturday, June 16, 2012 6:10:34 AM UTC-4, cmac0tt wrote:

kenneth gonsalves

unread,
Jun 16, 2012, 6:27:04 AM6/16/12
to django...@googlegroups.com
On Sat, 2012-06-16 at 15:52 +0530, kenneth gonsalves wrote:
> >
>
> what does write() do?
>
> btw, line 56 in models needs to be indented
> lines 54 and 55 have a double indent. Why?
> lines 21, 22 and 32 will never be executed.

oh, and your indents in 17 and 18 are not consistent with 21-22. A good
practice in python is to have *all* indents 4 spaces wide.

oops - you have a try statement in line 37, but no except statement.
--
regards
Kenneth Gonsalves

kenneth gonsalves

unread,
Jun 16, 2012, 6:32:28 AM6/16/12
to django...@googlegroups.com
On Sat, 2012-06-16 at 03:26 -0700, cmac0tt wrote:
> I was told by someone I had to use print() then write() then save.page
> to
> pass the data permanently as opposed to just printing the data and
> losing
> it. It didnt logically make sense to me, which is why I kind was
> hoping
> people could explain their answers a little if possible rather than
> just
> saying "do this"/

was the gentleman who told you that a python programmer? A print
statement just sends something to the console and is usually used to
debug. You have an empty print statement which will just print '()' on
the console. I have never heard of the write() statement as a python
builtin. Of course there is a whole lot of python I do not know, so I
may be wrong.
--
regards
Kenneth Gonsalves

Cal Leeming [Simplicity Media Ltd]

unread,
Jun 16, 2012, 10:01:53 AM6/16/12
to django...@googlegroups.com
Just to echo on advice already given, your indenting is a real mess here.

You can't get away with bad indenting like you can in other languages, because Python relies on indenting for the flow of code. (i.e. you use intends rather than brackets).

You are also using mixed indents (some are 4 spaces, some are 8, some are 1), you need to make them all consistent. Personally, I use 4 space indent without tabs.

Get the indents right, and the rest will follow :)

Cal


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/K4uyV6l6mz8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

yak-man

unread,
Jun 16, 2012, 11:46:57 AM6/16/12
to django...@googlegroups.com
Line 44 is an error:  unless you've defined it (you didn't), there is no "write()" - it seems like you took someone's comment too literally:  I imagine something like this was said "To save a wiki page, you have to ... um.... save it somehow, you know - print(), or write(), or something!" --- and you put print(), and write() in your code, without understanding why you were doing that.

So - contents of a wikipage which your program is enabling someone to create, must somehow persist between visits.

This can happen any number of ways, and you (basically) choose / decide / design how you will to this.

You can store the contents of the page in:
  • a database entry;
  • a file
  • ...<you fill in the other possibilities, if you like>...
The "magic" you are not grasping is that, in Django,   models are Python abstractions (representations) of a database table.  You (thankfully) don't have to worry about SQL, or creating a database - just interact with the model, and all the stuff with the backend (be it sqlite, or some other database) is magically translated and done for you.

SO - since you are using django models, you are storing to a database.   

That means you don't need  to use "print" to put some content in some file (you didn't even connect the content, nor the output destination in your print statement, so that would have been missing anyway);

That means you don't need to use a file write() method (you didn't have it associated with a file handle, nor with any content to write, so that wouldn't work as written anyway - see http://docs.python.org/library/stdtypes.html?highlight=write#file.write);

All you need is the model's instance ("page" in your case), and use it's save() method (not unlike you might use a file's  write() method) --- see https://docs.djangoproject.com/en/dev/intro/tutorial01/#playing-with-the-api

As for try:/except: --- they need to be in pairs.   You put this around something which you think might sometimes fail (like show a page, but the page doesn't exist yet).   You try; if anything indented under the "try" fails, the "except" line lists which error names it tries to recover from (that is, you can have more than one except, each trying to recover from a certain kind of error).   In the case of view_page(),  you do a render to "create" (did you want a redirect instead?).   BTW, the last two lines under except need to be out-dented (lines 21-22) - when they are, you will see that you have done "content = page.content" twice in a row: once under the "try" clause, and once again, right after the "try" clause (that is, right after it just succeeded).   Pick a place to put this, and have it in one place (I would do it outside the "try").

Hopefully, this gives you a start at understanding what you have, what you are trying to do.

Regards,
Yarko

Reply all
Reply to author
Forward
0 new messages