Am I stupid or is there an essential error in Django 1.10 Docs?

157 views
Skip to first unread message

Peter Müller

unread,
Jan 15, 2017, 1:03:54 PM1/15/17
to Django users
Hello everybody.


I just began learning how to use django. So I wanted to include data from the database in a testwebsite.
Therefore I made a new app and created a model called "user" and a view that should fill a template with data from that model.
To do this (according to Django 1.10 Docs/Tutorial) I need to import the model class into the view file and for example use models.user.all().
However on import of the view Django complains about
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

What is the solution to that problem?

Greetings
Peter

Mike Dewhirst

unread,
Jan 15, 2017, 4:11:30 PM1/15/17
to django...@googlegroups.com
On 16/01/2017 5:01 AM, 'Peter Müller' via Django users wrote:
> Hello everybody.
>
>
> I just began learning how to use django. So I wanted to include data
> from the database in a testwebsite.
> Therefore I made a new app and created a model called "user" and

You don't need to create your own user model. There's one in the box ...

from django.contrib.auth.models import User

Try using that and see if your error goes away. Also, you should work
through the tutorial to get a handle on how it is used ...

https://docs.djangoproject.com/en/1.10/intro/tutorial01/

As you are a Django beginner I assume you will try one or two small
projects before tackling a major one so just going with the standard
User model should be no impediment in the early stages. Then maybe look
through the docs to see how to customise/replace that User model.

Cheers

Mike

> a view that should fill a template with data from that model.
> To do this (according to Django 1.10 Docs/Tutorial) I need to import
> the model class into the view file and for example use models.user.all().
> However on import of the view Django complains about
> |
> django.core.exceptions.AppRegistryNotReady:Appsaren't loaded yet
> |
>
> What is the solution to that problem?
>
> Greetings
> Peter
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/31442c04-4da4-496b-8611-9d7b70e36138%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/31442c04-4da4-496b-8611-9d7b70e36138%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Daniel Roseman

unread,
Jan 16, 2017, 9:53:42 AM1/16/17
to Django users
There is no possible way to answer this question without seeing the view itself, and the full error traceback.
-- 
DR. 

Peter Müller

unread,
Jan 16, 2017, 10:49:00 AM1/16/17
to Django users
Thank you for your help. Even though I understand the obvious reason to use the in-built User model I prefer to make one myself.
Not because the integrated one is bad. It is just that it adds huge amounts of boilerplate and hidden django magic.
And since users are basically just a name and an identifier and present everywhere on the internet I chose to use them to learn how to interface DBs with Django.

Also I used the tutorial just that I abstracted the concept since I think the tutorial is more than bad.
In addition this method teaches on what essentially I am doing else then to blindly copying code.


Thanks

Peter

Peter Müller

unread,
Jan 16, 2017, 10:53:56 AM1/16/17
to Django users
For the traceback: http://pastebin.com/Wy7And6M
And the views configuration: http://pastebin.com/q0V3bQay


thanks

Peter

Daniel Roseman

unread,
Jan 16, 2017, 3:20:53 PM1/16/17
to Django users
On Monday, 16 January 2017 15:53:56 UTC, Peter Müller wrote:
For the traceback: http://pastebin.com/Wy7And6M
And the views configuration: http://pastebin.com/q0V3bQay


thanks

Peter


The traceback shows that you are importing your views module in the __init__.py of your app. Don't do that. 
-- 
DR.

Peter Müller

unread,
Jan 16, 2017, 3:23:09 PM1/16/17
to Django users
Well else python doesn't find the view.py file?

Andreas Kuhne

unread,
Jan 16, 2017, 4:24:30 PM1/16/17
to django...@googlegroups.com
What does the project structure look like?

Is Stranvr the name of the project?

It looks to me like you haven't created an app - or have you called your app "db_testing" and the project "Stranvr"?

Because you first have to create you project with "django-admin.py startproject <project_name>" and then within that created directory run either "./manage.py startapp <app_name>" or "django-admin.py startapp <app_name>".

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Peter Müller

unread,
Jan 16, 2017, 4:29:38 PM1/16/17
to Django users
Stranvr ist the project made with startproject and db_testing is an app made with startapp.

So:
ProjectRoot
-StranvrProject
--urls.py
--settings.py
--wsgi.py
-db_testing
--views.py
--__init__.py
--models.py
--admin.py
--apps.py


also the app is added to settings.py


Am Montag, 16. Januar 2017 22:24:30 UTC+1 schrieb Andréas Kühne:
What does the project structure look like?

Is Stranvr the name of the project?

