Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 形式-动作属性_Python_Django - Fatal编程技术网

Python 形式-动作属性

Python 形式-动作属性,python,django,Python,Django,我想创建一个“index.html”Django模板,其中包含一个按钮。当按下按钮时,我想呈现模板“home.html”,它本身显示值“123”。(当然,有一种更简单的方法来完成这项特定的任务——但我正在学习Django,所以想用这种方法来尝试。) 这是我的views.py文件: from django.shortcuts import render def home(request, x) context = {'x': x} return render(request, '

我想创建一个“index.html”Django模板,其中包含一个按钮。当按下按钮时,我想呈现模板“home.html”,它本身显示值“123”。(当然,有一种更简单的方法来完成这项特定的任务——但我正在学习Django,所以想用这种方法来尝试。)

这是我的views.py文件:

from django.shortcuts import render

def home(request, x)
    context = {'x': x}
    return render(request, 'home.html', context)
from django.conf.urls import patterns, include, url

from myapp import views

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^home', views.home, name='home'),
)
这是我的URL.py文件:

from django.shortcuts import render

def home(request, x)
    context = {'x': x}
    return render(request, 'home.html', context)
from django.conf.urls import patterns, include, url

from myapp import views

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^home', views.home, name='home'),
)
这是my home.html文件:

<html>
<body>
The value is: {{ x }}
</body>
</html>
<html>
<form method="post" action=???>
<input type="button" value="Click Me">
</form>

该值为:{x}
最后,这里是我的index.html文件:

<html>
<body>
The value is: {{ x }}
</body>
</html>
<html>
<form method="post" action=???>
<input type="button" value="Click Me">
</form>

有人能告诉我我需要在上面的动作属性中写些什么来代替?我试过设置“{%url'home'123%}”但这给了我一个“NoReverseMatch”错误。因此,我怀疑我的URL.py文件也可能有问题


谢谢大家!

由于没有捕获随url一起发送的123的url,因此会出现NoReverseMatch错误。让我告诉你一个简单的方法:

您可以将操作设置为类似以下内容:

action="/home/123" # or any integer you wish to send.
并在url py中匹配该url,方法是将主url修改为:

url(r'^home/(?P<x>\d+)/$', views.home, name='home')
url(r'^home/(?P\d+/$),views.home,name='home')

这会将您在主url中发送的任何参数(在本例中应为整数)传递为x。因此,x将显示在home.html中,请像这样重写index.html

<html>
<form method="post" action=/home>
<input type="hidden" name="my_value" value="123">
<input type="button" value="Click Me">
</form>