I wonder if the terms in the title are correct. .. .. .. .. ..
A memorandum for myself
When creating a program that uses Django To be convenient when the scale of the application grows Organize the procedure for splitting modules such as views and models into multiple files.
The environment is as follows
Ubuntu 16.04
python 3.5.2
django 1.10.5
Here, let's divide models.py into multiple files.
initial state
__init__.py
admin.py
apps.py
migrations
models.py
tests.py
views.py
Here, modify models.py so that 1 file = 1 model class.
With the models directory__init__.Prepare py file
__init__.py
admin.py
apps.py
migrations
models
__init__.py
task.py
tests.py
views.py
Delete models.py and prepare the models directory as shown in the code above. A file that describes the init.py__ file and the model class in it. Create (here, task.py).
task.py
from django.db import models
class Task(models.Model):
"""
Task to do.
"""
name = models.CharField(max_length=30)
startTime = models.DateTimeField()
endTime = models.DateTimeField()
memo = models.CharField(max。_length=200)
__init__py file
from webui.models.task import Task #Import the class created above
The file division method is the same.
File split
__init__.py
admin.py
apps.py
migrations
models
__init__.py
task.py
tests.py
views
__init__.py
task_view.py
The view module needs to be described in urls.py
Split views into multiple files If you are doing like
urls.py(Method based)
urlpatterns = [
...,
url(r'^Appropriate regular expression',Application name.views.file name.Method name)
]
The method to specify from ... import is also in the link mentioned at the beginning of this section. Although it is described, the same method name cannot be specified, so the above code example I think it's better to use ** maybe **.
In the case of a class unit, as shown in Make Django views.py a class It becomes as follows.
urls.py(Class based)
urlpatterns = [
...
url(r'^/URL regular expression/$',Application name.views.view class name.as_view()),
...
]
By declaring from ... import as well as method-based You shouldn't need to write the package name.
Make Django views.py a class Split views into multiple files
Recommended Posts