1👍
From scratch. You do not need two Models
, just one with two fields is fine. To make a process over one of the two fields you can override .save
method of the model:
models.py
def process_text_field(text):
processed_text = text.lower()
processed_text = 'Processed Text:\n' + processed_text
return processed_text
class TextProcess(models.Model):
original = models.TextField()
processed = models.TextField()
def save(self, *args, **kwargs):
self.processed = process_text_field(self.original)
return super().save(*args, **kwargs)
For example, by writing a function that would process the original
field and assign the result to processed
.
def index(request):
obj_instance = None
if request.method == 'POST':
original = request.POST.get('text')
obj_instance = TextProcess.objects.create(original=original)
return render(request, "index.html",{'text': obj_instance})
index.html
{% block content %}
<section id="det">
<form action="{% url 'index' %}" method="post">
{% csrf_token %}
<div class="theText">
<textarea name="text" placeholder="Type/Paste to Summarize"></textarea>
<br>
<button type="submit">Start</button>
<button >Clean</button>
<br><br>
</div>
</form>
{% if text %}
<div class="theText">
<textarea placeholder="Your summarized text">{{text.processed}}</textarea>
</div>
{% endif %}
</section>
{% endblock %}
The same template is rendered, for both entering and displaying information. The difference is that on GET
requests there is no object instance created (handled at the view
), so only the first block (to enter the text) will be displayed (because of the if
condition).
If you are wondering about the URL
, that is just a way to obtain the full path by using the reverse resolution:
urls.py
urlpatterns = [
path('', index, name='index'),
]
I must say that there are other approaches, you could do the processing in other places for example in the view
. But in programming you always want to separate your logic as much as possible, following the single responsibility
principle.