I'm making a simple web server to take html input data and insert it to data base table .
I've tried with POST request got into more CSRF troubles , Turned to GET request to go over CSRF (not necessary in my case ) , still not able to GET the html data into Database .
myapp/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=300, unique=True)
content = models.TextField()
myapp/templates/createpost.html
<head>
<title>Create a Post </title>
</head>
<body>
<h1>Create a Post </h1>
<form action="" method="GET">
{%csrf_token%}
Title: <input type="text" name="title"/><br/>
Content: <br/>
<textarea cols="35" rows="8" name="content">
</textarea><br/>
<input type="submit" value="Post"/>
</form>
</body>
</html>
myapp/views.py
from django.shortcuts import render
from .models import Post
def createpost(request):
if request.method == 'GET':
if request.GET.get('title', None) and request.GET.get('content', None):
post = Post()
post.title = request.GET.get('title', None)
post.content = request.GET.get('content', None)
post.save()
return render(request, 'createpost.html')
else:
return render(request, 'createpost.html')
urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='createpost.html'), name='createpost'),
]
am using Django 2.2.6 with PyCharm community 2019.2.3 I've been searching for almost 2 days , checked django doc , and stackoverflow answers , none was helpful , I had to ask to make sure its not a version related issue and forgive me if i miss understand a simple point am just a beginner.
request.GET.get('title', None)is it empty or None? what do you get?print(request.GET.get('title', None))and submit the form again and check