HTML form filled, proposes a download link for the data filled in a json file

29 views
Skip to first unread message

Bassem Boulfarkat

unread,
Apr 8, 2019, 6:20:25 PM4/8/19
to Django users

I am working on creating a website based on the django framework with python. The aim is to have a simple HTML form that will build around where the user fills in the form and when it is done can submit it. When submitted, he will have a json file downloaded.

After looking around, I found that serializer would be the best way but I am still really confused. Would appreciate any kind of help.

Robin Riis

unread,
Apr 9, 2019, 3:49:38 AM4/9/19
to django...@googlegroups.com
Hello!

well in the form is there anything that will be saved in a database?
if so you can start by looking at models.py in your application.

if not you can jump directly to forms.py.

an example of a model and form:

*** in models.py
from django.db import models

class Company(models.Model):
    """This is your database model with the fields you want to store and fetch!
    """
    name = models.CharField(max_length=60)
    telephone = models.CharField(max_length=15, null=True, blank=True)
    info = models.TextField(blank=True, null=True)
    is_awesome = models.BooleanField(default=False)

*** in forms.py
from django import forms
from .models import Company

class CompanyForm(forms.ModelForm):
    class Meta:
        model = Company
        fields = '__all__'
        widgets = {
            'is_awesome': forms.HiddenInput(),
        }

then when you have a form and model you can use the generic views like this:

*** in views.py
from django.urls import reverse_lazy
from django.views.generic import CreateView
from django.contrib.messages.views import SuccessMessageMixin

from .models import Company
from .forms import CompanyForm

class Add_Company(SuccessMessageMixin, CreateView):
    template_name = 'company/add_a_new.html'
    success_message = 'a new company was added!'
    success_url = reverse_lazy('home')
    form_class = CompanyForm

thats it!
    

Den tis 9 apr. 2019 kl 00:20 skrev Bassem Boulfarkat <bassem.b...@gmail.com>:

I am working on creating a website based on the django framework with python. The aim is to have a simple HTML form that will build around where the user fills in the form and when it is done can submit it. When submitted, he will have a json file downloaded.

After looking around, I found that serializer would be the best way but I am still really confused. Would appreciate any kind of help.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3bd76a23-e5d8-4597-8fc1-3e9f2fc7cb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages