Changes

Jump to: navigation, search

Gramps-Connect: Developer Introduction

155 bytes removed, 02:27, 8 July 2015
use {{Code Base}}
When you install and set up Django, all of the basic files are created for you with default settings. Django's
[httphttps://docs.djangoproject.com/en/1.17/ online documentation] and the [http://groups.google.com/group/django-users google group] are excellent references. To follow is an outline of Django's file structure, some important basics, and specific examples of ways to work with the Gramps dataset to create webpages.
In working with Gramps' Django files, there are four files that you may find yourself editing:
# [http://svn.code.sf.net/p/gramps/code/trunk/{{Code Base}}gramps/webapp/urls.py gramps/webapp/urls.py]# [http://svn.code.sf.net/p/gramps/code/trunk/{{Code Base}}gramps/webapp/grampsdb/views.py gramps/webapp/grampsdb/views.py]# [http://svn.code.sf.net/p/gramps/code/trunk/{{Code Base}}gramps/webapp/grampsdb/forms.py gramps/webapp/grampsdb/forms.py]# [http://svn.code.sf.net/p/gramps/code/trunk/{{Code Base}}data/templates/ data/templates/]
The template files have .html extensions and are found in the templates directory.
You will also find yourself referencing the
[http://svn.code.sf.net/p/gramps/code/trunk/{{Code Base}}gramps/webapp/grampsdb/models.py gramps/webapp/grampsdb/models.py] file, which defines the database structure. This file models the Gramps data as it exists in the desktop Gramps application and will not require alteration unless the database structure is changed. To access data from the database, you will often reference the models.py file and refer to the tables and fields it defines.
We will now go through each of these files, exploring the use and format of each.
'''==urls.py'''==
urls.py is the file in which you define of all of the website's urls
mapped the url (e.g., ''person_detail'').
'''==views.py'''==
The functions that you map to your urls will exist in the views.py
file. Each
[http://docs.djangoproject.com/en/1.17/topics/http/views/#topics-http-views view function] must take an http request object as its first parameter
and must return an http response object. The following view function
redirects the user to another page.
fields like ''first'' and ''last name'' are displayed.
'''==Templates'''==
A template defines the html, but it is more than just an html file.
within {{ }}, and there are a number of filters that you can apply to
the data using pipes. There is a
[httphttps://docs.djangoproject.com/en/1.17/ref/templates/builtins/#ref-templates-builtins full list] of filters and tags on the Django website.
[http://docs.djangoproject.com/en/1.17/topics/templates/#template-inheritance Template inheritance] is a huge timesavertime saver. With the ''extends''
tag at the top of the file, a template inherits everything from its
parent. The parent template defines blocks that may be overridden by
names in the dataset.
'''==forms.py'''==
Sometimes you want website users to be able to enter and edit data.
This requires a form, and Django has a system for generating and
manipulating form data.
[http://docs.djangoproject.com/en/1.17/topics/forms/#topics-forms-index Forms] are defined in the forms module and view functions may use
these definitions to create forms to pass along to a template. A form
outlines the fields for the screen:
Most of the forms you will need to define will be based on data from
the database. To save time, Django can define a form for you
[http://docs.djangoproject.com/en/1.17/topics/forms/modelforms/#topics-forms-modelforms based on a model definition]. Be sure to include models.py to access
those definitions.
than one record in a dataset. Django has a built-in abstraction layer
called
[http://docs.djangoproject.com/en/1.17/topics/forms/formsets/#topics-forms-formsets formsets] to create forms for datasets.
<pre>
An
[http://docs.djangoproject.com/en/1.17/topics/forms/modelforms/#inline-formsets inline formset] is a formset designed to help work with related
records. It isn't created with a class like a formset, but it does
automatically handle the foreign key fields between related tables.
Calling ''is_valid()'' on the form will run the clean functions and
return true if the data can be saved. Calling
[http://docs.djangoproject.com/en/1.17/topics/forms/modelforms/#the-save-method ''save()''] or ''save(commit=false)'' will also cause the clean
functions to run if they haven't already. The clean functions, when
they come across errors, fill error properties for each field and for
to format in paragraphs, ''.as_ul'' for an unordered list, and
''.as_table ''for a table. The following example
[http://docs.djangoproject.com/en/1.17/topics/forms/#looping-over-the-form-s-fields loops through the form's fields] to display each field (formatted as
an input), the field's label, the field's help text where applicable,
and the field's errors where not empty. A div refers to a custom css
</pre>
[http://docs.djangoproject.com/en/1.17/topics/forms/modelforms/#using-the-formset-in-the-template Formsets] are just a bit more complicated, since the formset consists
of multiple forms. As with forms, formsets can handle themselves:

Navigation menu