Skip to main content

Integrate Forms in view

Introduction

Django forms can be rendered as HTML forms using the as_p, as_table, or as_ul methods, which generate HTML code that can be included in a web page. You can also customize the appearance of forms by using CSS or by rendering the form fields manually.

For this purpose, view should send form to user. So let's add them.

User Form

Add following code in signup() in view.py

blog/views.py
if request.method == 'GET':
signupForm = UserForm()
return render(request, 'signup.html', {'signupForm': signupForm})
note

I will create html files in later topics.

Login Form

Add following code in signin() in view.py

blog/views.py
if request.method == 'GET':
loginForm = LoginForm()
return render(request, 'signin.html', {'loginForm': loginForm})

Post Form

Add following code in create_post() in view.py

blog/views.py
if request.method == 'GET':
blogForm = BlogForm()
return render(request, 'create_post.html', {'postForm': blogForm})

Comment Form

Add following code in comment() in view.py

blog/views.py
if request.method == 'GET':
commentForm = CommentForm()
return render(request, 'comment.html', {'commentForm': commentForm})

Final File

After adding above code, views.py should look like following:

blog/views.py
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render
from . models import *
from . forms import *
def home(request):
return HttpResponse("Hello, World. You're at the blog's home Page.")

def signup(request):
if request.method == 'GET':
signupForm = UserForm()
return render(request, 'signup.html', {'signupForm': signupForm})

if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['pass']
user = User.objects.create(username=username, email=email, password=password)
return HttpResponse("user created successfuly.", user)

def signin(request):
if request.method == 'GET':
loginForm = LoginForm()
return render(request, 'signin.html', {'loginForm': loginForm})

if request.method =='POST':
email = request.POST['email']
password = request.POST['password']
user = user.authenticate(email=email, password=password)
if user is not None:
return HttpResponse("user logged in successfuly.", user)
else:
return HttpResponse("user not found.", user)

def create_post(request):
if request.method == 'GET':
blogForm = BlogForm()
return render(request, 'create_post.html', {'postForm': blogForm})

if request.method == 'POST':
title = request.POST['title']
content = request.POST['content']
image = request.POST['image']
author= request.user
blog = Blog.objects.create(title=title, content=content, image=image, author=author)
return HttpResponse("Hello, World. You're at the blog's createPost Page.", blog)

def get_all_posts(request):
posts = Blog.objects.all()
return HttpResponse("Hello, World. You're at the blog's getAllPosts Page.", posts)

def get_post_by_id(request,id):
post = Blog.objects.get(id=id)
comments = Comment.objects.filter(blog=post)
return HttpResponse("Hello, World. You're at the blog's getPostById Page.", post, comments)

def comment(request):
if request.method == 'GET':
commentForm = CommentForm()
return render(request, 'comment.html', {'commentForm': commentForm})

if request.method == 'POST':
comment = request.POST['comment']
blog = request.POST['blog']
user = request.POST['user']
author= request.user
comment = Comment.objects.create(comment=comment, blog=blog, user=user, author=author)
return HttpResponse("Hello, World. You're at the blog's comment Page.", comment)