Django REST & Admin with a nice theme in 60 seconds

This is a next article from “Django in 60 seconds” series.

In the previous article we created a simple Django application. That is able to do some simple things. This time we will add REST API and administration panel.

REST Api

Last time we left on this col file:

[index: /]
@crud(#cat)

#cat
-----------
=name
age: int

Let’s add some api here:

[index: /]
@crud(#cat)

#cat
-----------
=name
age: int

@rest
@api

After that change, Zmei generator will detect the change happened and restart/regenerate Django code. Or if you stopped the app, then just go to the project folder, reactivate virtualenvironment and start again:

$ workon cats  # this line assumes you are using virtualenvwrapper

$ zmei gen up

And if you go to the http://127.0.0.1:8000/api/ url, you will see our API:

../_images/api.png

Admin

Now it’s time to add admin part to our application:

[index: /]
@crud(#cat)

#cat
-----------
=name
age: int

@rest
@api

@admin(list: *)

Zmei will pick up our changes and generate admin part http://127.0.0.1:8000/admin/:

../_images/admin.png

But in order to log in there, you need to create a superuser. Hit Ctrl (or Cmd) + C to stop Zmei and run:

$ python manage.py createsuperuser

After that you can log in to the admin panel:

../_images/admin2.png

Let’s make it a bit nicer:

@suit("My cats admin")

[index: /]
@crud(#cat)

#cat
-----------
=name
age: int

@rest
@api

@admin(list: *)

Just wait until Zmei will install dependency and restart the app:

../_images/admin3.png

Nice! Now we have much nicer admin-side.

Note

Django-suit is paid for commercial use. See http://djangosuit.com/pricing/

Polishing

And final steps. Our frontend crud allows to add cats and even delete. Let’s leave the ability to add cats by anyone, and remove edit/delete view, so nobody can delete or edit other’s cats:

@suit("My cats admin")

[index: /]
@crud(
    #cat,
    skip: edit, delete
)

#cat
-----------
=name
age: int

@rest
@api

@admin(list: *)

Here it is:

../_images/crud.png

… to be continued

Next time we will learn how to deploy the application to Zmei Apps so our cat-manager can see the entire world… Stay tuned ;)

Comments

comments powered by Disqus