我设置了null = true and black = true,但仍然获取“ django.db.utils.integrityerror :( 1048,columt; massenger_name; null; smassenger_name;
我正在尝试运行我的页面,但我会收到此错误“ django.db.utils.integrityerror:(1048,“列”'massenger_name'不能为null')”)“)
我又有了同样的问题,但我仍然有同样的问题
,我的数据库是mysql
这是我的models.py
from django.db import models
# Create your models here.
class Name(models.Model):
massenger_name = models.CharField(max_length=255,null=True,blank=True)
action_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.massenger_name)
和我的views.py
from django.shortcuts import render
from .models import Name
from django.shortcuts import redirect
# Create your views here.
def Home(request):
name_input = request.POST.get('user_name')
name_in_model = Name(massenger_name=name_input)
name_in_model.save()
return render(request , 'index.html')
和我的index.html
<!DOCTYPE html>
<html lang="en" dir="rtl">
<head>
<meta charset="utf-8">
<title></title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
{% load static %}
<link rel="stylesheet" href="{% static 'CSS/style.css' %}">
</head>
<body>
<div id="form">
<form class="row" method="POST">
{% csrf_token %}
<div class="">
<input type="textarea" class="form-control" placeholder="أكتب أسمك (مثلا/أخوكم عبدالله العتيبي)" id="text_name" name="user_name" required>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" id="button">حمل الصورة</button>
</div>
</form>
</div>
</body>
</html>
我的设置。
"""
Django settings for Eid_G project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = os.path.join(BASE_DIR,"templates")
STATIC_DIR = os.path.join(BASE_DIR,"static")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Eid_Post',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Eid_G.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Eid_G.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_p',
'HOST': 'localhost',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
以及
im trying to run my page but i get this error "django.db.utils.IntegrityError: (1048, "Column 'massenger_name' cannot be null")"
and i makemigrations and imgrate again but still i have same problem
and my database is mysql
this my models.py
from django.db import models
# Create your models here.
class Name(models.Model):
massenger_name = models.CharField(max_length=255,null=True,blank=True)
action_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.massenger_name)
and this my views.py
from django.shortcuts import render
from .models import Name
from django.shortcuts import redirect
# Create your views here.
def Home(request):
name_input = request.POST.get('user_name')
name_in_model = Name(massenger_name=name_input)
name_in_model.save()
return render(request , 'index.html')
and this my index.html
<!DOCTYPE html>
<html lang="en" dir="rtl">
<head>
<meta charset="utf-8">
<title></title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
{% load static %}
<link rel="stylesheet" href="{% static 'CSS/style.css' %}">
</head>
<body>
<div id="form">
<form class="row" method="POST">
{% csrf_token %}
<div class="">
<input type="textarea" class="form-control" placeholder="أكتب أسمك (مثلا/أخوكم عبدالله العتيبي)" id="text_name" name="user_name" required>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" id="button">حمل الصورة</button>
</div>
</form>
</div>
</body>
</html>
and this my setting.py
"""
Django settings for Eid_G project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = os.path.join(BASE_DIR,"templates")
STATIC_DIR = os.path.join(BASE_DIR,"static")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Eid_Post',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Eid_G.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Eid_G.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_p',
'HOST': 'localhost',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
and thank u so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之所以发生这种情况,是因为您已经在数据库中拥有一些模型的数据,因此您需要首先删除数据。
您可以简单地从Django Shell中删除表中的所有对象:
转到命令提示符,
此对象将删除教程表中的所有对象,然后进行makemigrations和magrate迁移,并且可以正常工作。
或者,如果它仍然不起作用,则可以尝试以下方法:
This is happening because you already have some data of the model in the database so you need to delete the data first.
You can simply delete all the objects in the table from django shell:
go to command prompt
This will delete all the objects in the Tutorial table and then makemigrations and migrate and it should work just fine.
or if it still don't work you can try these approaches:
由于您删除了迁移,因此问题必须是Django失去了桌子上的约束。您应该寻找一种直接从数据库中删除而不是无效约束的方法。也许尝试
Since you've deleted your migrations, the problem must be that Django has lost track of what constraints are on your tables. You should look for a way to delete that NOT NULL constraint directly from the database. Maybe try this