[PYTHON] Configure a module with multiple files in Django

I wonder if the terms in the title are correct. .. .. .. .. ..

A memorandum for myself

Purpose

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.

Premise

The environment is as follows

Ubuntu 16.04
python 3.5.2
django 1.10.5

procedure

Here, let's divide models.py into multiple files.

Split model 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

Split view into multiple files

The file division method is the same.

  1. Delete views.py
  2. Create a views directory
  3. Create an appropriate file under the views directory
  4. Create views / __init __.py file
  5. Import the classes and methods in the file created in 3. with the __init __.py file

File split


__init__.py
admin.py
apps.py
migrations
models
    __init__.py
    task.py
tests.py
views
    __init__.py
    task_view.py

Description in urls.py

The view module needs to be described in urls.py

If you are defining a view on a method basis

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 **.

If you are defining a view on a class basis

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.

Reference material

Make Django views.py a class Split views into multiple files

Recommended Posts

Configure a module with multiple files in Django
[Python] Get the files in a folder with Python
Save multiple models in one form with Django
Build a Django environment with Vagrant in 5 minutes
How to reference static files in a Django project
Create a homepage with django
Upload multiple files in Flask
Try running python in a Django environment created with pipenv
[Django] Manage settings like writing in settings.py with a model
[Linux] Grep multiple gzip files in a directory at once
Until I return something with a line bot in Django!
reload in django shell with ipython
Enumerate files with a specific extension
Handle csv files with Django (django-import-export)
Load multiple JavaScript files with PyWebView
Manage Django config files with Python-decouple
Deploy a Django application with Docker
Django Tips-Create a ranking site with Django-
Build a web application with Django
Make a filter with a django template
GraphQL API with graphene_django in Django
Create a file uploader with Django
Create a LINE Bot in Django
Get a list of files in a folder with python without a path
Install Django in a pipenv virtual environment
Generate all files with a specific extension
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
A simple RSS reader made with Django
Handle multiple Django projects with Apache (WSGIDaemonProcess)
Draw a heart in Ruby with PyCall
Notes on creating static files in Django
[Can be done in 10 minutes] Create a local website quickly with Django
Creating a login screen with Django allauth
Dynamically specify a ModelChoiceField queryset in Django
Process multiple lists with for in Python
Performance comparison of Parquet, Vaex, Dask, etc. in HDF5 with multiple files
A note on enabling PostgreSQL with Django
Create a record with attachments in KINTONE using the Python requests module
Implement a Custom User Model in Django
I made a WEB application with Django
I wanted to know the number of lines in multiple files, so I tried to get it with a command
Send an email with a user other than EMAIL_HOST_USER written in settings in django
I want to see a list of WebDAV files in the Requests module
Turn multiple lists with a for statement at the same time in Python
How to get a list of files in the same directory with python
Error when installing a module with Python pip
Draw a graph with Japanese labels in Jupyter
Delete data in a pattern with Redis Cluster
How to develop a cart app with Django
Create a virtual environment with conda in Python
Enable Django https in just a few lines
Recursively search all files with multiple specified extensions
Create a dashboard for Network devices with Django!
Clone with a specific branch / tag in GitPython
Handle zip files with Japanese filenames in Python 3
Easily log in to AWS with multiple accounts
A story about implementing a login screen with django
Character encoding when dealing with files in Python 3
Build a Django development environment with Doker Toolbox
To add a module to python put in Julialang
Work in a virtual environment with Python virtualenv.