It looks to me like you haven't created an app - or have you called your app "db_testing" and the project "Stranvr"?

Because you first have to create you project with "django-admin.py startproject <project_name>" and then within that created directory run either "./manage.py startapp <app_name>" or "django-admin.py startapp <app_name>".

Regards,

Andréas

2017-01-16 21:23 GMT+01:00 'Peter Müller' via Django users <django...@googlegroups.com>:
Well else python doesn't find the view.py file?

Am Montag, 16. Januar 2017 21:20:53 UTC+1 schrieb Daniel Roseman:
On Monday, 16 January 2017 15:53:56 UTC, Peter Müller wrote:
For the traceback: http://pastebin.com/Wy7And6M
And the views configuration: http://pastebin.com/q0V3bQay


thanks

Peter


The traceback shows that you are importing your views module in the __init__.py of your app. Don't do that. 
-- 
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Matthew Pava

unread,
Jan 16, 2017, 4:33:57 PM1/16/17
to django...@googlegroups.com

You import views in your urls.py file.  Your settings.py file has a constant pointing to where the urls.py file is located for the project.  Your urls.py file is the one that actually utilizes the views file.

--

Peter Müller

unread,
Jan 16, 2017, 4:35:36 PM1/16/17
to Django users, Matthe...@iss.com
Yes


Am Montag, 16. Januar 2017 22:33:57 UTC+1 schrieb Matthew Pava:

You import views in your urls.py file.  Your settings.py file has a constant pointing to where the urls.py file is located for the project.  Your urls.py file is the one that actually utilizes the views file.

 

From: 'Peter Müller' via Django users [mailto:django...@googlegroups.com]
Sent: Monday, January 16, 2017 2:23 PM
To: Django users
Subject: Re: Am I stupid or is there an essential error in Django 1.10 Docs?

 

Well else python doesn't find the view.py file?

Am Montag, 16. Januar 2017 21:20:53 UTC+1 schrieb Daniel Roseman:

On Monday, 16 January 2017 15:53:56 UTC, Peter Müller wrote:

For the traceback: http://pastebin.com/Wy7And6M
And the views configuration: http://pastebin.com/q0V3bQay


thanks

Peter

 

 

The traceback shows that you are importing your views module in the __init__.py of your app. Don't do that. 

-- 

DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to djang...@googlegroups.com.

Michal Petrucha

unread,
Jan 17, 2017, 2:01:41 AM1/17/17
to Django users
On Mon, Jan 16, 2017 at 12:23:09PM -0800, 'Peter Müller' via Django users wrote:
> Am Montag, 16. Januar 2017 21:20:53 UTC+1 schrieb Daniel Roseman:
> > The traceback shows that you are importing your views module in the
> > __init__.py of your app. Don't do that.
>
> Well else python doesn't find the view.py file?

Python will be able to import db_testing.views just fine even if you
don't import viewsi from . in db_testing/__init__.py. All Python cares
about is that there is a correct Python file in the correct location.

If you're interested in more details about what's going wrong here,
there's an explanation here:
https://docs.djangoproject.com/en/1.10/ref/applications/#how-applications-are-loaded

In particular, the following paragraphs:

> At this stage, your code shouldn’t import any models!
>
> In other words, your applications’ root packages and the modules
> that define your application configuration classes shouldn’t import
> any models, even indirectly.

What's happening here is that the root of your “db_testing” package
(i.e. db_testing/__init__.py) indirectly imports models, because it
imports the views module, which in turn imports models.

Just removing the “from . import views” line should let you move
forward – unless you also have other imports in the __init__.py file,
in which case you can most likely just remove them, too. In general,
in an average Django package, there's little reason to import stuff in
its root package.

Good luck,

Michal
signature.asc

Peter Müller

unread,
Jan 17, 2017, 4:34:43 PM1/17/17
to Django users
Ah I now see the problem. However I tried to remove the entry in the __init__.py file
Python is then unable to find any module in that directory. Don't ask me why that is.

Also I noticed that django has its completely own import ecosystem.
So I used django.conf.urls.include to include a new urls file in that sub directory.
And suddenly everything works fine.

Thanks everyone

Vijay Khemlani

unread,
Jan 17, 2017, 5:05:10 PM1/17/17
to django...@googlegroups.com
The only "custom include" in Django is for loading other url files

The rest of the imports are typical Python module / packages

On 1/17/17, 'Peter Müller' via Django users
<django...@googlegroups.com> wrote:
> Ah I now see the problem. However I tried to remove the entry in the
> __init__.py file
> Python is then unable to find *any *module in that directory. Don't ask me
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b1760656-c6a0-40c5-a002-19958e5706ef%40googlegroups.com.

Peter Müller

unread,
Jan 27, 2017, 11:03:20 AM1/27/17
to Django users
But if I do a direct import via python's import statement. it will execute the content of the file before loading apps and that raises that error.
No matter what you import you have to use django's import system. Also it just works fine now.

Melvyn Sopacua

unread,
Jan 27, 2017, 12:32:23 PM1/27/17
to django...@googlegroups.com

On Monday 16 January 2017 07:49:00 'Peter Müller' via Django users wrote:

 

> Also I used the tutorial just that I abstracted the concept since I

> think the tutorial is more than bad.

 

Awaiting your PRs.

--

Melvyn Sopacua

Peter Müller

unread,
Jan 27, 2017, 1:27:47 PM1/27/17
to Django users
What do you mean by PR?

Melvyn Sopacua

unread,
Jan 27, 2017, 2:50:39 PM1/27/17
to django...@googlegroups.com

https://github.com/django/django/pulls

 

The tutorial has a very logical order, going from the database abstraction up to the template layer and beyond. But feel free to rewrite it and submit it to the project.

 

I never follow tutorials to the letter - for example back when I did it in Django 1.3, my polls app had an ip-based rate limiter tucked onto existing code - but it certainly is one of the top tutorials for a framework I've seen.

> > Melvyn Sopacua

 

--

Melvyn Sopacua

Peter Müller

unread,
Jan 28, 2017, 10:14:23 AM1/28/17
to Django users

If I could I would gladly rewrite the tutorial. But sadly I do not have enough time to do so.
However I think it is bad since it not only completely disregards the zen of py but also its own principles (DRY etc.) If you need concrete examples I can name you tons.
A quick one is e.g. that there is no explanation of what a view is and what its purpose is.
Also there is literally in part three: "But, don’t do that. It’s silly.". If I should not do it then do not mention in a beginners tutorial…
Another example here is the "shortcut" render() function. It is a beginners tutorial so I do not expect an in-depth explanation of any way possible but only the easiest one.
For a beginner it would be much more easy to simply user render() from the beginning on and later show cases where render() is not sufficient to do the job.
By far the best tutorial I yet had was of pygame. One well documented example and the docs were enough to teach me how it works.

On a side-note (this is only personal preference):
I do not like that the tutorial is part of the documentation.
A tutorial should never be a documentation. A documentation should capture anything you can do with the framework.
However a tutorial should only show you one way (and also the easiest) of doing of one certain task.

Vijay Khemlani

unread,
Jan 28, 2017, 10:31:04 AM1/28/17
to django...@googlegroups.com
> A quick one is e.g. that there is no explanation of what a view is and what
>
> its purpose is.

"The code above maps URLs, as simple regular expressions, to the
location of Python callback functions (“views”)"

"Each view is responsible for doing one of two things: Returning an
HttpResponse object containing the content for the requested page, or
raising an exception such as Http404. The rest is up to you."

> For a beginner it would be much more easy to simply user render() from the
> beginning on and later show cases where render() is not sufficient to do
> the job.

As a matter of fact it is easier to use Class based views usually, so
calling render explicitly is not that common, but function based views
are easier to understand initially.

> If I could I would gladly rewrite the tutorial. But sadly I do not have
> enough time to do so.

Maybe... it's a good thing you don't have time really

On 1/28/17, 'Peter Müller' via Django users
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6c24f788-7a60-4118-96d7-58c52376bb30%40googlegroups.com.

Peter Müller

unread,
Jan 28, 2017, 11:15:35 AM1/28/17
to Django users
I do not think that


"Each view is responsible for doing one of two things: Returning an
HttpResponse object containing the content for the requested page, or
raising an exception such as Http404. The rest is up to you."

is easy for a beginner to understand. Also the tutorial directly asks me to copy and paste the code. And I think everybody agrees that copy pasting is teaching almost nothing of value.
 I think an explanation like:

A view processes a request and returns a Response such as a webpage.

is much rather fitting. Also the explanation should be given while the first view is written.
However I still get what the tutorial is trying to achieve:
To create a working project and then explain afterwards what each part does.
But then why not deliver a zip-file with all code already written?



As a matter of fact it is easier to use Class based views usually, so
calling render explicitly is not that common, but function based views
are easier to understand initially.

I agree that this is the case. However my point is that the tutorial should not show every possible way to make a view.
Rather than just one way and stick to it.


To make my argument more short and concise:
The tutorial lacks simplicity and tries to show everything you could possibly imagine doing with django while not managing to transmit the most basic usage.
Reply all
Reply to author
Forward
0 new messages