Question regarding taking input from a form in Django and transferring that data to a SQL database
You could use a model form. https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/#modelform
EG
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'email', 'body')
if request.method == 'POST':
# A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but dont save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
and then render that from . That will have the necessary fiedlds.
Or you could create a form the has the fields you created the html/css forms for and then render it. When it is submitted, save it to the model individually Eg
class CommentForm(forms.Form):
name = forms.CharField(max_length=25)
email = forms.EmailField()
body = forms.CharField(required=False,widget=forms.Textarea)
if request.method == 'POST':
# Form was submitted
form = CommentForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
comment = Comment(
name = cd['name'],
email = cd['email'],
body = cd['body'],
)
comment.save()
else:
form = EmailPostForm()