Django 表单和 GET 参数

发布于 2024-12-10 21:23:19 字数 1901 浏览 0 评论 0原文

我有这个:

<form action='/ltsp' method='GET'>
    <label>Download Option:</label>
    <select name=''>
        <option name='download' value='download'>download</option> 
        <option name='show' value='show'>show</option>           
    </select>
    <input type='submit' value='Ausführen'>  
</form>

在我的模板中。它在网址中显示为“....de/ltsp?=download”。现在在我看来,我想检查已选择哪个(显示/下载)。现在它看起来像这样:

def a(request):
    if request.method == 'GET':
        a = \
            A.objects.all().order_by('hostname').distinct()
        b = B.objects.all().order_by('name').distinct()
        if request.GET.get('name'):
            name = request.GET.get('ltsp', '')
            if name is 'download':
                response = render_to_response(
                    'thin/lts.conf', {
                        'a': a,
                        'b': b
                    }, mimetype='text/plain')
                response["Content-Disposition"] = "attachment; \
                    filename=lts.conf"
                return response
            elif name is 'show':
                return render_to_response(
                    'thin/lts.conf', {
                        'a': a,
                        'b': b
                    }, mimetype='text/plain')
            else:
                return HttpResponse(content="Failed", \
                    mimetype="text/plain", status=400)
        else:
            return HttpResponseBadRequest(content="Failed", mimetype="text/plain", \
                status=400)
    else:
        return HttpResponseBadRequest(content="Failed", mimetype="text/plain", \
            status=400)

显然这样做 'name = request.GET.get('ltsp', '')' 是错误的获取。我如何从该表格中下载或显示。我已经尝试过 'name = request.GET.get('name')' 和 name = request.GET.get('value') 。 感谢任何帮助!

I have this:

<form action='/ltsp' method='GET'>
    <label>Download Option:</label>
    <select name=''>
        <option name='download' value='download'>download</option> 
        <option name='show' value='show'>show</option>           
    </select>
    <input type='submit' value='Ausführen'>  
</form>

in my template. and it shows up like this '....de/ltsp?=download' in the url.Now in my view i want to check which (show/download) has been chosen. For now it looks like this:

def a(request):
    if request.method == 'GET':
        a = \
            A.objects.all().order_by('hostname').distinct()
        b = B.objects.all().order_by('name').distinct()
        if request.GET.get('name'):
            name = request.GET.get('ltsp', '')
            if name is 'download':
                response = render_to_response(
                    'thin/lts.conf', {
                        'a': a,
                        'b': b
                    }, mimetype='text/plain')
                response["Content-Disposition"] = "attachment; \
                    filename=lts.conf"
                return response
            elif name is 'show':
                return render_to_response(
                    'thin/lts.conf', {
                        'a': a,
                        'b': b
                    }, mimetype='text/plain')
            else:
                return HttpResponse(content="Failed", \
                    mimetype="text/plain", status=400)
        else:
            return HttpResponseBadRequest(content="Failed", mimetype="text/plain", \
                status=400)
    else:
        return HttpResponseBadRequest(content="Failed", mimetype="text/plain", \
            status=400)

Obviously doing this 'name = request.GET.get('ltsp', '')' is the wrong get. How do I get download or show from that form. I tried 'name = request.GET.get('name')' and name = request.GET.get('value') already. Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-12-17 21:23:19

尝试为 select 指定一个名称,并在获取所选值时使用该名称:

name = request.GET['name-of-select']

Try giving the select a name and using that when getting the selected value:

name = request.GET['name-of-select']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文