Home > OS >  Django Integrity Error 1048 cannot be null
Django Integrity Error 1048 cannot be null

Time:10-26

Under views.py

def addcomments(request):
    comment_text = request.POST.get('comment')
    user_id = request.POST.get('user_id')
    name = request.POST.get('name')
    email = request.POST.get('email')
    
    comment = Comment.objects.create(user_id=user_id, body=comment_text, name=name, email=email)
    comment.save()
    return HttpResponseRedirect(reverse('users:detail', args=(user_id, )))
   

this one, from detail.html

{% extends 'base.html' %}

{% block title %}
    {{ user.user_fname }} {{ user.user_lname }}
{% endblock %}

{% block content %}

{% if error_message %}
    <p >
        <strong>{{error_message}}</strong>
    </p>
{% endif %}


<div >

    <div >
        <div >
            <h1>{{ user.user_fname }} {{ user.user_lname }}</h1>
            <p >{{ user.user_email }}</p>
            <p>Position: {{ user.user_position }}</p>
        </div>
        
        <div >
            <img src="/users/media/{{user.user_image}}" alt="profile_user"  width="300">
            <!-- ito yung hinahanap ng search engine-->
        </div>
        
        <div >

            <a href="{% url 'users:delete' user.id %}" >Delete</a>
            <a href="{% url 'users:edit' user.id %}" >Edit</a>
            <a href="{% url 'users:index'%}" >Back</a>
    
        </div>  
    </div>
        
    <div >
        <h2>Comment</h2>
        <p > Number of comment : {{ comments_count }}</p>
        {% if comments_count > 0 %}
            {% for comment in comments %}
                {% if comment.active %}
                <p><strong>{{comment.name}}</strong> : {{comment.body}}</p>
                {% endif %}

            {% endfor %}
        {% endif %}

        <hr>
        <br><br><br><br><br><br>

        <form action="{%url 'users:addcomments'%}" method="post">
            
            {% csrf_token %}
            <div >
                <label>Comment</label>
                <textarea name="comment" id="comment" cols="30" rows="5"  required placeholder="Enter your comment here ..."></textarea>
            </div>
            
            <br>
            <input type="text" name="name" id="name" value="{{ user.user_lname }}">
            <input type="text" name="lname" id="lname" value="{{ user.user_fname }}">
            <input type="text" name="email" id="email" value="{{ user.user_email }}">
            <br><br><br><br>
            <button type="submit" >Add Comment</button>
        </form>
    </div>
</div>


{% endblock %}

this one for url.py

path('addcomments', views.addcomments, name='addcomments'),

I am having the error message,

IntegrityError at /users/addcomments (1048, "Column 'user_id' cannot be null")

During handling of the above exception ((1048, "Column 'user_id' cannot be null")), another exception occurred:

from 404 users\views.py, line 146, in addcomments comment = Comment.objects.create(user_id=users_id, body=comment_text, name=name, email=email) … Local vars Variable Value comment_text
'wwerwr' email
'[email protected]' name
'Heavy' request <WSGIRequest: POST '/users/addcomments'> user_id None

the heavyrain details, ignore it, was trying stuffs

this one for detail detail is working fine properly, detail is on left side of the page, while addcomments is on the right side of the same page

@login_required(login_url='/users/login')
def detail(request, profile_id):
    try:
        user = User.objects.get(pk=profile_id)
        comments = Comment.objects.filter(user_id=profile_id)
        comments_count = Comment.objects.filter(user_id=profile_id).count()
    except User.DoesNotExist:
        raise Http404("Profile does not exist")
    return render(request, 'UI/detail.html', {'user': user, 'comments': comments, 'comments_count': comments_count})

CodePudding user response:

You don't have an user_id field in your html form that's why it becomes None or null and since you don't allow your user_id to be none (don't allow it to be none) django fails to create it. I suggest you get rid of the user_id in the creation and search for a user with that exact unique username that posted the form with

user = request.user
comment = Comment.objects.create(user=user, body=comment_text, name=name, email=email)

CodePudding user response:

I'm sorry but use a model form. It's the MOST appropriate solution. It will manage rendering your form in html and ensuring data is clean and validated.

from django import forms

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = [...]

Please see further information in the documentation.

https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/

  • Related