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