2017-11-11 5 views
0

私は関数を定義し、URLに2つのパラメータを渡そうとしています。URLに2つのパラメータを渡すことができません

実行すると、それは誤りここ"not enough arguments for format string"

コード views.py

from django.shortcuts import render 

# Create your views here. 
from django.http import HttpResponse 

def detail(request, question_id,choice): 
    response="You're looking at question %s and choice %s." 
    return HttpResponse(response % question_id , choice) 

urls.py

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^$', views.index , name='index') , 
    url(r'^(?P<question_id>[0-9])/(?P<choice>[0-9]+)/$', views.detail , name='detail') , 
] 

私はURL

に合格であることを示しています

http://127.0.0.1:8000/polls/1/2/

エラー

TypeError at /polls/1/2/ 
not enough arguments for format string 

どのようにそれを解決するには?

答えて

0

を試してみてはPythonで文字列をフォーマットする古い方法です。それはだからあなたのコードが

def detail(request, question_id,choice): 
    response = "You're looking at question {} and choice {}.".format(question_id, choice) 
    return HttpResponse(response) 

詳細についてはpyformat.infoを見てくださいとなるよう

response = "You're looking at question {} and choice {}.".format(question_id, choice) 

として.format(..)を使用することをお勧めします。

+0

ときのよう デフ詳細(要求、question_id、選択肢)と一緒に使用し、これを: "あなたが質問{}を見ていると選択肢{}"。レスポンス=フォーマット(question_id、選択) リターンはHttpResponse( "文字列フォーマット中にすべての引数が変換されない" –

+0

申し訳ありませんが、 'format'の使用は'% 'を使用する代わりに使用されています。完全な例を示すために私の答えを更新しました。 – Tony

+0

ありがとう、それは働いた。 –

0

%を使用してreturn HttpResponse(response % (question_id, choice))

関連する問題