1

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

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

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

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)

<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!

1 Answers1

0

You specify the name of a <select> input element in the <select> tag, not in the <option> tag. 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]. These encapsulate the logic, make it less likely to make mistakes, and remove a lot of boilerplate code.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555