Hi,

On 06/19/2015 08:32 PM, Peith Vergil wrote:
> It depends on how your project files are structured. If you have a file
> structure that looks like this:
> 
> app/
>     __init__.py
>     models.py
>     views.py
>     urls.py
> 
> Then, you can simply use relative imports.
> 
> In views.py:
> from models import *
> 
> In urls.py:
> from views import *

This style of import is called "implicit relative imports", and you
should never use it. It is deprecated in Python 2, and doesn't work at
all in Python 3. (You can opt in to the Python 3 behavior in Python 2
with "from __future__ import absolute_import".)

The reason it is "implicit" is because it is ambiguous - the "models"
and "views" modules you are importing from could be relative to the
current module location, or "absolute" (directly on sys.path), and it
opens the possibility of accidentally shadowing a top-level installed
module (even a stdlib module) with a local module (e.g. if you put a
`datetime.py` or a `django.py` into your package), causing hard-to-debug
issues.

(Also, as has been mentioned, you shouldn't use `import *` because it
leads to ambiguity as to the source of names in your code.)

The right way to do relative imports is with the explicit syntax:

    from .models import MyModel
    from .views import some_view

or to import a whole module:

    from . import models
    from . import views

Using the `.` make it explicit that you are importing from a local
module relative to your current module, not from a top-level module.

Carl

> If, for example, your models live in a separate Django app (i.e. a
> separate Python package):
> 
> app1/
>     __init__.py
>     views.py
>     urls.py
> app2/
>     __init__.py
>     models.py
> 
> Then, you have to import your models using its full path so Python will
> know where to look for it.
> 
> In app1/views.py:
> from app2.models import *
> 
> You should familiarize yourself on how Python imports work since this is
> more of a Python issue rather than a Django issue.
> 
> On Jun 20, 2015 09:10, <jorrit...@gmail.com
> <mailto:jorrit...@gmail.com>> wrote:
> 
>     This is mostly a cosmetic question, and I could be completely wrong
>     because I'm fairly new to Django, or it could be that there is a
>     perfectly logical explanation for this, but here goes:
> 
>     It seems the code required to import views in urls.py and models in
>     views.py is inconsistent (and in the case of urls.py perhaps redudant).
> 
>     To import views in urls.py I have to use
> 
>     |
>     from<appname>importviews
>     |
> 
>     ...while in views.py I can simply write
> 
>     |
>     frommodels import*
>     |
> 
>     Why do I need to reference the appname in urls.py but not in
>     views.py? Or is there a reason for this?
> 
>     -- 
>     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+unsubscr...@googlegroups.com
>     <mailto:django-users+unsubscr...@googlegroups.com>.
>     To post to this group, send email to django-users@googlegroups.com
>     <mailto:django-users@googlegroups.com>.
>     Visit this group at http://groups.google.com/group/django-users.
>     To view this discussion on the web visit
>     
> https://groups.google.com/d/msgid/django-users/03eb4740-6503-4757-ae09-9183ac2b8502%40googlegroups.com
>     
> <https://groups.google.com/d/msgid/django-users/03eb4740-6503-4757-ae09-9183ac2b8502%40googlegroups.com?utm_medium=email&utm_source=footer>.
>     For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> 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+unsubscr...@googlegroups.com
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMHY-KdkaS0RBdS4F4mi%3DaGHTP9uZThJVCesXM9%3DGbVbZoteaw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMHY-KdkaS0RBdS4F4mi%3DaGHTP9uZThJVCesXM9%3DGbVbZoteaw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/55876924.4090409%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to