Home>

After writing all the data in the forms, nothing is added to the database, although in the terminal it says that the post request has been sent and does not give any errors. How to fix?

forms.py

from django.forms import ModelForm, TextInput, Textarea, NumberInput, FileInput
from .models import Recipe
class FoodForm(ModelForm):
    class Meta:
        model= Recipe
        fields= ["recipe_title", "recipe", "recipe_time", "recipe_ingridients", "author_name", "image"]
        widgets= {
            "recipe_title" : TextInput(
                attrs={
                    "class" : "title_form",
                    "placeholder" : "Enter recipe name"
                }
            ),
            "recipe": Textarea(
                attrs={
                    "class": "form_of_all",
                    "placeholder": "Enter your recipe"
                }
            ),
            "recipe_time" : NumberInput(
                attrs={
                    "class" : "ingr",
                    "placeholder" : "Enter time"
                }
            ),
            "recipe_ingridients": NumberInput(
                attrs={
                    "class": "ingr",
                    "placeholder": "Enter number of ingredients"
                }
            ),
            "image" : FileInput(
                attrs={
                    "name" : "input_file",
                    "id" : "input_file"
                }
            )
        }

models.py

from django.db import models
class Recipe(models.Model):
    recipe_title= models.CharField(max_length=20)
    recipe_time= models.IntegerField(max_length=3)
    recipe_ingridients= models.IntegerField(max_length=2)
    author_name= models.CharField(max_length=30)
    image= models.ImageField(upload_to='')
    recipe= models.TextField(max_length=300)

views.py

def crate(requset):
    form= FoodForm()
    if request.method== "POST":
        form= FoodForm(requset.POST)
        if form.is_valid():
            form.save()
    context= {
        "form" : form
    }
    return render(requset, "food/crate.html", context)

html

{% extends 'food/crate_base.html' %}
{% load static %}
{% block content %}
    <div class= "body_of_content">             <span class= "text_">Create Recipe</span>             <div class= "back1"><img src= "{% static 'images_defolt/back1.png' %}" class= "first_img"></div>             <div class= "back2"><img src= "{% static 'images_defolt/back1.png' %}" class= "first_img"></div>            <form method="post">                {% csrf_token %}
                {{ form.recipe_title }}<br>                {{ form.recipe}}<br>                {{ form.recipe_time }}<br>                {{ form.recipe_ingridients}}<br>                 <label for="input__file" class="input__file-button">                    {{ form.image}}<br>                  <span class="input__file-icon-wrapper"><img class="input__file-icon" src="{% static 'images_defolt/down.png' %}" alt="Select file" width="25"></span>                  <span class="input__file-button-text">Select file</span>               </label>                 <button class= "create_btn" type= "submit" formmethod="post" >Create</button>            </form>         </div>{% endblock %}

Replace all requset with request.

Sherlock2022-02-13 22:30:25

I later saw a typo, but the correct spelling of request did not solve the problem...

ivaivie2022-02-14 07:25:33