django模板中数据展示的切换
Switch between data presentation in template of django
我是 django 的新手。
我不知道我问的东西是否在纯 Django 模板中可用。
我有从数据库加载数据的应用程序主页。
它在 div
中输出信息,然后使用 bootstrap 以获得更好的外观。
在那 div 我有图片和标题。
有没有办法让用户能够在 views
之间切换,以便他们可以选择是否喜欢 div
,或者例如 table
只有标题?
我知道 ajax-based 框架有类似的东西,但从未使用过。
我还想在切换视图时保留模型中的数据,因为我不想在切换视图时向数据库询问数据。
可能吗?或者唯一的方法是制作单独的模板并为 /view/ 添加 route
所以我将用户重定向到具有不同模板的不同页面并处理作为新页面的查看(使用重新加载模型)
我不确定这是一个好的做法,但如果您真的希望它在同一个 html 文件中:
使用 Django 模板语言评估 "view" 用户想要的。
修改您的 html 文件以将其包含在正文中。
首先,我们创建一个表单,询问用户他想要什么视图,然后我们使用上下文字典中的数据进行评估
<form role="form" id="view_form" method="post" action="<same url>">
<input type="radio" name="view" value="view1" checked> View #1 <br> <!-- this one will be checked by default -->
<input type="radio" name="view" value="view2"> View #2 <br>
<input type="radio" name="view" value="view3"> View #3 <br>
<button type="submit" name="submit">Change View</button>
</form>
{% if view == view1 %}
<!-- your code here -->
{% elif view == view2 %}
<!-- your code here -->
{% endif %}
修改您在 views.py 中的视图以捕获表单中的数据并将其添加到上下文字典中。
def page(request):
context_dict = {}
if request.method=='POST':
view = request.POST.get['view']
context_dict['view'] = str(view)
#The rest of your code here
....
不过我认为最好在视图函数中评估视图值并为每个值呈现不同的模板
def page(request):
# Your code first
view_value = None
if request.method=='POST':
view = request.POST.get['view']
if view = 'view1':
render(request, 'your_template.html', context_dict) # If you don't have a context, simply don't include it in this line
elif view = 'view2':
render(request, 'your_template2.html', context_dict)
... # All your other possible views
else:
render(request, 'default_template.html', context_dict) # If there's no match, load the default one.
我是 django 的新手。 我不知道我问的东西是否在纯 Django 模板中可用。
我有从数据库加载数据的应用程序主页。
它在 div
中输出信息,然后使用 bootstrap 以获得更好的外观。
在那 div 我有图片和标题。
有没有办法让用户能够在 views
之间切换,以便他们可以选择是否喜欢 div
,或者例如 table
只有标题?
我知道 ajax-based 框架有类似的东西,但从未使用过。
我还想在切换视图时保留模型中的数据,因为我不想在切换视图时向数据库询问数据。
可能吗?或者唯一的方法是制作单独的模板并为 /view/ 添加 route
所以我将用户重定向到具有不同模板的不同页面并处理作为新页面的查看(使用重新加载模型)
我不确定这是一个好的做法,但如果您真的希望它在同一个 html 文件中:
使用 Django 模板语言评估 "view" 用户想要的。
修改您的 html 文件以将其包含在正文中。 首先,我们创建一个表单,询问用户他想要什么视图,然后我们使用上下文字典中的数据进行评估
<form role="form" id="view_form" method="post" action="<same url>">
<input type="radio" name="view" value="view1" checked> View #1 <br> <!-- this one will be checked by default -->
<input type="radio" name="view" value="view2"> View #2 <br>
<input type="radio" name="view" value="view3"> View #3 <br>
<button type="submit" name="submit">Change View</button>
</form>
{% if view == view1 %}
<!-- your code here -->
{% elif view == view2 %}
<!-- your code here -->
{% endif %}
修改您在 views.py 中的视图以捕获表单中的数据并将其添加到上下文字典中。
def page(request):
context_dict = {}
if request.method=='POST':
view = request.POST.get['view']
context_dict['view'] = str(view)
#The rest of your code here
....
不过我认为最好在视图函数中评估视图值并为每个值呈现不同的模板
def page(request):
# Your code first
view_value = None
if request.method=='POST':
view = request.POST.get['view']
if view = 'view1':
render(request, 'your_template.html', context_dict) # If you don't have a context, simply don't include it in this line
elif view = 'view2':
render(request, 'your_template2.html', context_dict)
... # All your other possible views
else:
render(request, 'default_template.html', context_dict) # If there's no match, load the default one.