如何从views.py测试选中的单选按钮?

时间:2016-04-15 17:06:21

标签: python html django

我有一个django项目,其中我的主页包含单选按钮,这让我有机会去另一个应用程序或显示消息。我认为我的按钮检查测试没有正确写入。 登录/ views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required,user_passes_test
from aps import views
from django.http import HttpResponse

@login_required()
def home(request):
    if request.user.is_superuser:
        return render(request,"home.html") 
        if ('choix1').checked  in request.POST:
            return HttpResponse("hedha lmail")
        elif 'choix2'  in request.POST:
            return HttpResponse("hedha ltemps")
        elif 'choix3'  in request.POST:
            return views.index(request)
        else :
            return HttpResponse("You have not chosenn ay choice")
    else:
        return views.index(request)

home.html的



{% extends 'base.html' %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="jumbotron">
            <h1>Hello</h1>
            <p>You are on your account</p>

        </div>
        <div class="jumbotron">
            
<FORM>
<p>you have the opprtunity to : </P>
<INPUT type= "radio" name="choix1" id="choix1" value="mail"> change mail sender
<INPUT type= "radio" name="choix2" id="choix2" value="temps"> change frequency of getting temperqture sensor's value
<INPUT type= "radio" name="choix3" id="choix3" value="graph"> show graph  <br>
<INPUT type= "submit" value="ok"> 
</FORM>
        </div>
    </div>
</div>

{% endblock %}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

首先,删除

if

因为您的应用未达到<form method="POST"> 支票。

另外,将POST方法添加到您的html表单:

if 'choix1' in request.POST:
    ...

查看是否选择了收音机:

$new_array = array(); // create a new array
$sql=mysql_query("SELECT * FROM animate ORDER BY RAND() LIMIT 10") or die("query Field");

 while($row=mysql_fetch_array($sql)){

    $new_array[] = $row['code']; // assign value to array

 }
echo "<pre/>";print_r($new_array); // print array which is created

答案 1 :(得分:0)

@login_required()
def home(request):
    if request.user.is_superuser:
        return render(request,"home.html")           #this line I am talking about
        if ('choix1').checked  in request.POST:
            return HttpResponse("hedha lmail")
        elif 'choix2'  in request.POST:
            return HttpResponse("hedha ltemps")
        elif 'choix3'  in request.POST:
            return views.index(request)
        else :
            return HttpResponse("You have not chosenn ay choice")
    else:
        return views.index(request)

在上述方法的第4行中,您将在检查任何条件之前返回您的方法。