简体   繁体   English

/home/create/ 处的 IntegrityError(1048,“列 'country_id' 不能为空”)

[英]IntegrityError at /home/create/ (1048, "Column 'country_id' cannot be null")

I'm trying to create news directly on the webpage using Django 2.7, but have a problem with adding categories and countries IntegrityError at /home/create/ (1048, "Column 'country_id' cannot be null") Here is my code: home/models.py我正在尝试使用 Django 2.7 直接在网页上创建新闻,但是在 /home/create/(1048,“列 'country_id' 不能为空”)添加类别和国家/地区时遇到问题,这是我的代码:home /模型.py

class Category(models.Model):

    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Country(models.Model):

    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class News(models.Model):

    pub_date = models.DateTimeField('Date: ')
    article = models.CharField(max_length=200)
    content = models.TextField(default="")
    country = models.ForeignKey(Country, on_delete=models.CASCADE)
    likes = models.IntegerField(default=0)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.article

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

home/views.py主页/views.py

def create(request):

    if request.method == "POST":
        adding = News()
        adding.likes = 0
        adding.pub_date = timezone.now()
        adding.article = request.POST.get("Article")
        adding.content = request.POST.get("Content")
        adding.category = request.POST.get("Category")
        adding.country = request.POST.get("Country")
        adding.save()

    return HttpResponseRedirect("/home/")

home/urls.py主页/urls.py

from django.urls import path

from . import views

app_name = 'home'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:news_id>/', views.detail, name='detail'),
    path('create/', views.create),
    path('<int:news_id>/delete/', views.delete_news, name='delete'),

]

index.html (I add my posts from my home page) index.html(我从我的主页添加我的帖子)

 <body> <form method="POST" action="create/" class="post_form"> {% csrf_token %} <p> <label>Article</label><br> <input type="text" name="Article" /> </p> <p> <label>Content</label><br> <input type="text" name="Content" /> </p> <p> <label>Category</label><br> <select> {% if category %} {% for el in category %} <option name="Category">{{ el.name }}</option> {% endfor %} {% endif %} </select> </p> <p> <label>Country</label><br> <select> {% if countries %} {% for el in countries %} <option name="Country">{{ el.name }}</option> {% endfor %} {% endif %} </select> </p> <input type="submit" value="Submit" > </form> </body>

Please help!请帮忙!

You specify the name of a <select> input element in the <select> tag, not in the <option> tag.您可以在<select>标签中指定<select>输入元素的名称,而不是<option>标签中。 You thus should write your form as:因此,您应该将表单编写为:

<select name="Country">
    {% if countries %}
        {% for el in countries %}
            <option>{{ el.name }}</option>
        {% endfor %}
    {% endif %}
</select>

That being said, I strongly advise you to work with Django forms [Django-doc] .话虽如此,我强烈建议您使用Django 表单[Django-doc] These encapsulate the logic, make it less likely to make mistakes, and remove a lot of boilerplate code.这些封装了逻辑,使其不太可能出错,并删除了大量样板代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 /quizmaker/ 处的 IntegrityError(1048,“列 'id_id' 不能为空”) - IntegrityError at /quizmaker/ (1048, “Column 'id_id' cannot be null”) /auth/userreg/ 处的 IntegrityError(1048,“列 'user_id' 不能为空”) - IntegrityError at /auth/userreg/ (1048, "Column 'user_id' cannot be null") python django 中 /***/ (1048, &quot;Column &#39;***&#39; cannot be null&quot;) 处的 IntegrityError - IntegrityError at /***/ (1048, "Column '***' cannot be null") in python django / admin / app / price_list / add /处的IntegrityError(1048,“&#39;business_id_id&#39;列不能为空”) - IntegrityError at /admin/app/price_list/add/ (1048, “Column 'business_id_id' cannot be null”) django.db.utils.IntegrityError:(1048,“列&#39;category_id&#39;不能为空”))? - django.db.utils.IntegrityError: (1048, “Column 'category_id' cannot be null”)? django.db.utils.IntegrityError:(1048,“Column&#39;create_timestamp&#39;不能为null”) - django.db.utils.IntegrityError: (1048, “Column 'create_timestamp' cannot be null”) pymysql.err.IntegrityError:(1048,“列'dob'不能为空”) - pymysql.err.IntegrityError: (1048, “Column 'dob' cannot be null”) 1048, &quot;列 &#39;user_id&#39; 不能为空&quot; - 1048, "Column 'user_id' cannot be null" Django IntegrityError SQL:1048,不能为null - Django IntegrityError SQL: 1048, cannot be null django模型在以后要填充数据时抛出IntegrityError:(1048,“列&#39;xxx&#39;不能为空”) - django model throw IntegrityError: (1048, “Column 'xxx' cannot be null”) when I want to fill data later
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM