Skip to main content

A simple Django app to generate dynamic forms during runtime

Project description

Diafo is a simple Django app for generating dynamic forms during the runtime.

Detailed documentation is in the “docs” directory.

Requirements

  • Python: 3.4, 3.5, 3.6

  • Django: 1.11, 2.0b

  • Django bootstrap form: 1.4

Installation

  1. Install using pip:

pip3 install diafo
pip3 install django-bootstrap-form
  1. Then add 'django_filters' to your INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    'diafo',
    'bootstrapform',
]
  1. Include the polls URLconf in your project urls.py like this:

    url(r'^diafo/', include('diafo.urls')),
  2. Run python manage.py migrate to create the diafo models.

Usage

Diafo can be used for generating questionnaires consisting various questions during runtime. One can specify the question type like CharField, TextFied, ChoiceField, MultipleChoiceField, etc. Requirement can also be mentioned whether the question is compulsary or not.

Let see its usage through a simple survey webapp.

from diafo.models import Questionnaire

class Survey(models.Model):
    title = models.CharField(max_length=200)
    #link the form to your model
    questionnaire = models.OneToOneField(Questionnaire, null=True)

And then in your view you could do:

def new_survey(request):
    ...
    #get the title by using form or any how..
    questionnaire = Questionnaire.objects.create(name=tilte)
    survey = Survey.objects.create(title=title, questionnaire = questionnaire)
    return HttpResponseRedirect(reverse('diafo:admin_view', kwargs={'pk': survey.form.pk}))

So now that you have questionnaire linked to your model,you can add questions and edit the questionnaire thru the diafo:admin_view, and let the user fill the questionnaire through diafo:user_view.

The diafo main URLs are as follows:

urlpatterns = [
    # for adding question,editing questionnaire detail,seeing responses,etc
    url(r'^admin/(?P<pk>[0-9a-f-]+)/$', views.admin_view, name='admin_view'),
    # for normal users to fill the questionnaire(no edit rights)
    url(r'^user/view/(?P<view_id>[0-9a-f-]+)/$',views.user_view, name = 'user_view'),
]

The Questionnaire model is as follows:

class Questionnaire(models.Model):
    name = models.CharField(max_length=200, null=True)
    # id used for admin purpose.. used as pk in admin view
    #id and pk are same since primary key is True
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # view_id - used for normal user (can only see and fill the questionnaire)
    view_id = models.CharField(max_length=50,unique=True, default=uuid.uuid4)
    #specific settings (can be edited thru diafo admin view for given questionnaire)
    requires_sign_in = models.BooleanField(default = False, blank = True)
    collect_identity = models.BooleanField(default = False, blank = True)

This is how you can use use the diafo in your views or templates.

class yourview(request,your_parameters):
    ...
    # get the questionnare object.
    if want_to_go_on_admin_view:
        # use questionnare.pk and diafo:admin_view
        #for use in template href={% url 'diafo:admin_view' questionnaire.pk %}
        return HttpResponseRedirect(reverse('diafo:admin_view', kwargs={'pk':questionnaire.pk}))
    elif want_a_rendered_form_for_user:
        # use questionnaire.view_pk and diafo:user_view
        #for use in template href={% url 'diafo:user_view' questionnaire.view_id %}
        return HttpResponseRedirect(reverse('diafo:admin_view', kwargs={'view_id':questionnaire.view_id}))

Support

If you have questions about usage or development you can contact me.

Bugs

Really? Oh well… Please Report. Or better, fix :)

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diafo-1.0.tar.gz (9.5 MB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page