Django 1.4 new project folder structure forces project prefixes?

后端 未结 2 1027
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 14:13

Using django 1.4 and have seen that when you use startproject it now creates a folder within your project with the same name.

-myproject/
               


        
相关标签:
2条回答
  • 2020-12-24 14:38

    I would just add that it forces you to use prefixes when you accces your main myproject.urls, but it doesn't force you either way for your apps. You can choose to store apps either in the top-level folder:

    myproject
    |-- manage.py
    |-- myproject
    |   |-- __init__.py
    |   |-- settings.py
    |   |-- urls.py
    |   `-- wsgi.py
    `-- polls
        |-- __init__.py
        |-- models.py
        |-- tests.py
        `-- views.py
    

    This is the default when you use python manage.py startapp polls In this case you'd use from polls.models import Whatever

    Alternatively, you can:

    mkdir myproject/polls
    python manage.py startapp polls myproject/polls
    

    And you'll get this:

    myproject
    |-- manage.py
    `-- myproject
        |-- __init__.py
        |-- polls
        |   |-- __init__.py
        |   |-- models.py
        |   |-- tests.py
        |   `-- views.py
        |-- settings.py
        |-- urls.py
        `-- wsgi.py
    

    In which case you'll have to from myproject.polls.models import Whatever...

    So the former is better for apps you think you might be able to re-use in other projects, and the latter is better for apps that are tightly coupled with other parts of your project.

    0 讨论(0)
  • 2020-12-24 14:54

    Yes, prefix ROOT_URLCONF with your project name:

    ROOT_URLCONF = 'myproject.urls'
    

    You shouldn't import settings directly anyway (see Using settings in Python code). Instead, use the following, which works for both the old and new project layouts.

    from django.conf import settings
    
    0 讨论(0)
提交回复
热议问题