TadasK
TadasK

Reputation: 55

Django adding fields in a form statically

Good day,

I am trying to make a Django form where I could add fields statically (the concept is described in the figure below).

I want to have a form that would have a button "new". The button "new" should redirect to another page where I could fill the data such as from, to, and amount. Once I press submit it should go back to the form where the filled data should be visible in the form (as amount 1, amount 2 and etc.).

Could somebody lead me on the right path how to start?

P.S. I cannot use javascript

enter image description here

Code which I am using:

models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from django.forms import inlineformset_factory


class Post(models.Model):
    title_choices = [
        ('Mr', 'Mister'),
        ('Ms.', 'Miss'),
    ]

    title = models.CharField(max_length=100)
    first_name = models.CharField(max_length=100, default = '')
    last_name = models.CharField(max_length=100, default = '')
    date_posted = models.DateTimeField(default=timezone.now)

    titles = models.CharField(
        max_length = 3,
        choices = title_choices,
        default= 'Mr', 
    )

    

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

class Debt(models.Model):
    Money = models.ForeignKey(Post, on_delete=models.CASCADE, null = True)
    CurrentDebt = models.CharField(max_length=100, default = '')
    Period = models.CharField(max_length=100, default = '')

views.py

from django.shortcuts import render
from django.views.generic import (
    View,
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView,
    TemplateView
)
from .models import Post, Debt
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .latex import tex
from django.http import FileResponse, Http404
from django.urls import reverse
from django.forms import inlineformset_factory, BaseInlineFormSet

FullForm = inlineformset_factory(Post, Debt, fields=('CurrentDebt', 'Period',), extra= 1, can_delete= True)

class PostCreateView(LoginRequiredMixin, CreateView, BaseInlineFormSet):
    model = Post
    fields = ['first_name', 'last_name', 'titles' ,'title']

    def form_valid(self, form):
        form.instance.author = self.request.user # Use this to get the information from the form.
        response = super().form_valid(form)
        return response


class PostCreate2View(LoginRequiredMixin, View):
    
    template_name = 'Blog/form.html'
    model = Debt
    fields = ['CurrentDebt', 'Period']

Upvotes: 1

Views: 140

Answers (1)

Vincent Lortie
Vincent Lortie

Reputation: 119

For simplicity, I will use the three rectangle and number them from 1 to 3 going from left to right.

Since you don't have code, I will try and give you a rough blueprint with methods I think can help you. I'm assuming rectangle 2 and rectangle 3 are forms which a user populates data and submitting rectangle 2 adds data to rectangle 1.

The challenges I see are: 1. Having rectangle 2 remember what original data was submitted for name and last_name once we go to rectangle 3 and back to rectangle 2. 2. Transmit the data from rectangle 2 back to rectangle 3.

For challenge 1, I would use a hidden input. You would have two hidden inputs, one for name and one for last_name.

For challenge 2, I would use FormMixin. With this mixin, you can use methods like get_form_kwargs (see previous link) to pass data from your view to your form. So once you submit rectangle 3, you should redirect the form data to your GET request for rectangle 2. From there, you use get_form_kwargs and populate your form with appropriate data in its __init__ method.

Upvotes: 1

Related Questions