T
T
thelionsin2020-02-04 17:26:42
Django
thelionsin, 2020-02-04 17:26:42

The form is not displayed in the modal window, how to make it display?

then this form is displayed there, and everything works, BUT, I want the modal window to be exactly where I have news, path('news',NewsView.as_view(),name='news') , and as I understand the form is not displayed, because in my url for adding news through the form path('news',Add.as_view(),name='add'), there is the same name as the news itself, i.e. news, Django probably thinks that the news link is already occupied by displaying news, and the form is not displayed, because when you call the modal window, nothing is added to the url, that is, /news/ remains the same as it was, and now, I need to the news page, the modal window with the form worked, it is necessary somehow that when you click on "add", the modal window is called and news / add is added, for example, and then the form should work ... I hope the essence is clear,

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>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question