【发布时间】:2018-11-25 12:57:18
【问题描述】:
我的网站目前在自己的页面上呈现表单。我现在正试图让它们在我的主页上的侧边栏 div 标签内呈现。但是,我不知道如何塑造 JavaScript 和/或视图,因此我将表单模板的 HTML 取回并插入到 div 标记中。
更新
我在控制台中收到以下错误:GET http://127.0.0.1:8000/new_trend/ 500 (Internal Server Error)
HTML(我想将表单模板注入到的主页上的标记):
<div id="sidebar">
</div>
JavaScript
$(function() {
$("#new-trend").click(function(event){
alert("User wants to add new trend"); //this works
$.ajax({
type: "GET",
url:"/new_trend/",
success: function(data) {
$('#sidebar').html(data),
openNav()
}
})
});
});
查看
def new_indicator(request):
# if this is a POST request we need to process the form data
if request.method == "POST":
# create a form instance and populate it with data from the request:
form = IndicatorForm(request.POST)
# check whether it's valid:
if form.is_valid():
indicator = form.save(commit=False)
indicator.author = request.user
indicator.modified_date = timezone.now()
indicator.save()
return redirect('dashboard')
else:
form = IndicatorForm()
return render(request, 'mysite/sidebar_trend.html', {'form': form})
【问题讨论】:
-
你应该解释一下它目前的作用。你有任何错误或错误行为吗?
-
是的,我意识到这一点并回来更新。我不知道如何调试终端,否则我会尝试发布更多详细信息。
标签: javascript django django-forms