apt-get install git python-pip python3-pip
git clone https://github.com/codenerix/django-codenerix-examples
cd django-codenerix-examples/agenda/
For python 2: sudo pip2 install -r requirements.txt
For python 3: sudo pip3 install -r requirements.txt
In python 2: python2 manage.py runserver
In python 3: python3 manage.py runserver
From here we will be improving the basic example with new models, views, etc...
First step it’s to create a project and configure its settings.
Follow this guide to set up a project from begin. We assume that you have sucessfully run the Quickstart.
django-admin startproject librarymanager
./manage.py startapp library
# Installed apps
INSTALLED_APPS = [
...
'library', # Our app
...
]
Following this step, you will have a basic Codenerix project ready to run with your new ‘library’ app.
from codenerix.models import CodenerixModel
from django.db import models
class Author(CodenerixModel):
name = models.CharField(_(u'Name'), max_length=128, blank=False, null=False)
birth_date = models.CharField(_(u'Fecha de nacimiento'), max_length=128, blank=False, null=False)
def __fields__(self, info):
fields=[]
fields.append(('name', _('Name'), 100, 'left'))
fields.append(('birth_date', _('Birth Date')))
return fields
class Book(CodenerixModel):
name = models.CharField(_(u'Name'), max_length=128, blank=False, null=False)
author = models.ForeignKey(Author, max_length=128, blank=False, null=False)
isbn = models.CharField(_(u'ISBN'), max_length=128, blank=False, null=False)
def __fields__(self,info):
fields = []
fields.append(('name', _('Name'), 100, 'left'))
fields.append(('isbn', _('ISBN')))
fields.append(('author', _('Author')))
return fields
The first step to start coding a Django project is create data models. In this case we will make two Models as showed above, Author and Book, both inheriting from the base class CodenerixModel. The structure is simple, we declare all fields (as in Django) and then the method __fields__. This method is mandatory because is needed by Codenerix to define which fields are shown and in which order.
from codenerix.forms import GenModelForm
class BookForm(GenModelForm):
model = Book
exclude = []
def __groups__(self):
groups = [(_('Book'), 12, ['name', 6], ['isbn', 6], ['author', 3])]
return groups
@staticmethod
def __groups_details__():
details = [(_('Book'), 12, ['name' , 6], ['isbn', 6], ['author', 3])]
return details
class AuthorForm(GenModelForm):
model = Author
exclude = []
def __groups__(self):
groups = [(_('Author'), 12, ['name', 6], ['birth_date', 6])]
return groups
@staticmethod
def __groups_details__():
details = [(_('Author'), 12, ['name', 6], ['birth_date', 6])]
return details
The second step is to create a form. In our example we are creating two forms, one for the Book model and another for the Author model. In addition, both forms have implemented the static method __groups_details__. This method is important because we will use them in GenDetail and GenDetailModal to layout its representation.
from codenerix.views import GenList, GenCreate, GenCreateModal, GenUpdate, GenUpdateModal, GenDelete
from library.forms import AuthorForm
class AuthorList(GenList):
model = Author
show_details = True
class AuthorCreate(GenCreate):
model = Author
form_class = AuthorForm
class AuthorCreateModal(GenCreateModal, AuthorCreate):
pass
class AuthorUpdate(GenUpdate):
model = Author
form_class = AuthorForm
class AuthorUpdateModal(GenUpdateModal, AuthorUpdate):
pass
class AuthorDelete(GenDelete):
model = Author
class AuthorDetails(GenDetail):
model = Author
groups = AuthorForm.__groups_details__()
class AuthorDetailModal(GenDetailModal, AuthorDetails):
pass
class BookList(GenList):
model = Book
show_details = True
class BookCreate(GenCreate):
model = Book
form_class = BookForm
class BookCreateModal(GenCreateModal, BookCreate):
pass
class BookUpdate(GenUpdate):
model = Book
form_class = BookForm
class BookUpdateModal(GenUpdateModal, BookUpdate):
pass
class BookDelete(GenDelete):
model = Book
class BookDetails(GenDetail):
model = Book
groups = BookForm.__groups_details__()
class BookDetailModal(GenDetailModal, BookDetails):
pass
The third step is to create the views. A basic view don’t need to associated to any html template, generation will be automatically accomplished by Codenerix.
from django.conf.urls import url
from library import views
urlpatterns = [
url(r'^book$',views.BookList.as_view(), name='book_list'),
url(r'^book/add$', views.BookCreate.as_view(), name='book_add'),
url(r'^book/addmodal$', views.BookCreateModal.as_view(), name='book_addmodal'),
url(r'^book/(?P<pk>\w+)$', views.BookDetail.as_view(), name='book_detail'),
url(r'^book/(?P<pk>\w+)/edit$', views.BookUpdate.as_view(), name='book_edit'),
url(r'^book/(?P<pk>\w+)/editmodal$', views.BookUpdateModal.as_view(), name='book_editmodal'),
url(r'^book/(?P<pk>\w+)/delete$', views.BookDelete.as_view(), name='book_delete'),
url(r'^author$', views.AuthorList.as_view(), name='author_list'),
url(r'^author/add$', views.AuthorCreate.as_view(), name='author_add'),
url(r'^author/addmodal$', views.AuthorCreateModal.as_view(), name='author_addmodal'),
url(r'^author/(?P<pk>\w+)$', views.AuthorDetail.as_view(), name='author_detail'),
url(r'^author/(?P<pk>\w+)/edit$', views.AuthorUpdate.as_view(), name='author_edit'),
url(r'^author/(?P<pk>\w+)/editmodal$', views.AuthorUpdateModal.as_view(), name='author_editmodal'),
url(r'^author/(?P<pk>\w+)/delete$', views.AuthorDelete.as_view(), name='author_delete'),
]
The last step is to associate the urls with the views using the Django routing system. The example from above shows the prefered naming conventions proposed by Codenerix.
Finally, we have a project ready to be tested using the Django development server.