[Django]-Django 1.11: post form data to database

4👍

There are lots of points you need to amend based on your code:

forms.py: you need ModelForm to work with model Post.

from django.forms import ModelForm
from .models import Post 
class ContentForm(ModelForm):
    class Meta:
        model = Post
        fields = "__all__" 

views.py: you have to pass the form to template so that it can be showed in your main html, and call form.save() to save data into db.

from django.shortcuts import render, redirect                                                                             
from django.http import HttpResponse                                                                            
from .forms import ContentForm                                                                                  
from .models import Post

def post(request ):                                                                                             
    post_list = Post.objects.order_by('id')
    form = ContentForm()
    return render(request, 'posts/main.html',
        {'post': post_list,
        'form':form},
    )                                                                                                           
def content_get(request):
    if request.method == 'POST':
        form=ContentForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/')

suppose you have app urls.py like this:

from django.conf.urls import url

from . import views

app_name = 'post'

urlpatterns = [
    url(r'^$', views.post, name='main'),
    url(r'^content$', views.content_get, name='content_get'),
]

last in main.html, you need to define which action to post:

<html>
<head>                                                                           
<h1>Nanoblogga</h1>                                                                                       
</head>                                                                          
<body>
    {% for i in post %}                                                                                       
    <li>{{ i }}</li> 
    {% endfor %}           

    <form action = '/content' method ='post'>
    {% csrf_token %} 
    {{ form }}
    <input type = 'submit' value = 'Submit' />
    </form>
</body> 
</html>
👤Tiny.D

2👍

def content_get(request):                                                                                                                                                                                      
   if request.method == 'POST':                                                                                                                                                                           
      form = ContentForm(request.POST)
      if form.is_valid():                                                          
           instance = form.save()
           return HttpResponse('create success')
      else:
           return HttpResponse(forms.errors)
   else:                                                                               
      return render(request, 'main.html', {'form':form})

main.html:

<form action = '/content_get/' method = 'post'>                                                                       
  {% csrf_token %}                                                                                        
  {{ form }}                                                                                              
  <input type = 'submit' value = 'Submit' />                                                              
</form>                                                                        

Note:you need to set form action to method content_get’s url.

👤Ykh

Leave a comment