Write Preview¶
Preview Structure¶
├── previews
│ ├── hello_preview.py # single preview file can work
│ ├── components # putting multiple previews in a directory can also work
│ │ ├── alert_preview.py
│ │ ├── modal_preview.py
Each preview file can contain multiple preview classes.
Each preview class should inherit from
ViewComponentPreviewand have at least one public method to render the preview.
Use Cases¶
Writing preview is very simple, just generate HTML according to the parameters (we can get parameters from the kwargs of the preview method)
We can use preview to help us to develop and organize:
Useful HTML snippets
HTML component code, like
alert,modal,card, etc. (You can take a look at django-viewcomponent)Some landing pages
Responsive HTML email (So we do not need to send email to check the result each time)
Isolated Environment¶
The preview give developer an isolated environment, so please feel free to play with it without worrying about breaking the production code, and you do not need to touch urls.py, views.py anymore.
Fake Data¶
We can use factory_boy or model_bakery to generate fake data
We can use
unittest.mockto generate fake dataWe can also use
from django.test import RequestFactoryto help simulate a request with special attributes.
All this can be done in Python code, without creating extra fixture files.