Reputation: 647
So what I'm trying to do is, I get long text from a user via the modelform I've created. Then in my models, I want to save the first sentence of the text.
In order to do this, I believe I have to get the data(long text) from the form, and use a function to leave nothing but the first sentence. But I have as a beginner in Django, I have no idea on how to get an individual field from a form. So this is my codes:
models.py
class InputArticle(models.Model):
objects = models.Manager()
authuser = models.ForeignKey(User, on_delete=models.CASCADE, related_name = 'dansang', null=True, default=None)
title = models.CharField(max_length=100)
contents = models.TextField(max_length=100000)
first_sentence = models.CharField(max_length=100)
def __str__(self):
return self.title
forms.py
class InputArticleForm(forms.ModelForm):
class Meta:
model=InputArticle
fields=['title', 'contents']
I want to later display the title, contents, and first sentence in a separate page.
I very much appreciate your help :)
Upvotes: 0
Views: 904
Reputation: 3392
you can call it individually with the field name, see doc
{{ form.fieldname }}
Upvotes: 1
Reputation: 4432
You can simply do this in your view:
if form.is_valid():
obj = form.save(commit=False)
obj.contents = obj.contents.split('.')[0] # or whatever way you want to keep only first sentence
obj.save()
Upvotes: 1