filmov
tv
Model Forms in Django (How to save data using model and form)

Показать описание
A model Form is a python class which is directly connected to model and helps in generating the required HTML form to facilitate the posting of the data by the user and easily saving it into the database.
class Articles(models.Model):
"to save data in the articles table"
title = models.CharField(max_length=150)
description = models.TextField()
created_by = models.CharField(max_length=150)
updated_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
class Meta:
db_table = 'articles'
from .models import Articles
from django import forms
class ArticlesForm(forms.ModelForm):
"to save data into the database using articles form"
class Meta:
model = Articles
fields = ('title','description')
from .forms import ArticlesForm
def create_article(request):
"to create articles"
form = ArticlesForm(request.POST)
return redirect(reverse('create_success'))
else:
form = ArticlesForm()
Please watch the video carefully to grasp the concept completely.
class Articles(models.Model):
"to save data in the articles table"
title = models.CharField(max_length=150)
description = models.TextField()
created_by = models.CharField(max_length=150)
updated_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
class Meta:
db_table = 'articles'
from .models import Articles
from django import forms
class ArticlesForm(forms.ModelForm):
"to save data into the database using articles form"
class Meta:
model = Articles
fields = ('title','description')
from .forms import ArticlesForm
def create_article(request):
"to create articles"
form = ArticlesForm(request.POST)
return redirect(reverse('create_success'))
else:
form = ArticlesForm()
Please watch the video carefully to grasp the concept completely.