python - Inserting data in postgreSQL database by Django model -
i found error when insert data postgres database django model,when put csrf package in comment oage found else shows forbidden error code , screen shot below
here html file: {% extends "homepage/index.html" %} {% block title %} contact {% endblock %} {% block content %} contact page. <form action="/ins/" method="post"> {% csrf_token %} <table> <tr> <td>created date</td> <td><input type="text" name="cid"></td> </tr> <tr> <td>updated date</td> <td><input type="text" name="uid"></td> </tr> <tr> <td>title</td> <td><input type="text" name="tid"></td> </tr> <tr> <td>text</td> <td><input type="text" name="txid"></td> </tr> <tr> <td>published date</td> <td><input type="text" name="pid"></td> </tr> <tr> <input type="hidden" name="fdfdf" value="{{ csrf_token }}"> <td><input type="submit" value="insert"></td> <td><input type="reset" value="reset"></td> </tr> </table> </form> {% endblock %} views.py file: def ins(request): #c = {} #c.update(csrf(request)) cr = request.post.get('cid','') = request.post.get('uid','') tit = request.post.get('tid','') tx = request.post.get('txid','') pd = request.post.get('pid','') e = entry(created=cr,updated=up,title=tit,text=tx,published=pd) e.save() return httpresponse("inserted successfuly..")
i'm not sure why you're doing work hand. here's need do:
# forms.py django import forms your_app.models import entry class entryform(forms.modelform): class meta: model = entry # views.py django.shortcuts import render your_app.forms import entryform def ins(request): form = entryform(request.post or none) if request.method == 'post' , form.is_valid(): form.save() return render(request, 'homepage/index.html', {'form': form}) # index.html {# code shortened demonstration purposes #} <form action="." method="post" enctype="application/x-www-form-urlencoded"> {{ form.as_table }} {% csrf_token %} <button type="submit">insert</button> </form>
pulling form values directly out of request.post dictionary without passing them through form's validation horrible idea - please don't that.
Comments
Post a Comment