[PYTHON] Implement a Django app on Hy

This article is like LISP, but it is the 5th day article of Python Part 2 Advent Calendar 2015.

There is a sentence "Django + LISP" in the README of Hy. The source code shows the concept that Django apps can be implemented in Hy as well, but I don't know what to do with this alone, so I will explain step by step from the point of start project.

I made a repository on github and pushed the commit separately for each step, so please refer to it.

start project

For now, use plain Python for django-admin.py start project. The version of Django I'm using is 1.9.

django-admin.py startproject hellohy

This is the commit at the stage of start project.

Rewrite * .py to * .hy

All programming languages lead to LISP, so I would like to eliminate Python code as much as possible and replace it with Hy.

This is the commit that rewrote the code immediately after creating the project to Hy.

There are two points that I stumbled upon a little.

--When I replaced manage.py with Hy, hy manage.hy runserver didn't work, so I gave up on this file. --It seems that you need to put ʻimport hy in your startup script (manage.py` in this case) to define a module with Hy. --Since single quotes have a special meaning in Hy (LISP), I changed all strings to double quotes.

See here for how to convert Hy in Python's ʻimport x, from y import z`. It's obvious at a glance.

At this stage, even if you delete all the py files except manage.py, the python manage.py runserver will work and you can actually access it in your browser.

Create ʻapp_template`

After startproject, adding a Django application module with startapp is a standard Django flow, but at this stage I knew that startapp would also add a Python module, so Add the application template first.

That is this commit.

Use this ʻapp_template to run startapp`.

python manage.py startapp --template=hellohy/app_template -ehy myapp

The point is

--Specify your own application template directory with --template. --Add the extension of the file to be treated as a template with -e.

That is.

Here is a commit that created an application called myapp with the above command and added a view that displays a very simple template called myapp.views.top.

Define a model

Hy Tutorial has Django model-like sample code, but at this stage You can see that it's not pseudo-code, it's actually working code.

Here is a commit that adds a model to display the data in the database.

The definition of the model is as follows.

(import [django.db [models]]
        [django.utils.timezone :as timezone])

(defclass Topic [models.Model]
  [title (models.TextField)
   url (models.URLField)
   created_at (models.DateTimeField :default timezone.now)])

Think of a Topic as an object that represents the URL of the source for some news title. The important points are as follows.

--def class uses the latest development version of Hy syntax. --Class properties are given as a list in the third argument of the def class macro --The Python keyword argument models.DateTimeField (default = timezone.now) is expressed as (models.DateTimeField: default timezone.now).

After defining the model, the procedure for make migrations, migrate is the same as for regular Django.

Implement the view

This is an example of getting multiple Topic objects defined above and rendering the template.

(import [django.shortcuts [render]]
        [myapp.models [Topic]])

(defn top [req]
  (def topics (-> (Topic.objects.all)
                  (.order_by "-id")))
  (render req "top.html" {"topics" topics}))

An example of getting an object with a primary key and rendering a template.

(import [django.shortcuts [render]]
        [django.http [Http404]]
        [myapp.models [Topic]])

(defn topic_detial [req topic_id]
  (def topic (try
              (Topic.objects.get :pk topic_id)
              (except [e Topic.DoesNotExist](raise Http404))))
  (render req "topics/topic_detail.html" {"topic" topic}))

I have implemented it up to the point of adding a list and detail view with the following commit.

Unit test

I think one of the benefits of using Django is that it's easier to unit test, so let's add unit tests this time as well.

That is this commit.

Actually, I wish I could run the unit test only with this tests.hy, but since the test runner did not find .hy, it feels like a trick, but the sky with the same name The .py file of is added.

I'd like to do something about this trick, but I've passed the test for the time being.

$ python manage.py test myapp
Creating test database for alias 'default'...
....
----------------------------------------------------------------------
Ran 4 tests in 0.051s

OK
Destroying test database for alias 'default'...

Finally

I introduced the procedure to implement the Django app on Hyde.

If you try it yourself, you'll understand that Hy can bring out the full power of Django, so you can feel that all programming languages reach LISP.

Next time, I would like to explore the possibilities of Hy by deploying the Django app written in Hy to the real environment.

Recommended Posts

Implement a Django app on Hy
Deploy a Python 3.6 / Django / Postgres web app on Azure
Deploy a Django application on Google App Engine (Python3)
Deploy a Django app made with PTVS on Azure
How to deploy a Django app on heroku in just 5 minutes
Deploy the Django app on Heroku [Part 2]
Django: Implement reusable APP settings following Django RestFrameWork
A note on enabling PostgreSQL with Django
Implement a Custom User Model in Django
Create a Todo app with Django ④ Implement folder and task creation functions
Build a Django environment on Raspberry Pi (MySQL)
How to develop a cart app with Django
Until you create a new app in Django
I want to upload a Django app to heroku
Create a Django schedule
Create a Todo app with Django REST Framework + Angular
Launch my Django app
Build a Django development environment using pyenv-virtualenv on Mac
Looking back on creating a web service with Django 1
What is a dog? Django App Creation Start Volume--startapp
Initialize your Django app
Celery notes on Django
Run Django on PythonAnywhere
Create a Todo app with the Django REST framework
Publish your Django app on Amazon Linux + Apache + mod_wsgi
How to build a Django (python) environment on docker
Try running a Django application on an nginx unit
Steps from installing Python 3 to creating a Django app
Create a Todo app with Django ③ Create a task list page
Looking back on creating a web service with Django 2
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
What is a dog? Django App Creation Start Volume--startproject
Make the model a string on a Django HTML template
Hello World on Django
How to use Django on Google App Engine / Python
Launch Django on a Docker container with docker-compose up
Miscellaneous notes about deploying the django app on Heroku
To myself as a Django beginner (1) --Create a project app--
[kotlin] Create a real-time image recognition app on android
A memo when Django was released on VPS (preparation)
Start a Django project
How to run Django on IIS on a Windows server
To myself as a Django beginner (4) --Create a memo app--
Create a Todo app with Django ⑤ Create a task editing function
Create a shogi game record management app using Django 4 ~ Create View ~
A quick look at your profile within the django app
A solution when you can't start project Django on Windows
(Failure) Deploy a web app made with Flask on heroku
Build a bulletin board app from scratch with Django. (Part 2)
Build a bulletin board app from scratch with Django. (Part 3)
Create a Todo app with Django ① Build an environment with Docker
Django --Overview the tutorial app on Qiita and add features (2)
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Implement a 3-layer neural network
Implement follow functionality in Django
A comment on Boruta algorithm
Create a homepage with django
[Django] Notes on using django-debug-toolbar
Deploy masonite app on Heroku 2020
[Django] Make a pull-down menu
Django environment development on Windows 10