如何从模板中的view.py渲染调用我的dict参数?

时间:2018-04-28 21:04:28

标签: django templates dictionary view parameters

我已经为此工作了两天,并且已经阅读了stackoverflow上的几乎所有示例并查阅了django文档。我试图将我的dict从views.py传递到我的模板,但我一直在愚蠢的#34;无法解析其余的"错误。我没有做任何花哨的事情。只需Href按钮传入一个参数来表示该按钮是什么。然后使用该参数作为字符串打开模板页面,以使新页面和网址唯一。

传递:

<a href="{% url 'site:call' 1 %}" class="btn">Call</a>

urls.py

urlpatterns = [
     url(r'^call=(\d+)/$', views.call, name='call')
]

views.py

def call(request, callID):
    call_id = { 'id':callID }
    return render(request, 'site/call.html', call_id)

致电模板

{% extends 'site/layout.html' %}
{% block content %}
{% with call_id.get('id') as view_id %}
<h3 class="center-align blue lighten-3">Site # Room {{ view_id }}</h3>
<a href="/site/cancel.html" class="btn">Cancel</a>
{% endwith %}

{% endblock %}

我已经尝试过request.GET.get(&#39; id&#39;)以及其他许多东西。有人能告诉我如何实际解析我传入的那些dict值吗?

1 个答案:

答案 0 :(得分:0)

你实际上根本没有传递字典。您传递的是单个值""" test.py user info <user name> test.py user create <user name> <last name> <first name> <email> test.py user create batch <file name> test.py user delete <user name> test.py room info <room number> test.py room create <room number> <room name> test.py room delete <room number """ from argparse import ArgumentParser import sys def main_user_info(args): print('You called "user info" for user {}'.format(args.user_name)) def main_user_create(args): print('You called "user create" for : {u}, {last} {first} {mail}'.format(u=args.user_name, last=args.last_name, first=args.first_name, mail=args.email)) def main_user_create_batch(args): print('User batch creation using file {}'.format(args.filename)) def main_user_delete(args): pass def main_room_info(args): print('Room info called for room number {}'.format(args.room_number)) def main_room_create(args): print('Creating room number {nr} with name {name}'.format(nr=args.room_number, name=args.room_name)) def main_room_delete(args): pass def parser_user_default(parser): parser.add_argument('user_name', metavar='<user name>') return parser def parser_user_create(parser): parser = parser_user_default(parser) # argument <user name> parser.add_argument('last_name', metavar='<last name>') parser.add_argument('first_name', metavar='<first name>') parser.add_argument('email', metavar='<email>') return parser def parser_room_default(parser): parser.add_argument('room_number', metavar='<room number>') # Above: restrict room number to ints: 'type=int' # Restrict room number to known rooms: 'choices=list_of_known_rooms' # Restrict room numbers to 1 to 1000: 'choices=range(1, 1001)' return parser def parser_room_create(parser): parser = parser_room_default(parser) parser.add_argument('room_name', metavar='<room name>') return parser parse_config = { 'user': {'info': {'desc': 'Show user info', 'main': main_user_info, 'prepare_parser': parser_user_default}, 'create': {'desc': 'Create new user', 'main': main_user_create, 'prepare_parser': parser_user_create}, 'delete': {'desc': 'Delete user', 'main': main_user_delete, 'prepare_parser': parser_user_default} }, 'room': {'info': {'desc': 'Show room info', 'main': main_room_info, 'prepare_parser': parser_room_default}, 'create': {'desc': 'Show room info', 'main': main_room_create, 'prepare_parser': parser_room_create}, 'delete': {'desc': 'Delete a room', 'main': main_room_delete, 'prepare_parser': parser_room_default} }, } def prepare_verb_args(parser, category): subparsers = parser.add_subparsers(title='verb') for verb, config in parse_config[category].items(): subparser = subparsers.add_parser(verb, description=config['desc']) subparser = config['prepare_parser'](subparser) # Reading 'set_defaults' is helpful. Notably, this overwrites default of args.main # when given this particular category on command line. subparser.set_defaults(main=config['main']) return parser if __name__ == '__main__': parser = ArgumentParser() parser.set_defaults(main=lambda _: parser.print_usage()) # Inspired by 'catkin_tools'. subparsers = parser.add_subparsers(dest='category', title='category') for c in parse_config.keys(): subparser = subparsers.add_parser(c, description='Act on {}'.format(c)) subparser = prepare_verb_args(subparser, category=c) # NOTE: Somehow the lambda binds to the last 'subparser' in the loop. # This is a bug but I haven't got the time to fix it. subparser.set_defaults(main=lambda x: subparser.print_usage()) args = parser.parse_args() sys.exit(args.main(args)) ,因此您应该使用它。并且不需要任何id块。

with