user5687432
user5687432

Reputation:

Django - Avoid writing duplicate code

I want to create 2 web pages which use the same code but I don't want to copy/paste the code in 2 places. I'm sure I don't need to have the same code in 2 different places to use it on multiple pages. Can someone suggest a way to avoid creating duplicate code?

If required, I can provide some sample code which I've already written.

Upvotes: 2

Views: 286

Answers (2)

AlanK
AlanK

Reputation: 9823

For generating HTML on the frontend, you can use templates to create static content which can be used across multiple pages on your site.

For more information on how to use django templates, see: https://docs.djangoproject.com/en/1.9/topics/templates/

As for the backend, you'll need to write your code as re-usable as possible and then import/reuse as much as you can. Pylint has a check for similar/duplicate code:

For more information on Pylint similarities-checker, see: https://docs.pylint.org/features.html#similarities-checker

Upvotes: 2

Dean Christian Armada
Dean Christian Armada

Reputation: 7364

Use this built-in django template tag for that:

{% include "subtemplate.html" %}

Upvotes: 1

Related Questions