首页 > 其他分享 >Go - Web Application 5

Go - Web Application 5

时间:2024-09-04 22:35:58浏览次数:10  
标签:Web form create will Application snippet our Go new

Processing forms

We’re going to add an HTML form for creating new snippets. The form will look a bit like this:

The high-level flow for processing this form will follow a standard Post-Redirect-Get pattern and will work like so:

1. The user is shown the blank form when they make a GET request to /snippet/create .
2. The user completes the form and it’s submitted to the server via a POST request to /snippet/create .
3. The form data will be validated by our snippetCreatePost handler. If there are any validation failures the form will be re-displayed with the appropriate form fields highlighted. If it passes our validation checks, the data for the new snippet will be added to the database and then we’ll redirect the user to GET /snippet/view/{id} .

 

Setting up an HTML form

Let’s begin by making a new ui/html/pages/create.html file to hold the HTML for the form.

{{define "title"}}Create a new Snippet{{end}}

{{define "main"}}
        <main>
            <form action="/snippet/create" method="POST">
                <div>
                    <label>Title:</label>
                    <input type="text" name="title">
                </div>
                <div>
                    <label>Content:</label>
                    <textarea name="content"></textarea>
                </div>
                <div>
                    <label>Delete in:</label>
                    <input type="radio" name="expires" value="365" checked> One Year
                    <input type="radio" name="expires" value="7"> One Week
                    <input type="radio" name="expires" value="1"> One Day
                </div>
                <div>
                    <input type="submit" value="Publish snippet">
                </div>
            </form>
        </main>
{{end}}

Now let’s add a new ‘Create snippet’ link to the navigation bar for our application, so that clicking it will take the user to this new form.

{{define "nav"}}
        <nav>
            <a href="/">Home</a>
            <a href="/snippet/create">Create snippet</a>
        </nav>
{{end}}

And finally, we need to update the snippetCreate handler so that it renders our new page like so:

func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
    data := app.newTemplateData(r)

    app.render(w, r, http.StatusOK, "create.html", data)
}

 

标签:Web,form,create,will,Application,snippet,our,Go,new
From: https://www.cnblogs.com/zhangzhihui/p/18397457

相关文章

  • Study Plan For Algorithms - Part21
    1.缺失的第一个正数题目链接:https://leetcode.cn/problems/first-missing-positive/给定一个未排序的整数数组nums,请找出其中没有出现的最小的正整数。classSolution:deffirstMissingPositive(self,nums:List[int])->int:n=len(nums)forii......
  • 使用AI写WebSocket知识是一种怎么样的体验?
    一、WebSocket基础知识1.WebSocket概念1.1为什么会出现WebSocket一般的Http请求我们只有主动去请求接口,才能获取到服务器的数据。例如前后端分离的开发场景,自嘲为切图仔的前端大佬找你要一个配置信息的接口,我们后端开发三下两下开发出一个RESTful架构风格的API接口,只有当......
  • GoPro HERO13 Black 图解 All In One
    GoProHERO13Black图解AllInOnehttps://gopro.com/zh/cn/shop/cameras/learn/hero13black/CHDHX-131-master.htmldemos(......
  • 【Django开发】前后端分离django美多商城项目第9篇:收货地址,1. 展示收货地址界面【附
    本教程的知识点为:项目准备项目准备配置1.修改settings/dev.py文件中的路径信息2.INSTALLED_APPS3.数据库用户部分图片1.后端接口设计:视图原型2.具体视图实现用户部分使用Celery完成发送判断帐号是否存在1.判断用户名是否存在后端接口设计:用户部分JWT什......
  • java 使用WebSocket 功能实现
    java使用WebSocket功能实现我整理的一些关于【架构设计】的项目学习资料+视频(附讲解~~)和大家一起分享、学习一下: https://d.51cto.com/bLN8S1实现Java直播弹幕功能1.引言本文将教会一位刚入行的小白如何实现Java直播弹幕功能。直播弹幕是指在直播过程中,观众可以发送实时弹幕......
  • Go - Web Application 4
    HowmiddlewareworksInfact,we’reactuallyalreadyusingsomemiddlewareinourapplication—the http.StripPrefix()functionfromservingstaticfiles,whichremovesaspecificprefixfrom therequest’sURLpathbeforepassingtherequestontothefi......
  • 基于Javaweb实现的物流管理系统设计与实现(源码+数据库+论文+部署+文档+讲解视频等)
    文章目录1.前言2.系统演示录像3.论文参考4.代码运行展示图5.技术框架5.1SpringBoot技术介绍5.2Vue技术介绍6.可行性分析7.系统测试7.1系统测试的目的7.2系统功能测试8.数据库表设计9.代码参考10.数据库脚本11.找我做程序,有什么保障?12.联系我们1.前......
  • VSCode Webview 插件开发的模板的踩坑记录
    问题CSP:refusedxxxxxx常见的几类报错(打开开发者工具,在控制台就会自动输出)refusedtoapplyinlinestylebecauseitviolatesthefollowingContentSecurityPolicydirectivexxxxxxrefusedtoloadthescript''becauseitviolatesthefollowingContentSecuri......
  • 动态引入模块:Webpack require.context 的灵活运用
    require.context是一个非常有用的WebpackAPI,它允许我们在编译时动态地引入模块。这个功能在一些场景下非常有用,比如需要动态加载模块、实现国际化、主题切换等功能时会经常用到。require.contextAPI说明官方文档:依赖管理|webpack中文文档首先,让我们了解一下require......
  • fatal: unable to access 'https://aomedia.googlesource.com/aom.git/': Failed to c
     低版本的Mac安装PHP就是受罪brewinstallshivammathur/php/[email protected]:YouareusingmacOS11.We(andApple)donotprovidesupportforthisoldversion.Itisexpectedbehaviourthatsomeformulaewillfailtobuildinthisoldversion.Itisexpec......