Home > Software design >  Why I can't save values in database? Django
Why I can't save values in database? Django

Time:04-10

I want to save my form in database, but save() doesn't work. When I do this, error wasn't showing. At the start, I think problem was in database, but it isn't

views.py

def comments(request):
     comments = Comment.objects.all()
     form = CommentForm()
     context = {"comments": comments, "form": form}
     if request.method == "POST":
         form = CommentForm(request.POST)
         if form.is_valid():
             comment = form.save(commit=False)
             comment.avtor = request.user
             comment.save() 
             return HttpResponseRedirect(reverse('comment'))
         else:
             context["form"] = form
             return render(request, "home/comments.html", context)
     else:
         return render(request, "home/comments.html", context)

And models. So, I think problem yet in views.py. I bad know how function save() is working.

models.py

class Comment(models.Model):
     Text = models.TextField(verbose_name='Text')
     date = models.DateTimeField(default=timezone.now, verbose_name='date')
     avtor = models.ForeignKey(User, verbose_name='avtor', on_delete=models.CASCADE)

     def __str__(self):
         return 'Comment {} at {}'.format(self.avtor, self.date)

     class Meta:
         ordering = ["-id"]

forms.py

class CommentForm(ModelForm):
     class Meta:
         model = Comment
         fields = ("Text",)

At the last, I want to save avtor, text and Date. Help me please.

CodePudding user response:

<div  style="min-height: 520px;">
    <form  action="/" method="post">
        {% csrf_token %}
        {{ form }}
        <br>
        <button style="width: 6%!important;"  type="submit">submit</button>

    </form>
    <div >
        {% for comentPost in comments %}
            <div  style="border-radius: 40px; padding: 20px;">
                <div style="border-bottom: 3px solid black;" >{{ comentPost.avtor }}</div>
                <div style="" >{{ comentPost.Text }}</div>    
                <div style="" >{{ comentPost.date|date:"F d, Время: h:i" }}</div>    
            </div>
        {% endfor %}

    </div>
</div>

CodePudding user response:

The action attribute specifies where to send the form-data when a form is submitted (https://www.w3schools.com/tags/att_form_action.asp).

So when you put the "/" in there, the form will send the data to the home page (represented by / in html). This means that your data doesn't get to the POST section of your def comments(request) method. When the method is not called, the logical outcome is thus that there isn't a new comment added.

This is the correct code:

<div  style="min-height: 520px;">
    <form  method="post">
        {% csrf_token %}
        {{ form }}
        <br>
        <button style="width: 6%!important;"  type="submit">submit</button>

    </form>
    <div >
        {% for comentPost in comments %}
            <div  style="border-radius: 40px; padding: 20px;">
                <div style="border-bottom: 3px solid black;" >{{ comentPost.avtor }}</div>
                <div style="" >{{ comentPost.Text }}</div>    
                <div style="" >{{ comentPost.date|date:"F d, Время: h:i" }}</div>    
            </div>
        {% endfor %}

    </div>
</div>


  • Related