Python Forum
Submit button not working when using form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Submit button not working when using form
#1
Hi all. I'm studying Django and i want to create a forum.
This is the html code code:

{% extends 'base.html' %}

{% load static %}
{% load crispy_forms_tags %}

{% block title %}Post a reply{% endblock %}

{% block stylesheet %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
{% endblock %}

{% block breadcrumb %}
<li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li>
<li class="breadcrumb-item"><a href="{% url 'topic_list' topic.board.pk %}">{{ topic.board.name }}</a></li>
<li class="breadcrumb-item"><a href="{% url 'post_list' topic.board.pk topic.pk %}">{{ topic.subject }}</a></li>
<li class="breadcrumb-item active">Post a reply</li>
{% endblock %}

{% block content %}

<form class="mb-5" method="post">
{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-primary" type="submit" value="Post a reply">
</form>

{% for post in topic.get_last_ten_posts %}
<div class="card mb-2">
<div class="card-body p-3">
<div class="row mb-3">
<div class="col-6">
<strong class="text-muted">{{ post.created_by.username }}</strong>
</div>
<div class="col-6 text-right">
<small class="text-muted">{{ post.created_at }}</small>
</div>
</div>
{{ post.get_message_as_markdown }}
</div>
</div>
{% endfor %}

{% endblock %}

{% block javascript %}
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
<script>
var simplemde = new SimpleMDE();
</script>
{% endblock %}

Here is the model:
class Post(TimestampedModel):
    message = models.CharField(max_length=4000)
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='posts')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    last_updated_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='+')

    def __str__(self):
        truncated_msg = Truncator(self.message)
        return truncated_msg.chars(30)

    def get_message_as_markdown(self):
        return mark_safe(markdown(self.message, safe_mode='escape'))
If i using this view code:
class PostCreateView(LoginRequiredMixin, generic.CreateView):
    model = models.Post
    fields = ('message',)
    template_name = 'boards/reply_topic.html'

    def get_context_data(self, **kwargs):
        kwargs['topic'] = get_object_or_404(models.Topic,
                                            pk=self.kwargs.get('topic_pk'),
                                            board__pk=self.kwargs.get('board_pk'))
        return super().get_context_data(**kwargs)

    def get_success_url(self):
        return reverse_lazy('post_list', kwargs={'board_pk': self.kwargs.get('board_pk'),
                                                 'topic_pk': self.kwargs.get('topic_pk')})

    def form_valid(self, form):
        user = self.request.user
        topic = get_object_or_404(models.Topic,
                                  pk=self.kwargs.get('topic_pk'),
                                  board__pk=self.kwargs.get('board_pk'))
        post = form.instance
        post.topic = topic
        post.created_by = user
        post.last_updated_by = user
        post.save()
        return HttpResponseRedirect(self.get_success_url())
A new post will be created after i click submit (Post a reply) button and page will be redirected to get_success_url function (post_list)

But i want to add text editor when creating a new post using SimpleMDE() javascript library
I changed the view code for html:
class PostCreateView(LoginRequiredMixin, generic.CreateView):
    model = models.Post
    fields = ('message',)
    template_name = 'boards/reply_topic.html'

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['message'].widget = forms.Textarea()
        return form

    def get_context_data(self, **kwargs):
        kwargs['topic'] = get_object_or_404(models.Topic,
                                            pk=self.kwargs.get('topic_pk'),
                                            board__pk=self.kwargs.get('board_pk'))
        return super().get_context_data(**kwargs)

    def get_success_url(self):
        return reverse_lazy('post_list', kwargs={'board_pk': self.kwargs.get('board_pk'),
                                                 'topic_pk': self.kwargs.get('topic_pk')})

    def form_valid(self, form):
        user = self.request.user
        topic = get_object_or_404(models.Topic,
                                  pk=self.kwargs.get('topic_pk'),
                                  board__pk=self.kwargs.get('board_pk'))
        post = form.instance
        post.topic = topic
        post.created_by = user
        post.last_updated_by = user
        post.save()
        return HttpResponseRedirect(self.get_success_url())
or using a form with code:
class NewPostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('message',)
        widgets = {'message': forms.Textarea()}
with view code:
class PostCreateView(LoginRequiredMixin, generic.CreateView):
    model = models.Post
    form_class = NewPostForm
    template_name = 'boards/reply_topic.html'
After changing the code, text editor is showed when creating a new post. But when a click submit (Post a reply) button, nothing happen. Post is not created when i click submit button and nothing happen when i click this button. no redirect to get_success_url page.
Here is full source code: Link to full source code

What should i do if i want to using text editor for this field?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Python request without selenium on html form with javascript onclick submit but eraosa 0 141 Jan-09-2021, 06:08 PM
Last Post: eraosa
  Contact form button click action Man_from_India 1 684 Feb-01-2020, 06:21 PM
Last Post: snippsat
  Radio button in form Heinrich 1 447 Jan-14-2020, 05:41 PM
Last Post: Heinrich
  form fill & button clicks rudolphyaber 3 1,591 Apr-25-2019, 06:32 PM
Last Post: Larz60+
  Creating few models with one submit depending on the selected items from multiselect? dmytruk 0 654 Mar-18-2019, 03:27 AM
Last Post: dmytruk
  submit element from a list into a post request Godzilla 0 2,551 Mar-24-2018, 02:35 PM
Last Post: Godzilla

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020