Beego process simple form using GET and POST method

7/23/2017 11:49 PM

In below example we are creating a simple Contact Us page. For which we are creating a form in template, new controller, set the route for controller, call Get method to load the page and send the form data using Post moethod.

Step 1:

Go to the views folder [src\demoProject\views] and create basic layout which we can use in all CMS pages. - Create homelayout.html write below code in head section.

- Add below code in body section.
View layout - Now Create another file contactusTemplate.html with below code.
When user will click on submit button page will redirect to same page "/contactus" but method is Post. Hence, it will display another page. - Now create another file thankyouTemplate.html with below code.
{{if .name}} Thank you, {{.name}}, we update you on {{.email}}. {{end}}
{{.name}} and {{.email}} variables will display values submitted by user.

Step 2:

Go to the controllers folder [src\demoProject\controllers] - Now create ContactusController this we can use to display a Contact Us page. - Write below code in contactus.go
package controllers

type ContactusController struct {

func (this *ContactusController) Get() {
	this.TplName = "contactusTemplate.html"

func (this *ContactusController) Post() {
	this.TplName = "thankyouTemplate.html"
	this.Data["name"] = this.GetString("name")
	this.Data["email"] = this.GetString("email")
In about ContactusController we have Get() method which is loads the contactusTemplate.html. The Post method will loads process the form data and pass to thankyouTemplate.html.

Step 3:

Go to the routers folder [src\demoProject\routers] and open the router.go file. Add below line in router.go to call the ContactusController's Get and Post Method. - The updated code in router.go is as follow:
package routers

import (

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/about", &controllers.AboutController{})
	beego.Router("/contactus", &controllers.ContactusController{})

Step 4:

Now using commond prompt go to the [src\demoProject] folder and run the commond "bee run watchall" - In browser now hit the URL Run Bee Run Bee