O
O
Oleg Kotov2019-04-12 13:16:19
Django
Oleg Kotov, 2019-04-12 13:16:19

How to render a variable in a Form Field in Django 2.1.7?

I am a beginner jungist using Django 2.7.1 and Python 3.7.2. I'm trying to make a thing that will receive data from Form Fields on the site and process it, and then display the result in another Form Field on the same page.
I tried to solve this whole thing through the methods of the models, but did not understand how to use them, so I decided to try writing functions in views.py.
Also, in the process of searching, I got acquainted with celery and other similar asynchronous solutions, but as it turned out, this will not help me, because I do not understand the essence.
In general, now I seem to be starting to understand, but I don’t understand at all how to render the result on pressing a button with a POST request.
The code is below.
This is models.py

from django.db import models

# Create your models here.

class CombinatorCols(models.Model):
  first_column = models.TextField("Column 1", null=True, blank=True)
  second_column = models.TextField("Column 2", null=True, blank=True)
  third_column = models.TextField("Column 3", null=True, blank=True)
  fourth_column = models.TextField("Column 4", null=True, blank=True)
  result = models.TextField("Result", help_text='Halo', blank=True,)
  def __str__(self):
    return self.first_column

This is my forms.py
from django import forms
from .models import CombinatorCols

class CombinatorForm(forms.ModelForm):
    class Meta:
        model = CombinatorCols
        fields = ('first_column', 'second_column', 'third_column','fourth_column', 'result',)

This is my views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.template import loader
from .models import Combinate,CombinatorCols
from .forms import CombinatorForm,ResultForm
# Create your views here.


def get_col(request):
  #column = get_object_or_404(CombinatorCols)
  if request.method == "POST":
    form = CombinatorForm(request.POST)
    if form.is_valid():
      column = form.save()
      column.sender = request.user
      first_handle = [i for i in column.first_column.split('\n') if i]
      second_handle = column.second_column
      third_handle = column.third_column
      fourth_handle = column.fourth_column
      column.result = first_handle
      column.save()
      print(first_handle)
      print('Naiiiceee')

  else:
    form = CombinatorForm()

  return render(request, 'index.html', {'form': form})

And finally my index.html:
<form action="" method="post">
  {% csrf_token %}
  {{ form.as_table }}
  <input type="submit" value="Submit">
</form>

I'm expecting the 'results' field on the site to update after the button is clicked and set to the value self_handlefrom views.py, but that doesn't happen. printoutput correctly.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question