TAGS :Viewed: 4 - Published at: a few seconds ago

[ Saving model form not working in Python and Django ]

I have the following models:

class Equipment(models.Model):
    asset_number = models.CharField(max_length = 200)
    serial_number = models.CharField(max_length = 200)
    description = models.TextField(max_length = 200)
    def __str__(self):
        return self.description

And Views:

from django.shortcuts import render
from .models import EquipmentForm
from django.utils import timezone
from django.views import generic
from django.urls import reverse
from .models import Equipment
from django.http import HttpResponse, HttpResponseRedirect

def default (request):
    return render(request, 'calbase/default.html')

def default_new (request):
    if request.method == "POST":
        form = EquipmentForm()
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            return HttpResponseRedirect(reverse('calbase:default_detail', args=(pk)))
        form = EquipmentForm()
    return render(request, 'calbase/default_edit.html', {'form':form})

class default_detail (generic.DetailView):
    model = Equipment
    template_name = 'calbase/default_detail.html'

And templates:


{% load staticfiles %}
        <title>Equipment calibration database</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
        <div class="page-header">
            <a href="{% url 'default_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
            <h1><a href="/">Equipment calibration database</a></h1>
        <div class="content container">
            <div class="row">
                <div class="col-md-8">
                    {% block content %}
                    {% endblock %}


{% extends 'calbase/default.html' %}
{% block content %}
    <h1>New post</h1>
    <form method="POST" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
{% endblock %}


<h1>{{ equipment.equipment.serial_number }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'calbase:default_new' pk %}" method="post">
{% csrf_token %}

<input type="submit" value="Default_new" />

What I would like to do here is simply post user input to database by a ModelForm. But after inputting those two parameters for a new post and click save, nothing happens and there is just no Equipment created. In theory it should record this and jump to the detail page for this piece of equipment. Where did I go wrong?

Answer 1

You have not passed your request to form.

if request.method == "POST":
        #form = EquipmentForm()
        form = EquipmentForm(request.POST)