2016-04-15 4 views
2

私のホームページにはラジオボタンが含まれているので、別のアプリケーションに移動したりメッセージを表示することができます。チェックしたボタンのテストが正しく書かれていないと思います。 ログ/ views.pyviews.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 %}

答えて

0

まず、あなたのアプリがifチェックに到達していないので、

return render(request,"home.html") 

を削除。また

、あなたのHTMLフォームにPOSTメソッドを追加します。

​​

をラジオが選択されているかどうかを確認するには、次の行番号4で

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

禁止されています(CSRFトークンがないか間違っています):/ log/and home .htmlスーパーユーザーが最初に家にアクセスする必要があるため、削除できません。 – user5996908

+0

'django.middleware.csrf.CsrfViewMiddleware 'あなたの' settings.py'ファイルの 'MIDDLEWARE_CLASSES'から – ahmed

+0

ありがとうございます。しかし、削除してもうまくいきません。 – user5996908

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) 

を上記の方法では、あなたが戻ってきていますメソッドを呼び出すことができます。

+0

スーパーユーザーはhome.htmlのボタンを選択する必要があり、チェックされたユーザーは別のアプリケーションにアクセスしたり、メッセージを表示したりすることができます – user5996908

関連する問題