一、前端模板
复制前端模板,一般都在form表单里面,另外再搭配一个java script。
二、添加csrf_token
一定要在表单的
</form>
前面加上{% csrf_token %}
三、修改models
根据前端表单的样式,设计数据库。
class UserMessage(models.Model): name = models.CharField(max_length=20,verbose_name="用户名") email = models.EmailField(verbose_name = "邮箱") original_url = models.URLField(max_length=300,verbose_name="原始链接") reported_url = models.URLField(max_length=300,verbose_name="本站链接") message = models.TextField(max_length = 1024,verbose_name="留言信息") class Meta: verbose_name = '用户留言信息'
并生成数据库。
四、views
其中urlcopy,link这些名称是前端表单中设定的name。
def DmcaView(request): if request.method == "POST": name = request.POST.get('name','') email = request.POST.get('email','') original_url = request.POST.get('urlcopy','') reported_url = request.POST.get('link','') message = request.POST.get('message','') user_message = UserMessage() user_message.name = name user_message.email = email user_message.original_url = original_url user_message.reported_url = reported_url user_message.message = message user_message.save() return render(request, 'blog/dmca.html')
五、设定url
url(r'^dmca/$', DmcaView, name='dmca'), # dmca页面
六、前端代码
<form class="form-horizontal" role="form" name="cform" method="post" action="/dmca/"> <input type="hidden" name="referer" value="https://www."> <input type="hidden" name="m" value="dmca"> <div class="formshow_block"> <div class="input_block mt20" style="margin-top:10px;"> <label for="email"><p class="g_tipsite">* Your Name:</p></label> <input type="text" name="name" class="input_text input_text_3 g_inputrigger" value=""> </div> <div class="input_block mt20" style="margin-top:10px;"> <label for="email"><p class="g_tipsite">* Your email:</p></label> <input type="text" name="email" class="input_text input_text_3 g_inputrigger email" value=""> </div> <div class="input_block mt20" style="margin-top:10px;"> <label for="email"><p class="g_tipsite">* The original URL of the copyrighted work:</p></label> <input type="text" name="urlcopy" class="input_text input_text_3 g_inputrigger email" value="https://"> </div> <div class="input_block mt20" style="margin-top:10px;"> <label for="email"><p class="g_tipsite">* Reported URL on Sparklepng.com:</p></label> <input type="text" name="link" class="input_text input_text_3 g_inputrigger email" value=""> </div> <div class="input_block mt20" style="margin-top:10px;"> <label for="givequestion"><p class="g_tipsite">* Please describe the copyrighted work in detail:</p></label> <textarea name=message class="form-control" cols=50 rows=12></textarea> </div> <p class="g_right_a mt20"><a class="create_one_but abtn" href="javascript:reportdmca()" style="background:#232328;">Submit</a><span id="mail_status_hint" style="color: green; font-weight: bold; margin-left: 15px; display: none;"></span></p> </div> {% csrf_token %} </form>
注意:
一、django-simple-captcha
本来打算安装django-simple-captcha,结果它不提示,直接将我的django卸载了。
后再卸载最新版本django,重安装django旧版本问题解决。
Installing collected packages: typing-extensions, sqlparse, asgiref, Django, django-ranged-response, django-simple-captcha Attempting uninstall: Django Found existing installation: Django 1.11.12 Uninstalling Django-1.11.12: Successfully uninstalled Django-1.11.12
参考:https://blog.csdn.net/happygjcd/article/details/103075918