[英]Why Bootstrap4 modal doesn't show data in my Django Template?
[英]Django with bootstrap4 and modal
當前,我正在研究一個項目,該項目的形式是一些用戶可以添加他們的想法,其他用戶可以評論(這些評論在Django中按用戶組划分類別)。 我遇到了一個問題,沒有收到模板的pk和django.urls.exceptions.NoReverseMatch:'user3_comment'的反向,沒有找到參數。 嘗試了1種模式:['帖子/評論/ user3 /(?P \\ d +)/ $']。 我的模型是這樣的:
models.py
class Post(models.Model):
post_title = models.CharField(max_length=200)
author = models.ForeignKey('auth.User', on_delete=models.PROTECT)
post_date = models.DateField(blank=True, null=True)
post_time = models.TimeField(blank=True, null=True)
post_text = models.TextField()
def __str__(self):
return self.post_title
class User3_comment(models.Model):
commented_post = models.ForeignKey(Post, on_delete=models.PROTECT, blank=True, null=True, related_name='user3_comment')
comment = models.TextField(blank=True)
(some lines skipped)
form.py類User3_comment_form(forms.Form):
class Meta:
model = User3_comment
fields = ['comment','commented_post']
views.py
def user3_comment(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = User3_comment_form(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.commented_post = post
return redirect('post_detail', pk=post.pk)
else:
form = User3_comment_form()
return render(request, 'myapp/user3_comment.html', {'form': form})
urls.py
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^post/comments/user3/(?P<pk>\d+)/$', views.user3_comment, name='user3_comment')
post_detail.html帖子詳細信息具有一個按鈕:
{% if request.user|is_authorized:"user3" %}
<div class="modal fade" id="modal"></div>
<button type="button" class="btn btn-primary open-modal" data-toggle="modal" data-target="#modal">Add user3 comment</button>
{% endif %}
以及模式代碼:
<script>
$('#modal').on('show.bs.modal', function (event) {
var modal = $(this)
$.ajax({
url: "{% url 'user3_comment' pk=post.initial.id %}",
context: document.body
}).done(function(response) {
modal.html(response);
});
})
</script>
問題是我得到一個:django.urls.exceptions.NoReverseMatch:反向'user3_comment',沒有找到參數。 嘗試了1個模式:['post / comments / user3 /(?P \\ d +)/ $'],但我似乎無法調試它。 模態本身可以工作,當我鏈接到它時我可以打開一個靜態頁面,但是我無法獲取ajax應該發送的pk。
如果與案例有關,我會為我的應用程序使用bootstrap4。
更新:我無法使用基於函數的視圖對問題進行排序,所以我切換到基於類的視圖,它解決了問題,對於對此解決方案感興趣的人,這就是我所做的事情:我使用了MultiFormsView(可以在github https://gist.github.com/jamesbrobb/748c47f46b9bd224b07f )並創建了MultiFormsView類:
class Post_Detail(MultiFormsView):
template_name = 'posts/post_detail.html'
form_classes = {'post': PostForm,
'user3_comment': User3CommentForm}
def get_success_url(self):
return reverse('post_detail', kwargs={'pk': post.pk})
def get_post_form_initial(self):
post = get_object_or_404(Post, pk=self.kwargs['pk'])
return { 'post_name': post.post_name,
'post_body': post.post_body, }
def get_user3_comment_form_initial(self):
try:
post = Post.objects.get(pk=self.kwargs['pk'])
comment = User3_Comment.objects.get(mark=post.pk)
return {'comment_body': post.comment_body,}
except User3_Comment.DoesNotExist:
pass
def get_context_data(self, **kwargs):
context = super(Post_Detail, self).get_context_data(**kwargs)
return context
class User3_Comment(CreateView):
model = User3_Comment
fields = ('post_body', )
template_name = 'posts/user3_comments.html'
def get_initial(self):
postx = self.kwargs['pk']
return {'post': postx}
def form_valid(self, form):
#save logic
return redirect('post_detail', pk=self.kwargs['pk'])
def get_success_url(self):
return reverse('post_detail', kwargs={'pk': self.object.pk})
initial
領域應該是什么? 沒有提及。 無論如何,您都可以嘗試以下操作:
{% with post.initial as initial %}
$.ajax({
url: "{% url 'user3_comment' pk=initial.id %}",
context: document.body
}).done(function(response) {
modal.html(response);
});
{% endwith %}
根據您的錯誤,您在此行url: "{% url 'user3_comment' pk=post.initial.id %}"
傳遞的參數為url: "{% url 'user3_comment' pk=post.initial.id %}"
。
在您看來,您實際上並沒有將Post
作為上下文傳遞給頁面,這可能就是為什么沒有價值的原因。
return render(request, 'myapp/user3_comment.html', {'form': form, 'post': post})
如果將{{ post.initial.id }}
或任何其他Post
字段放在頁面上的其他位置,它是否顯示值? 如果不是,則您的模板標簽是錯誤的。 根據您的模型,它應該是{{ post.id }}
。
<button type="button" name="modal_button" data-toggle="modal" data-target="#modal"
value="{{ post.id }}" class="Modal">
</button>
$('button.Modal').click(function() {
var post_id = $(this).val();
$.ajax({
url: "{% url 'user3_comment' pk=post_id %}",
context: document.body
}).done(function(response) {
$('#modal').html(response);
});
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.