2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Djangoでのwebアプリ開発~GETでURLパラメータを取得する~

Posted at

自分のメモ用です。 h3

formタグのactionで指定のURLに飛ばして
request.GETでURLパラメータを取得する

※合っているかわからない

3つ設定すること

hello/urls.py
# 省略
urlpatterns = [
    #省略
    url(r'^get/$', views.hello_get_query, name='hello_get_query'),
    url(r'^get_name/$', views.hello_get_name, name='hello_get_name'),
]
template/get_query.html
    <form action="{% url 'hello:hello_get_name' %}" method="get">
        <label>名前:<input type="text" size="20" name="your_name"></label>
        <input type="submit" value="送信">
    </form>

actionに”{% url 'hello:hello_get_name' %}”と書くことで
レスポンスを返すビュー関数を指定することができる。

hello/views.py
# 省略
def hello_get_name(request):
   d = {
       'your_name': request.GET.get('your_name')
   }

   return render(request, 'get_name.html', d)
# 省略
template/get_name.html
    {% if your_name %}
        {{ your_name }}さんこんにちは
    {% endif %}

”http://****/hello/get_name/?your_name=9999”
となり、
getname.PNG

となった。

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?