Answer the question
In order to leave comments, you need to log in
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">×</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
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">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question