我是新手Django用户,正在尝试将上传文件表单放入包含标签中,以便在各种模板中使用。我创建了以下包含标签:
#upload_files.py
@register.inclusion_tag('upload_form.html') def upload_handler(context): request = context['request'] view_url = reverse('upload.views.upload_handler') if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect(view_url)
upload_url, upload_data = prepare_upload(request, view_url) form = UploadForm()
upload_model_list = UploadModel.objects.all().order_by('-pub_date')
现在我想将其包含在模板中,所以在页面上我有:
#mypage.html {% extends 'base.html' %} {% load upload_files %}
{% upload_handler %}
我得到以下错误: upload_handler需要1个参数
请问我应该从模板传递什么参数?
#upload_files.py
@register.inclusion_tag('upload_form.html') def upload_handler(context): request = context['request'] view_url = reverse('upload.views.upload_handler') if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect(view_url)
upload_url, upload_data = prepare_upload(request, view_url) form = UploadForm()
upload_model_list = UploadModel.objects.all().order_by('-pub_date')
现在我想将其包含在模板中,所以在页面上我有:
#mypage.html {% extends 'base.html' %} {% load upload_files %}
{% upload_handler %}
我得到以下错误: upload_handler需要1个参数
请问我应该从模板传递什么参数?
GET
,因为登录视图很可能在表单发送后执行重定向,所以您无法在那里访问请求数据... - Bernhard Vallant