1

I am new to golang. I am creating quiz application in gin framework of golang. After fetching questions from database when I try to render questions in html page using c.HTML(), c.HTML() is not working. I dont know why. In other controllers I have done the same thing it works fine, but in GetQuizController it is not working. Please help me.

https://github.com/Shashwat5522/quizzapp

router code

r := gin.Default()
r.LoadHTMLGlob("views/*")
Teacher := r.Group("/teacher", SessionHandler)
    {
        Teacher.GET("/teacherpanel", controllers.TeacherPanelController)
        Teacher.GET("/createquizz", controllers.CreateQuizController)
        Teacher.POST("/createquizz", controllers.PostQuizController)
        Teacher.GET("/addstudent", controllers.AddStudentController)
        Teacher.POST("/addstudent", controllers.PostStudentController)
        Teacher.GET("/listofquiz", controllers.GetListOfQuizController)
        Teacher.GET("/quiz",controllers.GetQuizController)
    }

controller code

func GetQuizController(c *gin.Context) {

    fmt.Println("get quiz controller called")
    quizname := c.Query("quizname")
    var questions []models.Questions
    dbconnection.DB.Debug().Model(&models.Quiz{}).Select("quizzes.quiz_name,questions.question,questions.option_a,questions.option_b,questions.option_c,questions.option_d,questions.answer,questions.difficulty").Joins("inner join questions on questions.quiz_id=quizzes.id").Where("quizzes.quiz_name=?", quizname).Scan(&questions)
    fmt.Println(questions)

    c.HTML(200,"showquiz.html",gin.H{
        "data":questions,
    })
}

Here in this GetQuizController c.HTML is not working. Please help me.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
  • What is the expected result? What have you seen instead? – Zeke Lu May 28 '23 at 05:47
  • @ZekeLu I want user directed towards showquiz.html file but nothing is happening. controller is called properly and questions are also get loaded but c.html is completely getting skipped. – Mehta Shashwat May 28 '23 at 07:58

1 Answers1

0

It seems that you don't know how to describe the issue so I will describe what I have done and the issues I have found. If you think that you have encountered other issues, please edit your question to describe what you have done and what is wrong like the one below.

  1. start a mysql server with docker:

    docker run -d --name mariadb -e MARIADB_ROOT_PASSWORD=root -p 3306:3306 mariadb:10.11.2
    
  2. create a database named quizzapplication:

    docker exec mariadb mysql --password=root -e 'CREATE DATABASE quizzapplication;'
    
  3. clone the repository (the commit is bfda880):

    git clone https://github.com/Shashwat5522/quizzapp.git
    
  4. change the working directory

    cd quizzapp/Quizz-Application
    
  5. add missing modules and remove unused modules

    go mod tidy
    

    The go.mod file requires gorm.io/driver/postgres, but the source code imports gorm.io/driver/mysql. Run go mod tidy to correct it.

  6. migrate the database

    go run ./migration
    
  7. start the web server:

    go run .
    
  8. open a new terminal and send a request:

    curl 'http://localhost:8080/teacher/quiz' 
    

    The output is:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Login-Form</title>
    </head>
    <body>
    <h1>Login-Form</h1>
        <form action="/authentication/Login" method="post">
            <label for="email">Email</label>
            <input type="email" id="email" name="email"><br>
            <label for="password">Password</label>
            <input type="text" id="password" name="password"><br>
    
    
            <input type="submit" value="submit">
        </form>
        <h1></h1>
    </body>
    </html><h1>Show quizz</h1>
    

<h1>Show quizz</h1> is rendered after the </html> tag. This is not a valid HTML. That's because you haven't called c.Abort() in SessionHandler. It could be fixed like this:

  func SessionHandler(c *gin.Context) {
    fmt.Println("session is called")

    session := sessions.Default(c)
    fmt.Println(session.Get("userID"))
    if session.Get("userID") == nil {
        c.HTML(200, "login.html", nil)
+       c.Abort()
    }
  }

I believe there are other issues in the source code. But I won't review the full project and find out all of them. This is merely a demo to show how to describe an issue clearly.

And bear in mind that, please keep the question focus. It's bad to post a project full of issues and ask others to find them out.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23