T
T
thelionsin2020-02-04 08:42:40
Django
thelionsin, 2020-02-04 08:42:40

Why is the form not showing up in the modal window?

Actually, I understand why it’s not displayed, because there is some kind of error with the path, but I can’t understand what exactly, in general, the form is not displayed in the modal window, probably due to the fact that the path to which I specify the form is already taken others, here is the code:

class NewsView(ListView):
    model = News
    template_name = 'News.html'
    context_object_name = 'news'

class Add(CreateView):
    model = News
    template_name = 'News.html'
    form_class = ArticleForm
    success_url = reverse_lazy('news')


    path('news',NewsView.as_view(),name='news'),
    path('news',Add.as_view(),name='add'),


class ArticleForm(forms.ModelForm):
    class Meta:
        model = News
        fields = '__all__'


<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Добавить пост
</button>


<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="add_form" method="get">
            {% csrf_token %}
       {{form}}
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть</button>
        <button form="add_form" type="submit" class="btn btn-primary">Добавить</button>
      </div>
    </div>
  </div>
</div>


As you can see, I have two requests for news in the url, if I create a new template, empty, then the form is displayed there, and I need it to be where the records are displayed, the form is displayed, when you click on the button, the url does not change, t .e. you can't do let's say news/add, it just won't be displayed...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
diver22, 2020-02-05
@diver22

You don't need a separate url for the form in the modal window.
The correct approach would be to send the form data as a POST request to the 'news' url.
But if you still want a separate view-xy for adding news, then assign a separate url to it,
for example, change "news/add
" to
<form id="add_form" method="get">

<form action="/news/add" id="add_form" method="post">

Creating entities using the GET method is a bad idea. All parameters will be passed in the header.
Try to take the form out of the modal window and place it on the main page.
And make it work first like this. And then you can put it in a modal window.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question