Я получаю эту ошибку, я не знаю, в чем проблема. Я очень новичок в django. Ошибка здесь: ограничение NOT NULL не выполнено: food_app_recipe.user_id, когда я пытаюсь опубликовать данные из шаблона. Помогите с ошибкой здесь
Вот мой Models.py
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length=20,null=True)
email = models.CharField(max_length=100,null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=50,null=True)
steps = models.TextField(max_length=1000,null=True)
image = models.ImageField(null = True, blank = True)
ingredients = models.CharField(max_length=100, null=True)
description = models.TextField(max_length=1000,null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
Вот мой forms.py
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Recipe
class SignupForm(UserCreationForm):
username = forms.CharField(max_length=30)
email = forms.EmailField(max_length=200)
class Meta:
model = User
fields = ['username','email','password1','password2']
class RecipeForm(ModelForm):
class Meta:
model = Recipe
fields = ['name','steps','image','ingredients','description']
Вот мой Views.py
from django.shortcuts import render, redirect
from .forms import SignupForm,RecipeForm
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import User,Recipe
# views here
@login_required(login_url='blog-login')
def add_recipes(request):
user = request.user
form = RecipeForm(instance=user)
if request.method == "POST":
print('printing post:',request.POST)
form = RecipeForm(request.POST,request.FILES)
if form.is_valid():
form.save()
return redirect('blog-home')
context = {'form':form}
return render(request,'food_app/Add_recipe.html',context)
Вот мой шаблон этой формы
{% block content %}
<div class = "container mt-4">
<div class = "row justify-content-center">
<div class = "col-6">
<div class = "card">
<div class = "card-body">
<form action="" method="POST" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</div>
</div>
</div>
</div>
</div>
{% endblock content %}